黑马程序员技术交流社区
标题:
抽象类练习:铲屎的与傲娇猫
[打印本页]
作者:
石头stone
时间:
2016-5-22 17:55
标题:
抽象类练习:铲屎的与傲娇猫
class Test2_Animal {
public static void main(String[] args) {
JumpCat jc = new JumpCat("萌萌",3);
System.out.println("喵的名字是:" +jc.getName() + ",喵已经" + jc.getAge()+"岁了.");
jc.eat();
jc.sleep();
jc.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 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();
}
class Cat extends Animal {
public Cat() {}
public Cat(String name,int age) {
super(name,age);
}
public void eat() {
System.out.println("铲屎的,喵要吃鱼!!");
}
public void sleep() {
System.out.println("铲屎的,喵要睡觉!!");
}
}
interface Jumping {
public abstract void jump();
}
class JumpCat extends Cat implements Jumping {
public JumpCat() {}
public JumpCat(String name,int age) {
super(name,age);
}
public void jump() {
System.out.println("喵要蹦过去!!");
}
}
复制代码
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2