java继承 构造器 重载,重写

继承的重写 (Override)

class Base

{

    ​public void function1()    ​//无参数的方法

    ​{

    ​    ​功能1;

    ​}

     ​public void function1(Datatype var1)    ​//带一个参数的方法

    ​{

    ​    ​功能2;

    ​}

}

class Sub extends Base

{

    ​public void function1()

    ​[

    ​    ​//如果需要和父类的function1功能完全一样,直接调用父类的function1()

    ​    ​super.function1();

    ​    ​//如果希望子类的function1()功能和父类的不一样,那么就需要重新写子类的function1()

    ​    ​//各种自定义的..功能

    ​}    ​

}

=================================================================================

继承的重载 (Overload)

默认下子类继承了父类所有的public公有的方法,private的不能获得

方法的重载是相同的函数名,但是方法里面的参数列表不同,和一般方法重载一样

所以当父类的function是private修饰时,子类的function是新建的和父类没关系

class Base

{

    ​public void function1()    ​//无参数的方法

    ​{

    ​    ​功能1;

    ​}

     ​public void function1(Datatype var1)    ​//带一个参数的方法

    ​{

    ​    ​功能2;

    ​}

}

class Sub extends Base

{

    ​public void function1(和父类参数列表不同的参数)

    ​{

    ​    ​功能;

    ​}

}

=================================================================================

=================================================================================

对父类构造器的调用

子类不能获得父类的构造器

子类不能直接使用父类的构造器,但是可以在子类的构造器中使用super(value)来调用父类的构造器

class Base

{

    ​Base(){}    ​//不带参数的构造器

    ​Base(Datatype var)

    ​{

    ​    ​功能1;

    ​}

    ​Base(Datatype var1,Datatype var2)

    ​{

    ​    ​功能1;

    ​    ​功能2;

    ​}

}

class Sub extends Base

{

    ​Sub(){}    ​//不带参数的子类

    ​Sub (Datatype var1)    ​    ​//带一个参数的子类构造器

    ​{

    ​    ​super(var1);

    ​    ​其他功能;

    ​}

    ​

    ​Sub(Datatype var1,Datatype var2)    ​//带两个参数的子类构造器

    ​{

        ​super(var1,var2);

    ​    ​其他功能;

    ​}

    ​public static void (String[] arg)

    ​{

    ​    ​//这样使用子类的构造器,简介使用了父类的构造器,对这些初始化

    ​    ​//错误的使用

    ​    ​//new super(var1,var2); 不能直接调用父类的构造器

    ​}

}

  1 class FatherA 
  2 {
  3     String name;
  4     FatherA()
  5     {
  6         System.out.println("FatherA不带参数的构造器");
  7     }
  8     FatherA(String name)
  9     {
 10         this.name = name;
 11         System.out.println("FatherA带一个参数的构造器 : " + name);
 12     }
 13 }
 14 
 15 class FatherB extends FatherA
 16 {
 17     String name;
 18     int age;
 19     String sex;
 20     FatherB()
 21     {
 22         System.out.println("FatherB不带参数的构造器");
 23     }
 24 
 25     FatherB(String name)
 26     {
 27         super(name);
 28         System.out.println("FatherB带一个参数的构造器 : " + name);
 29         
 30     }
 31 
 32     FatherB(String name , int age)
 33     {
 34         super(name);
 35         this.age = age;
 36         System.out.println("FatherB带两个参数的构造器 : " + name + " : " + age);
 37     }
 38 
 39     FatherB(String name ,int age ,String sex)
 40     {
 41         //super(name,age);其父类没有该构造器,故不能使用啦
 42         this(name,age);    //调用本类的构造器
 43         this.sex = sex;
 44         System.out.println("FatherB带三个参数的构造器 : " + name + " : " + age + " : " + sex);
 45     }
 46 
 47     public void myFunction()
 48     {
 49         System.out.println("hello world");
 50     }
 51     public void myFunction1()
 52     {
 53         System.out.println("base class function");
 54     }
 55 }
 56 
 57 public class SonClass extends FatherB
 58 {
 59     public SonClass()    //无参数构造器
 60     {
 61         //super("admin",20);
 62         //System.out.println("显式调用了其父类的带两个参数的");
 63 
 64         super();
 65         System.out.println("调用其父类不带参数的构造器");
 66     }
 67 
 68     public SonClass(String name)
 69     {
 70         super(name);//调用其父类带一个参数的构造器
 71         System.out.println("显式调用一个参数的..");
 72     }
 73 
 74     public SonClass(String name ,int age)
 75     {
 76         super(name,age);
 77         System.out.println("调用两个参数的...");
 78     }
 79 
 80     public SonClass(String name , int age ,String sex)
 81     {
 82         super(name,age,sex);
 83         System.out.println("调用三个参数的...");
 84     }
 85     
 86     public void myFunction(String name) //重载啦
 87     {
 88         System.out.println(name + " hello world");
 89     }
 90     
 91     public void myFunction()    //重写了父类的function()
 92     {
 93         System.out.println("china hello world");    
 94     }
 95     public static void main(String[] arg)
 96     {
 97         new SonClass();    //new一个无参数的对象
 98         System.out.println("============================");
 99         new SonClass("hello"); //new一个带参数的对象
100 
101         System.out.println("============================");
102         new SonClass("china",20);
103 
104         System.out.println("============================");
105         new SonClass("japan",20,"man");
106 
107         System.out.println("============================");
108         (new SonClass()).myFunction();
109 
110         System.out.println("============================");
111         (new SonClass()).myFunction("admin");
112     }
113 }
如有雷同,纯属意外! good good study,day day up! go,go,go!
原文地址:https://www.cnblogs.com/act262/p/3173612.html