zoukankan      html  css  js  c++  java
  • 归档七

    课后作业1

    运行 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 statclass 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();

        }

    }

    ic void main(String args[]) {

            Child c = new Child();

        }

    }

    结论:

        通过 super 调用基类构造方法,必须是子类构造方法中的第一个语句。子类中,调用父类中被覆盖的方法,可以使用super语句

    课后作业2

    代码:

            class Parent{

        public Parent() {

           

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

        }

        public void show(){

          System.out.println("hello java !");

        }

    }

    class Child extends Parent {

        public Child(){

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

    }

        public void show(){

            super.show();

         System.out.println("say hello! child!!!");

         

       }

    }

    public class Test6 {

        public static void main(String args[]) {

            Child c = new Child();

            c.show();

        }

    }

  • 相关阅读:
    ASP.NET Web API是如何根据请求选择Action的?[上篇]
    Ruby的对象模型
    MongoDB学习3
    Linux目录树详细说明
    Matlab.NET混合编程技巧之——直接调用Matlab内置函数(附源码)
    [置顶] SQL注入安全分析
    3.9 聚集和联接
    Qt之QTemporaryFile(文件名唯一,且可以自动删除)
    调用Windows属性窗口(居然是通过注册表来调用的)
    QTextEdit中选中文本修改字体与颜色,全部文本修改字体与颜色(设置调色板的前景色、背景色、文字颜色以及基色)
  • 原文地址:https://www.cnblogs.com/CkmIT/p/6055621.html
Copyright © 2011-2022 走看看