zoukankan      html  css  js  c++  java
  • 继承与多态动手动脑

    一、继承条件下构造方法的调用

    测试代码一:

     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 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.");
         }
     }
    复制代码

    测试结果:

    测试代码三:

    复制代码
     1 //编译错误
     2 class Grandparent 
     3 {
     4     public Grandparent()
     5      {
     6             System.out.println("GrandParent Created.");    
     7 }
     8     public Grandparent(String string) 
     9     {
    10             System.out.println("GrandParent Created.String:" + string);
    11     }
    12 }
    13 
    14 class Parent extends Grandparent
    15 {
    16     public Parent()
    17     {
    18             //super("Hello.Grandparent.");
    19             System.out.println("Parent Created");    
    20             super("Hello.Grandparent.");        //报错
    21     }
    22 }    
    复制代码

    测试结果:

    (构造函数调用必须在构造函数第一条语句)

    结论:子类的构造方法在运行之前,必须调用父类的构造方法。

    原因:构造函数的作用为初始化,当被继承的父类未初始化时,无法生成子类对象。

    二、ParentChildTest

    程序代码:

    复制代码
      package ParentChild;
      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); } }
    复制代码

    预测输出结果:

    实际执行结果:

    原因分析:

      当子类与父类具有相同名称的成员时,调用子类对象,默认调用的是子类中的成员,即父类同名成员被隐藏。

      当父类变量引用一个子类对象时,使用父类还是子类成员,由对象自己的“真实”类型所决定

  • 相关阅读:
    语料和文本处理
    seq2seq+torch7聊天机器人bug处理
    unity3d inputfield标签控制台打印object
    多种语言tcp编程
    处理json中的空格
    安卓无法访问Azure服务器和微软API
    Xamarin/Unity3d无法访问Azure服务器或者微软API
    unity3d C# soket客户端接受失败
    unity3d之public变量引发错误
    unity3d,java,c#,python,rospy的socket通信测试
  • 原文地址:https://www.cnblogs.com/xqyfight/p/7865552.html
Copyright © 2011-2022 走看看