正在回答 回答被采纳积分+1
5回答
OrcaIMOOC
2017-11-29 14:41:35
public class Z_Cat_T { public static void main(String[] args) { //对象实例化 //对象:猫1; Z_Cat one = new Z_Cat(); //猫1,test赋值:姓名、年龄、体重、品种、价格 one.setName("波子"); one.setAge(3);//-3 one.setWeight(23); one.setSpecies("英短"); //对象:猫2; Z_Cat two = new Z_Cat(); //猫2,test赋值:同上 two.setName("阿龙"); two.setAge(2); two.setWeight(20); two.setSpecies("中华田园"); //输出属性 System.out.println("呢称:" + one.getName()); if(one.getAge()==0) return; System.out.println("年龄:" + one.getAge()); System.out.println("体重:" + one.getWeight()); System.out.println("品种:" + one.getSpecies()); //输出方法 one.run(one.getName()); one.eat(one.getName()); }
OrcaIMOOC
2017-11-29 14:40:16
public class Z_Cat { //属性:隐藏属性可见性 —— private private String name; //隐藏后,实例化测试页不能访问。限定当前类内访问 private int age; private double weight; private String species; //构造方法 public Z_Cat() {} static int temp = 14; public Z_Cat(int age) { //this.age = age; this.setAge(age); } //公有方法:get/set public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { if(age<=0) System.out.println("输入年龄错误,必须大于0"); else this.age = age; } public double getWeight() { return weight; } public void setWeight(double weight) { this.weight = weight; } public String getSpecies() { return species; } public void setSpecies(String species) { this.species = species; } //成员方法 //方法:错误提示。 public void mistake() { System.out.println("输入信息错误"); } //方法:跑 public void run() { System.out.println("跑"); } public void run(String name){ this.name = name; System.out.println(name + "跑"); } //方法:吃 public void eat() { System.out.println("吃"); } public void eat(String name) { this.name = name; System.out.println(name + "吃"); } }
Java零基础入门18
- 参与学习 人
- 提交作业 7317 份
- 解答问题 14452 个
想要入门学编程?多年一直活跃在编程语言排行版前列的Java是一个很好的选择。本路径将从Java基础语法、面向对象、常用工具类三部分,为你开启软件开发的大门!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星