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一定要写在子类函数定义的第一句。

  • 相关阅读:
    php 正则获取字符串中的汉字(去除字符串中除汉字外的所有字符)
    发送短信倒计时效果实现
    《JavaScript模式》第2章 基本技巧
    《JavaScript模式》第1章 简介
    点击元素,只有它的背景变色
    笔试题之优化代码
    jQuery实现一个全选复选框联动效果
    《深入浅出Node.js》第3章 异步I/O
    《深入浅出Node.js》第2章 模块机制
    《深入浅出Node.js》第1章 Node简介
  • 原文地址:https://www.cnblogs.com/zhaoziming/p/6054856.html
Copyright © 2011-2022 走看看