2020.10.21

一、今日学习内容

     动手动脑

   1.

class Mammal{}
class Dog extends Mammal {}
class Cat extends Mammal{}

public class TestCast
{
    public static void main(String args[])
    {
        Mammal m;
        Dog d=new Dog();
        Cat c=new Cat();
        m=d;
        //d=m;
        d=(Dog)m;
        //d=c;
        c=(Cat)m;

    }
}

d=m错误:不能从Mammal类转换到Dog类;

d=c错误:不能从Cat类转换到Dog类;

c=(Cat)m正确:

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

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

   

原因:

前两行正常输出,父类对象调用父类的方法,子类对象调用子类的方法;

第三行,当子类与父类拥有一样的方法,并且让一个父类变量引用一个子类对象时,到底调用哪个方法,由对象自己的“真实”类型所决定;

第四行,当parent=child;仅仅是将parent中有的方法用child的方法代替,所以parent.myValue++;而输出的是child的printValue(),而printValue()方法中输出的是child.myValue,所以输出的是child.myValue;

第五行,强制类型转换,++作用在child的myValue,所以输出的也是child的myValue,而不是parent的myValue;

结论:

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

      3.在子类中,若要调用父类中被覆盖的方法,可以使用super关键字。

class test{
    void play() {
        System.out.println("我是父类test");
    }
}

class test11 extends test{
    void play() {
        super.play();
    }
}
public class test1
{
    public static void main(String[] args) {
        test11 t=new test11();
        t.play();
    }

}

  

 4.

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

   

  5.

package second;

class Parent

{

    public int value=100;

public void Introduce()
    {

            System.out.println("I'm father");

        }


}

class Son extends Parent
{

    public int value=101;

         public void Introduce()
    {

            System.out.println("I'm son");

}

}


class Daughter extends Parent
{

    public int value=102;
    public void Introduce()
    {

            System.out.println("I'm daughter");

}

}

public class TestPolymorphism
{


    public static void main(String args[])
    {

        Parent p=new Parent();

        p.Introduce();

        System.out.println(p.value);

        p=new Son();

        p.Introduce();

        System.out.println(p.value);

        p=new Daughter();

        p.Introduce();

        System.out.println(p.value);


    }


}

  

 二、遇到的问题

   没有遇到问题

三、明日计划

   明天完成Javaweb相关方面的配置,上次配置没有配置好

原文地址:https://www.cnblogs.com/wmdww/p/14149018.html