Java面向对象基础练习有哪些?
- 内容介绍
- 文章标签
- 相关推荐
本文共计348个文字,预计阅读时间需要2分钟。
java// 定义西游人物类class XiyouFigure { private String name; private double height; private String weapon;
// 构造方法初始化属性 public XiyouFigure(String name, double height, String weapon) { this.name=name; this.height=height; this.weapon=weapon; }
// 输出人物详细信息的方法 public void printDetails() { System.out.println(姓名: + name); System.out.println(身高: + height + cm); System.out.println(武器: + weapon); }}
// 主类public class Main { public static void main(String[] args) { // 创建两个西游人物对象 XiyouFigure sunWukong=new XiyouFigure(孙悟空, 175, 如意金箍棒); XiyouFigure pig=new XiyouFigure(猪八戒, 200, 九齿钉耙);
// 输出人物详细信息 sunWukong.printDetails(); pig.printDetails(); }}
6.按要求编写Java应用程序: (1)定义西游记人物类,有属性:名字、身高和武器, 有构造方法初始化属性,还有输出人物详细信息的方法。 (2)在主类的main方法中创建两个西游记人物对象,分别调 用方法输出各自的详细信息。public class Peoper { String name; int height; String weapon; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getHeight() { return height; } public void setHeight(int height) { this.height = height; } public String getWeapon() { return weapon; } public void setWeapon(String weapon) { this.weapon = weapon; } public Peoper(String name, int height, String weapon) { super(); this.name = name; this.height = height; this.weapon = weapon; } } package gh; public class text6 { public static void main(String[] args) { Peoper sunwukong= new Peoper("孙悟空",165,"金箍棒"); Peoper zhubajie=new Peoper("猪八戒",187,"耙子"); System.out.println(sunwukong.getName()+" "+sunwukong.getHeight()+" "+sunwukong.getWeapon()); System.out.println(zhubajie.getName()+" "+ zhubajie.getHeight()+" "+zhubajie.getWeapon()); } }
本文共计348个文字,预计阅读时间需要2分钟。
java// 定义西游人物类class XiyouFigure { private String name; private double height; private String weapon;
// 构造方法初始化属性 public XiyouFigure(String name, double height, String weapon) { this.name=name; this.height=height; this.weapon=weapon; }
// 输出人物详细信息的方法 public void printDetails() { System.out.println(姓名: + name); System.out.println(身高: + height + cm); System.out.println(武器: + weapon); }}
// 主类public class Main { public static void main(String[] args) { // 创建两个西游人物对象 XiyouFigure sunWukong=new XiyouFigure(孙悟空, 175, 如意金箍棒); XiyouFigure pig=new XiyouFigure(猪八戒, 200, 九齿钉耙);
// 输出人物详细信息 sunWukong.printDetails(); pig.printDetails(); }}
6.按要求编写Java应用程序: (1)定义西游记人物类,有属性:名字、身高和武器, 有构造方法初始化属性,还有输出人物详细信息的方法。 (2)在主类的main方法中创建两个西游记人物对象,分别调 用方法输出各自的详细信息。public class Peoper { String name; int height; String weapon; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getHeight() { return height; } public void setHeight(int height) { this.height = height; } public String getWeapon() { return weapon; } public void setWeapon(String weapon) { this.weapon = weapon; } public Peoper(String name, int height, String weapon) { super(); this.name = name; this.height = height; this.weapon = weapon; } } package gh; public class text6 { public static void main(String[] args) { Peoper sunwukong= new Peoper("孙悟空",165,"金箍棒"); Peoper zhubajie=new Peoper("猪八戒",187,"耙子"); System.out.println(sunwukong.getName()+" "+sunwukong.getHeight()+" "+sunwukong.getWeapon()); System.out.println(zhubajie.getName()+" "+ zhubajie.getHeight()+" "+zhubajie.getWeapon()); } }

