动态绑定的概念:
    运行时根据对象的类型进行绑定;以便在运行时能判断对象的类型,从而调用正确的方法。

    final在大多数情况下对提高性能影响不大:
    final可以有效的关闭“动态绑定”告诉编译器不需要对其进行动态绑定

    1>熟悉对象工厂的使用,可以根据参数来生成不同的对象,参考以下代码。

    1. public class RandomShapes {
    2. private Random rand = new Random(47);
    3. public Shape get() {
    4. switch(rand.nextInt(3)) {
    5. default:
    6. case 0: return new Circle();
    7. case 1: return new Square();
    8. case 2: return new Triangle();
    9. }
    10. }
    11. public Shape[] array(int sz) {
    12. Shape[] shapes = new Shape[sz];
    13. // Fill up the array with shapes:
    14. for(int i = 0; i < shapes.length; i++)
    15. shapes[i] = get();
    16. return shapes;
    17. }
    18. }

    2>对于多态的理解:
    1,编译时,编译器不需要额外的信息来应对接下来的调用;对draw()正确的调用是在运行时通过动态绑定来产生的正确行为。
    2,多态是一项让程序员“将改变的事物与未变的事物分离开来的一项重要技术”
    3,多态只存在于普通方法中
    当Sub对象转型为Super引用时,任何域访问操作都将由编译器解析,因此不存在多态。

    1. class Super{
    2. public int i = 0;
    3. public int getI() {
    4. return i;
    5. }
    6. }
    7. class Sub extends Super{
    8. public int i = 1;
    9. @Override
    10. public int getI() {
    11. //System.out.println("execute......");
    12. return i;
    13. }
    14. //i 变量是否会被覆盖
    15. public int getSuperI(){
    16. return getI();
    17. }
    18. }
    19. public class FieldAccess {
    20. public static void main(String[] args) {
    21. // 对于域来说 值在编译器都已经定了 方法呢是在运行时候动态绑定的
    22. Super sup = new Sub();
    23. //System.out.println("sup.i = "+sup.i+" "+"sup.getI ="+sup.getI());
    24. Sub sub = new Sub();
    25. System.out.println("sub.i = "+sub.i+" "+"sub.getI ="+sub.getI());
    26. }
    27. }

    8.2 转机 - 图1

    1. 编码建议
    2. 因为非private才可以被覆盖,建议在导出类中对于基类的private方法采用不同的命名。