黑马程序员技术交流社区
标题:
已知动物类(姓名,年龄,吃饭,睡觉),它有两个子类...
[打印本页]
作者:
chenshaohua
时间:
2016-5-30 23:42
标题:
已知动物类(姓名,年龄,吃饭,睡觉),它有两个子类...
class Demo1_Abstract {
public static void main(String[] args) {
JumpCat jc = new JumpCat();
jc.eat();
jc.sleep();
jc.jump();
JumpDog jd = new JumpDog();
jd.eat();
jd.sleep();
jd.jump();
}
}
abstract class Animal {
private String name;
private int age;
public Animal() {
}
public Animal(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public abstract void eat();
public void sleep() {
System.out.println("睡觉");
}
}
class Cat extends Animal{
public Cat() {
}
public Cat(String name, int age) {
super(name, age);
}
public void eat() {
System.out.println("猫吃鱼");
}
}
class Dog extends Animal {
public Dog() {
}
public Dog(String name, int age) {
super(name, age);
}
public void eat() {
System.out.println("狗吃肉");
}
}
interface Jump {
public abstract void jump();
}
class JumpCat extends Cat implements Jump {
public JumpCat() {
}
public JumpCat(String name, int age) {
super(name, age);
}
public void jump() {
System.out.println("猫跳高");
}
}
class JumpDog extends Dog implements Jump {
public JumpDog() {
}
public JumpDog(String name, int age) {
super(name, age);
}
public void jump() {
System.out.println("狗跳高");
}
}
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2