upcast例: public class Test { public static void main(String[] args) { Cup aCup = new BrokenCup(); aCup.addWater(10); // method binding } } class Cup { public void addWater(int w) { this.water = this.water + w; } public void drinkWater(int w) { this.w…
excerpted from Type conversions K&R Section 2.7 p59 对type conversion 的解释: The precise meaning of a cast is as if the expression were assigned to a variable of the specified type, which is then used in place of the whole construction. (类型名) 表达式 将被转换的表…
Enumerated Types Basic enum features When you create an enum, an associated class is produced for you by the compiler. This class is automatically inherited from java.lang.Enum. The ordinal( ) method produces an int indicating the declaration order o…
Reusing Classes 有两种常用方式实现类的重用,组件(在新类中创建存在类的对象)和继承. Composition syntax Every non-primitive object has a toString() method, and it’s called in special situations when the compiler wants a String but it has an object. Inheritance syntax You’re always do…