• 热门专题

ThinkingInJava 9 继承与清理

作者:  发布日期:2015-04-17 00:06:25
Tag标签:ThinkingInJava  9  继承与清理  
  • /**
    * 书本:《Thinking In Java》
    * 功能:继承与清理
    * 文件:Frog.java
    * 时间:2015年4月1日19:38:22
    * 作者:cutter_point
    */
    package Lesson8Polymorphism;
    
    import static net.mindview.util.Print.*;
    
    class Characteristic
    {
    	private String s;
    	
    	Characteristic(String s) 
    	{
    		this.s = s;
    		print("Creating Characteristic " + s);
    	}
    	
    	protected void dispose()
    	{
    		print("disposing Characteristic " + s);
    	}
    }
    
    
    class Description
    {
    	private String s;
    	Description(String s)
    	{
    		this.s = s;
    		print("Creating Description " + s);
    	}
    	
    	protected void dispose()
    	{
    		print("disposing Description " + s);
    	}
    }
    
    //类的组合
    class LivingCreature
    {
    	private Characteristic p = new Characteristic("is alive");
    	private Description t = new Description("Basic Living Creature");
    	
    	LivingCreature()
    	{
    		print("LivingCreature()+构造函数");
    		
    	}
    	
    	protected void dispose()
    	{
    		print("LivingCreature dispose");
    		t.dispose();
    		p.dispose();
    	}
    }
    
    //类的继承
    class Animal extends LivingCreature
    {
    	private Characteristic p = new Characteristic("has heart");
    	private Description t =new Description("Animal not Vegetable");
    	
    	Animal() { print("Animal()"); }
    	protected void dispose()
    	{
    		print("Animal dispose");
    		t.dispose();
    		p.dispose();
    		super.dispose();
    	}	
    }
    
    
    class Amphibian extends Animal
    {
    	private Characteristic p = new Characteristic("Can live in water");
    	private Description t = new Description("Both water and land");
    	
    	Amphibian() 
    	{
    		print("Amphibian()");
    	}
    	protected void dispose()
    	{
    		print("Amphibian dispose");
    		t.dispose();
    		p.dispose();
    		super.dispose();
    	}	
    }
    
    public class Frog extends Amphibian
    {
    	private Characteristic p = new Characteristic("Croaks");
    	private Description t = new Description("Eats bugs");
    	
    	public Frog() { print("Frog()"); }
    	protected void dispose()
    	{
    		print("Frog dispose");
    		t.dispose();
    		p.dispose();
    		super.dispose();
    	}	
    	
    	public static void main(String args[])
    	{
    		Frog frog = new Frog();
    		print("Bye bye!");
    		frog.dispose();
    		print("all is over");
    	}
    }
    
    
    


    * 终结:
    * 1、首先就是调用父类的构造函数
    * 2、到根父类之后,首先实例化数据成员(一般是类的组合的实现)
    * 3、数据成员实例化之后,再是构造函数里面的类容

    输出:

    Creating Characteristic is alive obj1
    Creating Description Basic Living Creature obj1
    LivingCreature()+构造函数 obj1
    Creating Characteristic has heart obj1
    Creating Description Animal not Vegetable obj1
    Animal() obj1
    Creating Characteristic Can live in water obj1
    Creating Description Both water and land obj1
    Amphibian() obj1
    Creating Characteristic Croaks obj1
    Creating Description Eats bugs obj1
    Frog() obj1
    Bye bye! obj1
    Frog dispose obj1
    disposing Description Eats bugs obj1
    disposing Characteristic Croaks obj1
    Amphibian dispose obj1
    disposing Description Both water and land obj1
    disposing Characteristic Can live in water obj1
    Animal dispose obj1
    disposing Description Animal not Vegetable obj1
    disposing Characteristic has heart obj1
    LivingCreature dispose obj1
    disposing Description Basic Living Creature obj1
    disposing Characteristic is alive obj1
    all is over obj1


About IT165 - 广告服务 - 隐私声明 - 版权申明 - 免责条款 - 网站地图 - 网友投稿 - 联系方式
本站内容来自于互联网,仅供用于网络技术学习,学习中请遵循相关法律法规