zoukankan      html  css  js  c++  java
  • 07-继承与接口动手动脑及课后实验性问题总结

    一.运行TestInherits.java示例,观察输出,注意总结父类与子类之间构造方法的调用关系修改Parent构造方法的代码,显式调用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");//必须放在第一句
        }
    }
    class Child extends Parent{
        public Child(){
            System.out.println("Child Created");
        }
    }
    public class TestInherits {
        
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            Child c=new Child();
        }
    
    }

     结论:通过super调用基类构造方法,必须是子类构造方法中的第一个语句。

    二.动手动脑:请自行编写代码测试以下特性:在子类中,若要调用父类中被覆盖的方法,可以使用super关键字。

    package Work;
    
     
    
    class A{
    
    public A(){
    
    System.out.println("Class A.");
    
    }
    
    public A(String string){
    
    System.out.println("Class A."+string);
    
    }}
    
    class B extends A{
    
    public B(){
    
    super("string ");
    
    System.out.print("Class B.");
    
    }
    
    }
    
    public class Test01 extends B {
    
    public static void main(String[] args) {
    
             B b=new B();
    
    }
    
    }

  • 相关阅读:
    iOS UI调试神器,插件injection for Xcode使用方法
    iOS 开发笔记-Objective-C之KVC、KVO
    iOS 测试企业应用的分发
    iOS 阅读唐巧博客心得
    iOS 添加启动图片
    Xcode 常用命令
    iOS 开发笔记
    iOS 开发常用链接总结
    iOS
    iOS UI基础
  • 原文地址:https://www.cnblogs.com/zhangjiabei/p/6051374.html
Copyright © 2011-2022 走看看