Animal.java
public class Animal { /** * @auther JACK; * @method Con_structure、Getter and Setter、toString; * @package including:name、age、variety; * @inheritance Cats、Dogs and so on; * @limitAge age>0&&age<30; */ private String name; private int age; private String variety; /**仅对子类产生作用 public Animal() { } public Animal(String name, int age, String variety) { this.name = name; this.age = age; this.variety = variety; } */ @Override public String toString() { return "Animal{" + "name='" + name + '\'' + ", age=" + age + ", variety='" + variety + '\'' + '}'; } public int getAge() { return age; } public void setAge(int age) { if (age < 0 || age > 30) { System.out.println("请输入正确的年龄"); } else { this.age = age; } } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getVariety() { return variety; } public void setVariety(String variety) { this.variety = variety; } public void eat(){ System.out.println(this.name +"正在吃饭"); } public void sleep(){ System.out.println(this.name+"正在吃饭"); } public void crazy(){ System.out.println(this.name + "正在发狂"); } public void check(){ System.out.println(this.name + "正在检查身体"); } }
plot.java
public class plot { /** * 使用单例设计模式设计小区名称; * 小区名称设置为“富贵小区”,且不可更改; */ private static final plot NameInstance = new plot(); private plot(){ } public static plot getNameInstance(){ return NameInstance; } public void PlotName(){ System.out.println("富贵小区"); } }
Dogs.java
public class Dogs extends Animal { }
Labrador.java
public class Labrador extends Dogs{ }
Cats,java
public class Cats extends Animal { }
Applicatin.java
public class ApplicationRun { public static void main(String[] args) { plot PLOT_NAME = plot.getNameInstance(); PLOT_NAME.PlotName(); Cats XIAO_ZHANG_PETS = new Cats(); XIAO_ZHANG_PETS.setName("Tom"); XIAO_ZHANG_PETS.setAge(2); XIAO_ZHANG_PETS.setVariety("布偶"); System.out.println(XIAO_ZHANG_PETS); Dogs XIAO_MING_PETS = new Dogs(); XIAO_MING_PETS.setName("Ton"); XIAO_MING_PETS.setAge(4); XIAO_MING_PETS.setVariety("拉布拉多"); System.out.println(XIAO_MING_PETS); XIAO_MING_PETS.eat(); XIAO_ZHANG_PETS.check(); Labrador labrador = new Labrador(); } }
注意:java支持多层继承,但是不支持一个class同时继承两个class,即class A extends B,C,这是错误的