06-继承与多态——课后动手动脑

1.怎样判断对象是否可以转换?

答:可以使用instanceof运算符判断一个对象是否可以转换为指定的类型:实例:

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.现有三个类

class Mammal{}

class Dog extends Mammal{}

class Cat extends Mammal{}

先对其分别进行初始化:

Mammal m = null;

Dog d = new Dog();

Cat c = new Cat();

下列语句哪一个将引起编译错误?为什么?哪一个会引起运行时错误?为什么?

m = d;

d = m;

d = (Dog)m;

d = c;

c = (Cat)m;

事先判断结果:

出现编译错误:第二行,原因:不能将父类变量赋值给子类变量。

                  第四行,原因:继承于同一个父类的两个平行子类不能互相赋值。

出现运行错误:第五行,原因:在第一行中,m变量已经指向Dog对象,如果再执行c = (Cat)m,就是让c指向Dog对象,而Cat和Dog之间不存在继承关系,无法让c指向Dog对象。

程序编译截图:

测试截图:

结论:

对象之间互相赋值时,子类对象可以对父类对象赋值,父类对象不能对子类变量赋值,继承与同一基类的平行子类之间不能相互赋值。

3.运行以下测试代码:

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();
        
        parent.myValue++;
        parent.printValue();
        
        ((Child)parent).myValue++;
        parent.printValue();
        
    }
}

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);
    }
}

1)上边的程序的运行结果是什么?

运行结果如图:

总结:

子类可以赋值给父类,代表父类变量引用子类的对象。子类可以覆盖父类,覆盖后引用的是子类的方法,如果需要引用父类的方法可以用super函数。当子类与父类拥有一样的方法,并且让一个父类变量引用一个子类对象时,到底调用哪个方法,由对象自己的“真实”类型所决定,这就是说:对象是子类型的,它就调用子类型的方法,是父类型的,它就调用父类型的方法。这个特性实际上就是面向对象“多态”特性的具体表现。如果子类与父类有相同的字段,子类方法中访问的是子类中的字段(而不是父类中的字段)。如果子类方法确实想访问父类中被隐藏的同名字段,可以用super关键字来访问它。如果子类被当做父类使用,则通过子类访问的字段是父类的。

原文地址:https://www.cnblogs.com/guo-xu/p/7802536.html