zoukankan      html  css  js  c++  java
  • 作业

    .

    复制代码
    package test;
    
    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 
    {
        @SuppressWarnings("unused")
        public static void main(String args[])
        {
            Child c = new Child();
        }
    }
    复制代码

     

     

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

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

    3.当子类与父类拥有一样的方法,并且让一个父类变量引用一个子类对象时,到底调用哪个方法,由对象自己的“真实”类型所决定,这就是说:对象是子类型的,它就调用子类型的方法,是父类型的,它就调用父类型的方法。

    4.类型转换

    复制代码
    package test;
    
    class Mammal{}
    class Dog extends Mammal {}
    class Cat extends Mammal{}
    
    public class TestCast
    {
        public static void main(String args[])
        {
            Mammal m;
            Dog d=new Dog();
            Cat c=new Cat();
            m=d;
            //d=m;
            d=(Dog)m;
            //d=c;
            //c=(Cat)m;
    
        }
    }
    复制代码

     子类对象可以直接赋给基类变量。

    基类对象要赋给子类对象变量,必须执行类型转换,

    其语法是: 子类对象变量=(子类名)基类对象名;

  • 相关阅读:
    网摘习惯
    关于Application.DoEvents()
    五句话足以改变一生
    ActionForm中reset()的用法
    Java的MD5加密和解密类
    ibatis主键自动生成
    Parameter index out of range (3 > number of parameters
    ibatis 2.0 3.0 DTD
    SmartUpload在servlet中使用方法
    The prefix "tx" for element "tx:annotationdriven" is not bound.
  • 原文地址:https://www.cnblogs.com/cuijunfeng/p/9996211.html
Copyright © 2011-2022 走看看