码迷,mamicode.com
首页 > 编程语言 > 详细

【ThinkingInJava】9、继承与清理

时间:2015-04-16 15:45:38      阅读:136      评论:0      收藏:0      [点我收藏+]

标签:java   java编程思想   继承   类的组合   

/**
* 书本:《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





【ThinkingInJava】9、继承与清理

标签:java   java编程思想   继承   类的组合   

原文地址:http://blog.csdn.net/cutter_point/article/details/45075733

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!