Initial commit
This commit is contained in:
44
实验报告模板/实验报告4/BackAnimal.java
Normal file
44
实验报告模板/实验报告4/BackAnimal.java
Normal file
@@ -0,0 +1,44 @@
|
||||
package ch04;
|
||||
|
||||
abstract class Animal{
|
||||
public abstract void eat(); //只有方法名,而没有方法体
|
||||
}
|
||||
class Dog extends Animal{
|
||||
private String name;
|
||||
public void setName(String name){
|
||||
this.name=name;
|
||||
}
|
||||
public String getName(){
|
||||
return name;
|
||||
}
|
||||
public void eat(){
|
||||
System.out.println("小狗啃骨头啦");
|
||||
}
|
||||
}
|
||||
class Bird extends Animal{
|
||||
public void fly( ){
|
||||
System.out.println("小鸟飞呀飞呀");
|
||||
}
|
||||
public void eat( ){
|
||||
System.out.println("小鸟吃虫子啦");
|
||||
}
|
||||
}
|
||||
public class BackAnimal{
|
||||
public static void main( String args[ ] ){
|
||||
Animal a1=new Animal(); //1.该行代码可不可以执行?为什么?
|
||||
Animal an = new Dog( );
|
||||
an.eat( ); //2.该行代码输出什么?
|
||||
an=new Bird();
|
||||
an.eat(); //3.该行代码输出什么?
|
||||
Animal x1=new Dog();
|
||||
Animal x2=new Bird();
|
||||
print(x1); //4.以下2行代码输出什么?为什么?
|
||||
print(x2);
|
||||
|
||||
}
|
||||
public static void print(Animal a){
|
||||
a.eat();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user