• 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 static void main(String args[]) {
            Child c = new Child();
        }
    }
    

      

    super放在首行结果:

    结论:

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

     

    2、为什么子类的构造方法在运行之前,必须调用父类的构造方法?能不能反过来?为什么不能反过来?

    必须调用,不能反过来,构造函数的作用就是初始化

    子类继承了父类的各种属性,而构造方法则相当于把父类给实例化出来。所以一定会先调用父类的构造方法。

     

    3、参看ExplorationJDKSource.java示例此示例中定义了一个类A,它没有任何成员:

    class A { }

    示例直接输出这个类所创建的对象

    public static void main(String[] args)    {

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

    }

    我们得到了一个奇特的运行结果:

    A@1c5f743

    为什么?

    前面示例中,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();

    4、我们来看一段代码(示例Fruit.java ):

    注意最后一句,一个字串和一个对象“相加”,得到以下结果,为什么?

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

    5、请自行编写代码测试以下特性(动手动脑):

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

    1)覆盖方法的允许访问范围不能小于原方法。

    2)覆盖方法所抛出的异常不能比原方法更多。

    3)声明为final方法不允许覆盖。

    例如,Object的getClass()方法不能覆盖。

    4)不能覆盖静态方法。

    代码:

    package TestInherits;
    public class Person {
    	   String name;
    	    int age;
    		double score;
    		void introduce(){
    	    System.out.println("我的名字是:"+name+",我的年龄是:"+age+",我的成绩是:"+score);
    }
    }
    package TestInherits;
    
    public class Student extends Person{
        void introduce(){
            System.out.println("我的名字是:"+name+",我的年龄是:"+age+",我的成绩是:"+score);      
        }
    }
    package TestInherits;
    public class Fruit {
        public static void main(String[] args) {
            // 生成子类对象,调用子类的方法
            Student a=new Student();
            a.name="hsn";
            a.age=10;
            a.score=99.0;
            a.introduce();
            // 生成父类对象,调用父类的方法
            Person b=new Person();
            b.name="hhh";
            b.age=20;
            b.score=120;
            b.introduce();
        }
    }
    

      结果:

  • 相关阅读:
    【cocos2d-js官方文档】十四、cc.spriteFrameCache 改造说明
    [SVN]创建本地的SVN仓库
    [C++]函数参数浅析
    [Windows Phone]AnimationHelper管理分散的Storyboard
    [Windows Phone]常用类库&API推荐
    [Windows Phone]模仿魔兽3技能按钮SkillButton
    [C++]引用浅析
    [C++]new和delete
    [C++]指针浅析
    [C++]C++中的运行时类型检测
  • 原文地址:https://www.cnblogs.com/hanbook/p/6052416.html
Copyright © 2020-2023  润新知