一个程序尽可能多的实现Java面向对象编程的概念

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) + "。"
	);
	}

}

在编写过程中,该程序使我明确了很多概念。

  1. 抽象类中可以定义构造函数,有参数,跟无参数皆可。
  2. 其实在全功能测试中,我没在派生类重写构造函数,而直接调用的无参构造函数。
  3. 而为什么我又重写了呢?在第二个父类引用勾搭子类对象时,我用了(String,String)建立对象。但是IDE报错了。
  4. 上面的细节,我没搞懂。理论上,应该是要重写构造函数。所以统统重写了。
  5. 抽象类的构造函数能声明为抽象的么?No!!
  6. 对于强制类型转换有问题的博友,可以在该段带入不同定义的模块实验。

执行结果也很可爱。

就panda而言,它是杂食动物。兴奋起来上蹿下跳,龇牙咧嘴。然而这等无价之宝只能在保护区或者室内生活
就chiken。兴奋起来嚎叫。而我不知道,可能食草主要吃什么。
就犀牛而言,它只能在保护区或者室内生活。
就elephant而言,它吃什么我不知道,可能食草。兴奋起来嚎叫。然而这等动物只能在保护区生活。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值