一、多态定义方法并调用子类特有方法
public class Animal {private String colour;private int age;public Animal(){}public Animal(int age,String colour){this.age=age;this.colour=colour;}public String getColour() {return colour;}public void setColour(String colour) {this.colour = colour;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public void eat(String something){System.out.println("吃"+something);}}public class Cat extends Animal {public Cat(){super();}public Cat(int age,String colour){super(age,colour);}public void catchMouse(){System.out.println("抓老鼠");}
}public class Dog extends Animal {public Dog(){super();}public Dog(int age,String colour){super(age,colour);}public void lookHome(){System.out.println("看家");}}public class Person {private String name;private int age;public Person() {}public Person(String name, int age) {this.name = name;this.age = age;}/*** 获取* @return name*/public String getName() {return name;}/*** 设置* @param name*/public void setName(String name) {this.name = name;}/*** 获取* @return age*/public int getAge() {return age;}/*** 设置* @param age*/public void setAge(int age) {this.age = age;}public void KeepPet(Animal animal,String something) {if(animal instanceof Dog d) {System.out.println(this.age + "岁的" + this.name + "养了一只" + animal.getAge() + "的" + animal.getColour() + "的狗抱着" + something + "猛吃。");d.lookHome();}else if(animal instanceof Cat d) {System.out.println(this.age + "岁的" + this.name + "养了一只" + animal.getAge() + "的" + animal.getColour() + "的猫抱着" + something + "猛吃。");d.catchMouse();}}}public class Test {public static void main(String[] args){Animal dog=new Dog(2,"黑");Person man=new Person("张三",30);Person woman=new Person("张四",39);Cat cat=new Cat(1,"白色");man.KeepPet(dog,"鸡腿");woman.KeepPet(cat,"猫粮");}
}
知识点:
对象名 instanceof 类名 d :
返回 true 或 false,若为true 则将该对象赋值为d,d为对应类。可以通过d调用该类的特有方法。
二、编写带有抽象类的标准JavaBean类
package test;public abstract class Animal {private String name;public Animal(){}public Animal(String name){this.name=name;}public String getName() {return name;}public void setAge(String name) {this.name = name;}public abstract void eat();}package test;public class Dog extends Animal{@Overridepublic void eat(){System.out.println("吃骨头");}
}package test;public class Frog extends Animal{@Overridepublic void eat(){System.out.println("吃虫子");}public Frog(){super();}public Frog(String name){super(name);}
}package test;public class Sheep extends Animal{@Overridepublic void eat(){System.out.println("吃草");}
}package test;import com.test1.Teacher;public class Test {public static void main(String[] args){Frog frog=new Frog("青蛙王子");frog.eat(); //吃虫子}
}
三、带有接口和抽象类的标准JavaBean类
package test;public abstract class Person {private int age;private String name;public Person() {}public Person(String name,int age) {this.age = age;this.name = name;}public abstract void work();public int getAge() {return age;}public void setAge(int age) {this.age = age;}public String getName() {return name;}public void setName(String name) {this.name = name;}}package test;public abstract class Coach extends Person{public Coach(){super();}public Coach(String name,int age){super(name,age);}
}package test;public abstract class Player extends Person{public Player(){super();}public Player(String name,int age){super(name,age);}
}package test;public class BasketBallCoach extends Coach{public BasketBallCoach(){super();}public BasketBallCoach(String name,int age){super(name,age);}public void work(){System.out.println("教打篮球");}
}package test;public class BasketBallPlayer extends Player{public BasketBallPlayer(){super();}public BasketBallPlayer(String name,int age){super(name,age);}@Overridepublic void work() {System.out.println("打篮球");}
}package test;public class TennisCoach extends Coach implements SpeakEnglish{public TennisCoach(){super();}public TennisCoach(String name,int age){super(name,age);}public void work(){System.out.println("教打乒乓球");}@Overridepublic void speakEnglish() {System.out.println("教练学英语");}
}package test;public class TennisPlayer extends Player implements SpeakEnglish{public TennisPlayer(){super();}public TennisPlayer(String name,int age){super(name,age);}@Overridepublic void work() {System.out.println("打乒乓球");}@Overridepublic void speakEnglish() {System.out.println("队员学英语");}
}package test;import com.test1.Teacher;import java.sql.SQLOutput;public class Test{public static void main(String[] args){BasketBallCoach coach1=new BasketBallCoach("张三教练",40);TennisCoach coach2=new TennisCoach("李四教练",45);BasketBallPlayer player1=new BasketBallPlayer("易建联",20);TennisPlayer player2=new TennisPlayer("张继科",30);player2.speakEnglish(); //教练学英语coach2.speakEnglish(); //队员学英语coach2.work(); //教打乒乓球coach1.work(); //教打篮球player1.work(); //打篮球player2.work(); //打乒乓球 }
}