黑马程序员技术交流社区
标题:
代码分享
[打印本页]
作者:
wuchangfeng
时间:
2015-7-19 09:34
标题:
代码分享
class Demo {
public static void main(String[] args) {
Dog dog = new Dog();
AnimalTools.print(dog);
/*
dog.eat();
dog.drink();
dog.la();
dog.sa();
dog.sleep();
*/
Cat cat = new Cat();
AnimalTools.print(cat);
/*
cat.eat();
cat.drink();
cat.la();
cat.sa();
cat.sleep();
*/
Pig pig = new Pig();
AnimalTools.print(pig);
/*
pig.eat();
pig.drink();
pig.la();
pig.sa();
pig.sleep();
*/
}
}
class Dog extends Animal {
public void eat() {
System.out.println("吃骨头");
}
public void drink() {
System.out.println("狗喝");
}
public void la() {
System.out.println("狗拉");
}
public void sa() {
System.out.println("狗撒");
}
public void sleep() {
System.out.println("狗睡");
}
}
class Cat extends Animal {
public void eat() {
System.out.println("吃鱼");
}
public void drink() {
System.out.println("猫喝");
}
public void la() {
System.out.println("猫拉");
}
public void sa() {
System.out.println("猫撒");
}
public void sleep() {
System.out.println("猫睡");
}
}
class Pig extends Animal {
public void eat() {
System.out.println("吃肉");
}
public void drink() {
System.out.println("猪喝");
}
public void la() {
System.out.println("猪拉");
}
public void sa() {
System.out.println("猪撒");
}
public void sleep() {
System.out.println("猪睡");
}
}
class Animal {
String name;
int age;
public void eat() {
System.out.println("吃");
}
public void drink() {
System.out.println("喝");
}
public void la() {
System.out.println("拉");
}
public void sa() {
System.out.println("撒");
}
public void sleep() {
System.out.println("睡");
}
}
class AnimalTools {
public static void print(Animal animal) {//Animal aniaml = new Dog();
animal.eat(); //Animal aniaml = new Cat();
animal.drink(); //Animal aniaml = new Pig();
animal.la();
animal.sa();
animal.sleep();
}
}
作者:
白面馅包子
时间:
2015-7-19 09:37
咳咳 上海 0705 皓哥 教的 一股接地气的气息扑面而来 :lol
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2