Main方法
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
penguin a = new penguin();
Dog b = new Dog();
zhur c = new zhur();
c.wei(a);
c.wei(b);
Cw q= c.linyan();
if(q instanceof Dog){
Dog dog=new Dog();
dog.Cf();
dog.Dz();
}
if(q instanceof penguin){
penguin qe =new penguin();
qe.Cf();
qe.Dz();
}
}
}
多态类
public class zhur {
Scanner sm = new Scanner(System.in);
public Cw wei(Cw cw) {
if ("1".equals(cw)) {
return new Dog();
} else if ("2".equals(cw)) {
return new penguin();
}
return cw;
}
public Cw linyan() {
System.out.println("请输入编号(1.企鹅/2.狗)");
int sr = sm.nextInt();
if (sr == 1) {
return new penguin();
} else if (sr == 2) {
return new Dog();
} else {
System.out.println("输入有误");
}
return null;
}
}
父类/
public abstract class Cw {
public abstract void Cf();
public abstract void Dz();
}
狗类
public class Dog extends Cw {
public void Cf() {
System.out.println("狗在吃饭");
}
public void Dz() {
System.out.println("狗在和主人玩扔飞盘!!!健康值减少10,亲密度加5");
}
}
企鹅类
public class penguin extends Cw {
public void Cf() {
System.out.println("企鹅在吃饭");
}
public void Dz() {
System.out.println("主人 在看企鹅仔TM游泳!~~~ 健康度减少10,亲密度加5");
}
}