zoukankan      html  css  js  c++  java
  • java中07的动手动脑

    动手实验:

    代码:

    package Shi;

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

        }

    }

    截图:

    结论:继承先调用父类的构造函数,在调用子类的构造函数;通过super调用基类构造方法,必须是子类构造方法中的第一个语句。

    动手动脑:

    代码: 

    package Shi;

    class fulei{

             public void show(){

                       System.out.println("这是父类中的函数");

             }

    }

    class zilei extends fulei{

             public void show1(){

                       super.show();

                       System.out.println("这是子类中的函数");

             }

    }

    public class Shi {

     

             public static void main(String[] args) {

                       zilei x=new zilei();

                       x.show1();

             }

     

    }

    截图:

           

    思路:这个题目主要考察super调用基类方法的应用,应注意的是super一定要写在子类函数定义的第一句。

  • 相关阅读:
    python模块之random模块
    python模块之os模块
    python模块之collections模块
    python模块之re模块
    python基础十五之递归函数
    python基础十四之匿名函数
    python基础十三之内置函数
    leetcode 108 和leetcode 109 II
    leetcode 108 和leetcode 109
    对于final修饰的类型运算时的表现
  • 原文地址:https://www.cnblogs.com/zhaoziming/p/6054856.html
Copyright © 2011-2022 走看看