zoukankan      html  css  js  c++  java
  • 课后作业06多态与接口

    继承和多态

    动手实验1

    1.      实验要求:


    2.      实验代码

    class Grandparent

    {

     

     

        public Grandparent()

          {

     

                  System.out.println("GrandParent Created.");

          

    }

     

     

        public Grandparent(String string)

           {

     

                  System.out.println("GrandParent Created.String:" + string);

          

     }

     

    }

     

     

     

    class Parent extends Grandparent

    {

     

     

        public Parent()

            {

     

                  //super("Hello.Grandparent.");

     

                  System.out.println("Parent Created");

          

           // super("Hello.Grandparent.");

     

             }

     

    }

     

     

     

    class Child extends Parent

    {

     

     

        public Child()

            {

          

            System.out.println("Child Created");

     

             }

     

    }

     

     

     

    public class TestInherits

    {

     

     

        public static void main(String args[])

            {

     

                  Child c = new Child();

          

      }

     

    }

    3.      实验结果及分析

    如果采用命令行的编译方法,目录下会多出这么几个文件:

    也就是说父类会被单独放在一个
    .class文件里。

    运行结果是:

    显式调用GrandParent的另一个构造函数后父类构造函数就是在第一句输出的。

    当把通过 super 调用基类构造方法的语句放在后面时会报错


    动手实验2

    1.      问题


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

           }

    }

    3.      问题回答

    我推测的运行结果是:

    Parent.printValue(),myValue=100

    Child.printValue(),myValue=200

    Child.printValue(),myValue=200

    Child.printValue(),myValue=200

    Child.printValue(),myValue=201

    因为我觉得当父类的变量引用一个子类的对象的时候,它不能控制这个子类对象的属性变量的改变。

    Java语言可能非本对象的引用不能修改这个对象的变量值。

    4.     
    运行结果分析及总结

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

    如果子类被当作父类使用,则通过子类访问的字段是父类的!

    也就是说子类被当作父类使用时,方法是子类的,属性变量是父类自己的。

  • 相关阅读:
    命令窗口
    文件压缩,文件夹压缩
    objectarx之工具
    Quick Search Articles in My Blog
    Draw graph(network) with nx_pydot in networkx
    How to Share Wired Internet Via Wi-Fi and Vice Versa on Linux
    Deluge: Enables BT download on your Raspberry Pi
    你还在想用 nextCloud 自建NAS? 何不试试P2P Sync?
    screen 命令使用 keep session running after ssh logout
    Zotero: add a history feature for paper viewing
  • 原文地址:https://www.cnblogs.com/w-honey/p/7813324.html
Copyright © 2011-2022 走看看