动手动脑

1.可以使用instanceof运算符判断一个对象是否可以转换为指定的类型:
Object obj="Hello";
if(obj instanceof String)
 System.out.println("obj对象可以被转换为字符串");

验证:

public class TestInstanceof
{
    public static void main(String[] args) 
    {
        //声明hello时使用Object类,则hello的编译类型是Object,Object是所有类的父类
        //但hello变量的实际类型是String
        Object hello = "Hello";
        //String是Object类的子类,所以返回true。
        System.out.println("字符串是否是Object类的实例:" + (hello instanceof Object));
        //返回true。
        System.out.println("字符串是否是String类的实例:" + (hello instanceof String));
        //返回false。
        System.out.println("字符串是否是Math类的实例:" + (hello instanceof Math));
        //String实现了Comparable接口,所以返回true。
        System.out.println("字符串是否是Comparable接口的实例:" + (hello instanceof Comparable));
        String a = "Hello";
        //String类既不是Math类,也不是Math类的父类,所以下面代码编译无法通过
        //System.out.println("字符串是否是Math类的实例:" + (a instanceof Math));
    }
}

2.请看以下“变态”的类

public class ParentChildTest {
    public static void main(String[] args) {
        Parent parent=new Parent();
        parent.printValue();
        Child child=new Child();
        child.printValue();
        
        parent=child;
        parent.printValue();//调用child的函数
        
        parent.myValue++;//parent的值增加,child的不变
        parent.printValue();//调用child的函数
        
        ((Child)parent).myValue++;//child的值增加
        parent.printValue();//调用child的函数
        
    }
}

class Parent{
    public int myValue=100;
    public void printValue() {
        System.out.println("Parent.printValue(),myValue="+myValue);
    }
}
class Child extends Parent{
    public int myValue=200;
    public void printValue() {
        System.out.println("Child.printValue(),myValue="+myValue);
    }
}

子类和父类定义了一模一样的字段和方法

结果:

 

 结论:

当子类与父类拥有一样的方法,并且让一个父类变量引用一个子类对象时,到底调用哪个方法,由对象自己的“真实”类型所决定,这就是说:对象是子类型的,它就调用子类型的方法,是父类型的,它就调用父类型的方法。如果子类与父类有相同的字段,则子类中的字段会代替或隐藏父类的字段,子类方法中访问的是子类中的字段(而不是父类中的字段)。如果子类方法确实想访问父类中被隐藏的同名字段,可以用super关键字来访问它。
如果子类被当作父类使用,则通过子类访问的字段是父类的!牢记:在实际开发中,要避免在子类中定义与父类同名 的字段。不要自找麻烦

 

3.滞后绑定

当程序运行并且/使用/动态绑定调用方法时,虚拟机必须调用同/所指向的对象的实际类 型/相匹配的方法版本, 而这些事情以往要在编译时候就要确定下来, 所以动态绑定又叫滞后绑定。 它能使程序 变得可扩展而无需重编译已存代码。 

通过在编程中应用多态,可以使我们的代码具有更强的适用性。当需求变化时,多态特性可以帮助我们将需要改动的地方减少到最低限度。
多态编程有两种主要形式:
(1)继承多态

(2)接口多态

当你要修改程序并扩充系统时,你需要修改的地方较少,对其它部分代码的影响较小!千万不要小看这两个“较”字!程序规模越大,其优势就越突出。

posted on 2016-11-18 21:56  藏藏藏  阅读(178)  评论(0编辑  收藏  举报

导航