合规国际互联网加速 OSASE为企业客户提供高速稳定SD-WAN国际加速解决方案。 广告
>[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(); } } ~~~