abstract class Animal{//这是一个抽象类
String name;
String furcolor;
String family;
Animal(){//抽象类中的无参构造函数
String name = null;
String furcolor = null;
this.name = name;
this.furcolor = furcolor;
}
Animal(String name, String furcolor){//抽象类中的有参构造函数
this.name = name;
this.furcolor = furcolor;
}
void setName(String name){
this.name = name;
}
void setFurcolor(String furcolor){
this.furcolor = furcolor;
}
void setFamily(String family){
this.family = family;
}
String getName(){
return name;
}
String eatRecipe(Animal ani){
if (ani.family == "primate" || ani.family == "bear")
return "杂食动物";
else
return "我不知道,可能食草";
}
abstract String excitement();//设置对象兴奋的样子,显然为抽象函数
}
interface Valuable{//接口
String value(Animal ani);
}
interface Protective{//接口
String protect(Animal ani);
}
class Particular extends Animal implements Protective{
String name;
String furcolor;
String family;
Particular(){
String name = null;
String furcolor = null;
super.name = name;
super.furcolor = furcolor;
}
Particular(String name, String furcolor){
super.name = name;
super.furcolor = furcolor;
}
String excitement(){
return "嚎叫";
}
public String protect(Animal ani){
return "在保护区生活";
}
}
class RareAnimal extends Animal implements Protective, Valuable{
String name;
String furcolor;
String family;
RareAnimal(){
String name = null;
String furcolor = null;
super.name = name;
super.furcolor = furcolor;
}
RareAnimal(String name, String furcolor){
super.name = name;
super.furcolor = furcolor;
}
public String excitement(){
return "上蹿下跳,龇牙咧嘴";
}
public String value(Animal ani){
return "无价之宝";
}
public String protect(Animal ani){
return "在保护区或者室内生活";
}
}
public class TestPanda {
public static void main(String[] args) {
//Animal firefox = new Animal(); 抽象类不能实例化对象出来
//全功能测试。这里调用了抽象类的无参构造函数
RareAnimal panda = new RareAnimal();
panda.setName("panda");
panda.setFurcolor("Black and White");
panda.setFamily("bear");
System.out.println
("就" + panda.getName() + "而言," + "它是" + panda.eatRecipe(panda) + "。兴奋起来" + panda.excitement() + "。然而这等" + panda.value(panda) + "只能" + panda.protect(panda)
);
//全功能测试完毕
//测试父类引用勾搭子类对象。这里调用了抽象类的带参数的构造函数
Animal chiken = new Particular("chiken","White Feather");
System.out.println("就" + chiken.getName() + "。兴奋起来" + chiken.excitement() + "。而" + chiken.eatRecipe(chiken) + "主要吃什么。" );
//测试父类引用勾搭子类对象完毕
//强制类型转换
Protective rhino = (RareAnimal)panda;
//rhino作为protect的函数参数时,再一次被转换。因为参数被定义为animal,而非Protective
System.out.println("就犀牛而言,它只能" + rhino.protect((Animal) rhino) + "。");
//强制类型转换完毕
//随手玩儿
Particular elephant = new Particular("elephant","gray");
elephant.setFamily(null);
System.out.println
("就" + elephant.getName() + "而言," + "它吃什么" + elephant.eatRecipe(elephant) + "。兴奋起来" + elephant.excitement() + "。然而这等动物" + "只能" + elephant.protect(elephant) + "。"
);
}
}
在编写过程中,该程序使我明确了很多概念。
- 抽象类中可以定义构造函数,有参数,跟无参数皆可。
- 其实在全功能测试中,我没在派生类重写构造函数,而直接调用的无参构造函数。
- 而为什么我又重写了呢?在第二个父类引用勾搭子类对象时,我用了(String,String)建立对象。但是IDE报错了。
- 上面的细节,我没搞懂。理论上,应该是要重写构造函数。所以统统重写了。
- 抽象类的构造函数能声明为抽象的么?No!!
- 对于强制类型转换有问题的博友,可以在该段带入不同定义的模块实验。
执行结果也很可爱。
就panda而言,它是杂食动物。兴奋起来上蹿下跳,龇牙咧嘴。然而这等无价之宝只能在保护区或者室内生活
就chiken。兴奋起来嚎叫。而我不知道,可能食草主要吃什么。
就犀牛而言,它只能在保护区或者室内生活。
就elephant而言,它吃什么我不知道,可能食草。兴奋起来嚎叫。然而这等动物只能在保护区生活。