ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
>[success] # 综合案例一 ~~~ package src; public class Animal { private String name; private String color; public Animal(String name, String color) { setColor(color); setName(name); } public Animal() { } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } public void show(){ System.out.println(color+","+name); } } ~~~ * 子 ~~~ package src; public class Dog extends Animal{ private int tooth; public Dog(){ super(); } public Dog(String name,String color,int tooth){ super(name,color); setTooth(tooth); } // 封装 public void setTooth(int tooth){ if(tooth>0){ this.tooth = tooth; }else{ System.out.print("牙齿不能小于0"); } } public int getTooth(){ return this.tooth; } // 重写show 方法 @Override public void show(){ super.show(); System.out.print(tooth); } } ~~~ * 运行 ~~~ package src; public class DogTest { public static void main(String[] args) { Dog dog = new Dog(); dog.show(); // null,null 0 Dog dog1 = new Dog("w","黄色",20); // 黄色,w 20 dog1.show(); } } ~~~