[Java] 纯文本查看 复制代码 package com.day9;
/*
代码中使用多态作为参数但是调用不出子类的特有方法
*/
public class Abstract_Dongwu {
public static void main(String[] args) {
// TODO Auto-generated method stub
Mao m = new Mao("白猫",8);
System.out.println("这个是一只:" + m.getName() + "它的年龄是:" + m.getAge());
m.eat();
m.sleep();
JumpMao jm = new JumpMao("黑猫",3);
System.out.println("这个是一只:" + jm.getName() + "它的年龄是:" + jm.getAge());
jm.eat();
jm.sleep();
jm.jump();
Gou g = new Gou("白狗",10);
System.out.println("这个是一只:" + g.getName() + "它的年龄是:" + g.getAge());
g.eat();
g.sleep();
JumpGou jg = new JumpGou("黑狗",5);
System.out.println("这个是一只:" + jg.getName() + "它的年龄是:" + jg.getAge());
jg.eat();
jg.sleep();
jg.jump();
method(new Mao("张三",23));
method(new Gou("李四",24));
//method(new Dongwu("王五",25)); //动物类是抽象类,不能创建对象
method(new JumpMao("赵六",26));
method(new JumpGou("刘七",27));
Dongwu d = new JumpMao("ssss",2);
JumpMao x = (JumpMao) d;
System.out.println("这个是一只:" + x.getName() + "它的年龄是:" + x.getAge());
x.jump();
}
public static void method(Dongwu d) {
if(d instanceof Mao) {
Mao m = (Mao) d;
System.out.println("这个是一只:" + m.getName() + "它的年龄是:" + m.getAge());
m.eat();
m.sleep();
}else if(d instanceof Gou) {
Gou g = (Gou) d;
System.out.println("这个是一只:" + g.getName() + "它的年龄是:" + g.getAge());
g.eat();
g.sleep();
}else if(d instanceof JumpMao) {
JumpMao jm = (JumpMao) d;
System.out.println("这个是一只:" + jm.getName() + "它的年龄是:" + jm.getAge());
jm.eat();
jm.sleep();
jm.jump(); //为什么这里的方法调用不出来
}else if(d instanceof JumpGou) {
JumpGou jg = (JumpGou) d;
System.out.println("这个是一只:" + jg.getName() + "它的年龄是:" + jg.getAge());
jg.eat();
jg.sleep();
jg.jump();
}else {
System.out.println("动物类的方法是抽象的");
}
}
}
/*
案例演示:
动物类:姓名,年龄,吃饭,睡觉
猫和狗
动物培训借口:跳高
*/
abstract class Dongwu { //抽象类:动物
private String name; //名字
private int age; //年龄
public Dongwu() { //空参
}
public Dongwu(String name,int age) { //有参
this.name = name;
this.age = age;
}
public void setName(String name) { //设置名字
this.name = name;
}
public String getName() { //获取名字
return name;
}
public void setAge(int age) { //设置年龄
this.age = age;
}
public int getAge() { //获取年龄
return age;
}
public abstract void eat(); //抽象方法吃
public abstract void sleep(); //抽象方法睡
}
interface Jump { //接口跳高
public abstract void jump(); //抽象方法跳高
}
class Mao extends Dongwu { //猫类继承动物类
public Mao() { //空参
}
public Mao(String name,int age) { //有参
super(name,age);
}
public void eat() { //具体化猫类的方法吃
System.out.println("猫吃鱼");
}
public void sleep() { //具体化猫类的方法睡
System.out.println("侧着睡");
}
}
class JumpMao extends Mao implements Jump { //跳高猫类继承猫类并且实现接口跳高
public JumpMao() { //空参
}
public JumpMao(String name,int age) { //有参
super(name,age);
}
public void jump() { //跳高猫类实现接口跳高
System.out.println("猫跳高");
}
}
class Gou extends Dongwu { //狗类继承动物
public Gou() { //空参
}
public Gou(String name,int age) { //有参
super(name,age);
}
public void eat() { //具体化狗类的方法吃
System.out.println("狗吃肉");
}
public void sleep() { //具体化狗类的方法睡
System.out.println("趴着睡");
}
}
class JumpGou extends Gou implements Jump { //跳高狗继承狗类并且实现接口跳高
public JumpGou() { //空参
}
public JumpGou(String name,int age) { //有参
super(name,age);
}
public void jump() { //跳高狗类实现接口跳高
System.out.println("狗跳高");
}
}
|