黑马程序员技术交流社区
标题:
抽象类练习
[打印本页]
作者:
jekyll
时间:
2015-9-27 00:33
标题:
抽象类练习
class Noname1 {
public static void main(String[] args) {
Cat cat = new Cat("罗小黑",250);
cat.job();
cat.eat();
cat.jump();
Dog dog = new Dog("菜包狗",100);
dog.job();
dog.eat();
JumpDog jdog = new JumpDog("高斯",400);
jdog.jump();
}
}
interface Jump {
public void jump();
}
abstract class Animal {
String name;
int age;
Animal() {};
Animal(String name,int age) {
this.name = name;
this.age = age;
}
public void setName() {
this.name = name;
}
public String getName() {
return name;
}
public void setAge() {
this.age = age;
}
public int getAge() {
return age;
}
public String getname(String name) {
return name;
}
abstract void eat();
abstract void job();
}
class Cat extends Animal implements Jump {
Cat(String name,int age) {
super(name,age);
}
void job() {
System.out.println(this.name+"捉老鼠");
}
void eat() {
System.out.println(this.name+"吃鱼");
}
public void jump() {
System.out.println(this.name+"跳高");
}
}
class Dog extends Animal {
Dog(String name,int age) {
super(name,age);
}
void eat() {
System.out.println(this.name+"吃大白兔");
}
void job() {
System.out.println(this.name+"看门");
}
}
class JumpDog extends Dog implements Jump {
JumpDog(String name,int age) {
super(name,age);
}
public void jump() {
System.out.println(this.name+"跳高");
}
}
复制代码
作者:
Synaric
时间:
2015-9-27 07:50
写的挺不错的
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2