第七章: Polymorphism
来源:互联网 发布:vb可不可以治耳螨 编辑:程序博客网 时间:2024/04/25 02:00
第七章: Polymorphism
1、 Upcasting revisited--Taking an object reference and treating it as a reference to its base type.(通过绑定来实现多态的方法调用)
Method-call binding方法调用的绑定,分前绑定和后绑定--early binding(When binding is performed before the program is run)、Late binding also called dynamic binding or run-time binding(means that the binding occurs at run time, based on the type of object)
定义抽象类:abstract class Instrument {private int i; // Storage allocated for each
public abstract void play(Note n);
public String what() {
return "Instrument";
}
public abstract void adjust();
}
2、Constructors and polymorphism
2.1)The base-class constructor is called. This step is repeated recursively such that the root of the hierarchy is constructed first, followed by the next-derived class, etc., until the most-derived class is reached.
2.2)Member initializers are called in the order of declaration.
2.3)The body of the derived-class constructor is called
a good guideline for constructors is, “Do as little as possible to set the object into a good state, and if you can possibly avoid it, don’t call any methods.” The only safe methods to call inside a constructor are those that are final in the base class. (This also applies to private methods, which are automatically final.)理解如下示例:
import com.bruceeckel.simpletest.*;
abstract class Glyph {
abstract void draw();
Glyph() {
System.out.println("Glyph() before draw()");
draw(); //call a dynamically-bound method(多态性:所有的东西都是继承下来的)
System.out.println("Glyph() after draw()");
}
}
class RoundGlyph extends Glyph {
private int radius = 1;
RoundGlyph(int r) {
radius = r;
System.out.println(
"RoundGlyph.RoundGlyph(), radius = " + radius);
}
void draw() { //基类构造函数在调用的时侯,radius还没有初始化
System.out.println(
"RoundGlyph.draw(), radius = " + radius);
}
}
public class PolyConstructors {
private static Test monitor = new Test();
public static void main(String[] args) {
new RoundGlyph(5);
monitor.expect(new String[] {
"Glyph() before draw()",
"RoundGlyph.draw(), radius = 0",
"Glyph() after draw()",
"RoundGlyph.RoundGlyph(), radius = 5"
});
}
} ///:~
- 第七章: Polymorphism
- Thinking in java (第三版)第七章 多态性(Polymorphism)
- 【学习笔记】Thinking in java (第三版)第七章 多态性(Polymorphism)
- Polymorphism
- Polymorphism
- polymorphism
- Polymorphism
- Polymorphism
- Polymorphism
- Polymorphism
- Polymorphism
- Polymorphism
- polymorphism
- polymorphism
- 《Java编程思想》(第二版)第07章:多态(polymorphism)
- Think in java学习笔记-第7章 Polymorphism
- polymorphism多态
- 多态(Polymorphism)
- [分享]领会
- 【^_^】飞越疯人院
- 【^_^】男人力挽狂澜的一句话
- 一个在时间选择上很有用的代码
- 删除MSSQL危险存储过程的代码
- 第七章: Polymorphism
- 删除sqlserver中的sa帐户
- SQL异地备份
- SQL解密破解字节不受限制,适用于SQLSERVER2000存储过程,函数,视图,触发器
- 中文MSDN上一篇关于第三方ORM的描述文章,其ORM解决方案用起来居然跟Courser十分相似
- 多媒体短消息业务及其实现
- 【爱情】男女关系大实话 婚姻篇
- Java/JSP中文乱码问题解决心得
- 判断一个查询的结果是否为空