本人的很多内容来自于java编程思想
1.构造器和多态:
构造器的调用顺序:基类的构造器总是在派生类的构造过程中被调用,而且按照继承层级逐渐向上链接以此来使每个基类的构造器都能得到调用
这样做的原因是检查对象是否被正确构造,没有构造器,编译器会默认生成一个构造器;
eg:
public class Sandwith extends PortableLunch{
private Bread b = new Bread();
private Cheese c = new Cheese();
private Lettuce l = new Lettuce();
public Sandwith(){
System.out.println("Sandwith");
}
/**
* <b>方法说明:</b>
* <ul>
*
* </ul>
* @param args
*/
public static void main(String[] args) {
new Sandwith();
}
}
class Meal{
Meal(){System.out.println("construct meal");}
}
class Bread{
Bread(){System.out.println("construct bread");}
}
class Cheese{
Cheese(){System.out.println("construct Cheese");}
}
class Lettuce{
Lettuce(){System.out.println("construct Lettuce");}
}
class Lunch extends Meal{
Lunch(){System.out.println("construct Lunch");}
}
class PortableLunch extends Lunch{
PortableLunch(){System.out.println("construct Lunch");}
}
console:
construct meal
construct Lunch
construct Lunch
construct bread
construct Cheese
construct Lettuce
Sandwith
从上面的例子中可以看出调用顺序:
1).调用基类 构造器,从基类的跟,依次向下执行;
2).按申明顺序调用成员的初始化方法;
3).调用派生类构造器的主体;
2.继承与清理:
组合和继承来创建类时,不需要考虑清理问题,子对象通常都会给垃圾回收期进行处理,若确实遇到清理问题,那么必须为新类创建dispose()方法,并且由于继承的缘故,若我们有其他作为垃圾回收一部分的特殊清理动作,就必须在子类中覆盖dsipose(),当覆盖被继承类的dispose()方法时,一定别忘记了调用基类版本的dispose(),否则基类的清理动作就不会发生,下面是证明
public class Frog extends Amphibian{
private Characteristic p = new Characteristic("Croaks");
private Description t = new Description("Eats Bugs");
/**
* <b>构造方法</b>
* <br/>
*/
public Frog() {
System.out.println("Frog()");
}
protected void dispose(){
System.out.println("Frog dispose");
t.dispose();
p.dispose();
super.dispose();
}
/**
* <b>方法说明:</b>
* <ul>
*
* </ul>
* @param args
*/
public static void main(String[] args) {
Frog frog = new Frog();
System.out.println("Bye");
frog.dispose();
}
}
class Characteristic{
private String s;
/**
* <b>构造方法</b>
* <br/>
*/
Characteristic(String s) {
this.s = s;
System.out.println("creating Charecteristic " + s);
}
protected void dispose() {
System.out.println("dispose Charecteristic " + s);
}
}
class Description{
private String s;
/**
* <b>构造方法</b>
* <br/>
*/
Description(String s) {
this.s =s;
System.out.println("creating Description " + s);
}
protected void dispose() {
System.out.println("disposing Description " + s);
}
}
class LivingCreature {
private Characteristic p = new Characteristic("is alive");
private Description t = new Description("Basic Living Creature");
/**
* <b>构造方法</b>
* <br/>
*/
LivingCreature() {
System.out.println("LivingCreature");
}
protected void dispose(){
System.out.println("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");
/**
* <b>构造方法</b>
* <br/>
*/
Animal() {
System.out.println("Animal");
}
protected void dispose() {
p.dispose();
t.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");
/**
* <b>构造方法</b>
* <br/>
*/
Amphibian() {
System.out.println("Amphibian()");
}
protected void dispose() {
System.out.println("Amphibian dispose");
t.dispose();
p.dispose();
super.dispose();
}
}
console:
creating Charecteristic is alive
creating Description Basic Living Creature
LivingCreature
creating Charecteristic has heart
creating Description Animal not Vegetable
Animal
creating Charecteristic can live in water
creating Description Both water and land
Amphibian()
creating Charecteristic Croaks
creating Description Eats Bugs
Frog()
Bye
Frog dispose
disposing Description Eats Bugs
dispose Charecteristic Croaks
Amphibian dispose
disposing Description Both water and land
dispose Charecteristic can live in water
dispose Charecteristic has heart
disposing Description Animal not Vegetable
LivingCreature dispose
disposing Description Basic Living Creature
dispose Charecteristic is alive