zoukankan      html  css  js  c++  java
  • 继承与 接口

    一.运行 TestInherits.java 示例,观察输出。

    1.代码

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

    }

    }

    2.

     

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

    4.思考:为什么子类的构造方法在运行之前,必须调用父类的构造方法?能不能反过来?为什么不能反过来?

    5.因为子类是通过父类继承过来的,所以子类拥有父类的属性和方法。如果不调用父类的构造方法,就不能初始化父类中定义的属性,也就不能给父类的属性分配内存空间 ,如果父类的属性没有分配内存空间,那么当子类访问父类的属性时,系统必然会报错。

    二:

    1.代码

    public class ExplorationJDKSource

    public static void main(String[] args) {

    System.out.println(new A());

    }

    }

    class A{}

    2.结果

    3.

              

    真相前面示例中,main方法实际上调用的是:

    public void println(Object x),这一方法内部调用了String类的valueOf方法。

    valueOf方法内部又调用Object.toString方法:

    public String toString() {

    return getClass().getName() +"@" +

    Integer.toHexString(hashCode());

    }

    hashCode方法是本地方法,由JVM设计者实现:

    public  native int hashCode();

    三.动手动脑

    ·在子类中,若要调用父类中被覆盖的方法,可以使用super关键字。

    public class Super

    {

    public static void main(String args[])

    {

    Child c=new Child();

    c.show();

    }

     

    }

    class Parent

    {

    public void show()

    {

    System.out.println("parent");

    }

    }

    class Child extends Parent

    {

    public void show()

    {

    System.out.println("child");

    super.show();

    }

    }

    四.方法覆盖(override

    public class Fruit

    {

     

    public String toString()

    {

    return "Fruit toString.";

    }

     

    public static void main(String args[])

    {

    Fruit f=new Fruit();

    System.out.println("f="+f);

    // System.out.println("f="+f.toString());

    }

    }

    运行结果截图:

     

    ·结论:在+”运算中,当任何一个对象与一个String对象,连接时,会隐式地调用其toString()方法,默认情况下,此方法返回“类名 @ + hashCode”。为了返回有意义的信息,子类可以重写toString()方法。

    ·要点:方法覆盖要求子类与父类的方法一模一样,否则就是方法重载(overload)!

     

  • 相关阅读:
    第8.13节 Python类中内置方法__repr__详解
    Python中splitlines方法判断文本中一行结束除了回车换行符是否还有其他字符?
    Python中使用eval执行下面函数的结果怎么是字符串'10020'?
    第8.12节 Python类中使用__dict__定义实例变量和方法
    ThinkPHP---thinkphp拓展之空操作
    ThinkPHP---TP功能类之邮件
    ThinkPHP---案例--实现知识管理功能
    ThinkPHP---TP功能类之公文管理功能2----------继续完善
    ThinkPHP---TP拓展之获取IP信息
    ThinkPHP---layer插件
  • 原文地址:https://www.cnblogs.com/shenghuizhang/p/6053822.html
Copyright © 2011-2022 走看看