构造方法也可以实现overloading

构造方法也可以实现overloading。例:

public void teach(){};

public void teach(int a){};

public void teach(String a){}为三种不同的方法。

 

Overloading方法是从低向高转。

Byte—short—float—int—long—double。

 

在构造方法中,this表示本类的其他构造方法:

student(){};

student(string n){

 this();//表示调用student()

}

如果调用student(int a)则为this(int a)。

特别注意:用this调用其他构造方法时,this必须为第一条语句,然后才是其他语句。

 1 package TomText;
 2 
 3 public class TomText_10 {
 4      /*
 5       * 计算阶乘的和
 6       */
 7      public long sumOfFactorial(int n){
 8       
 9          long result = 0; //阶乘的和
10       long f = 1;      //阶乘
11          
12       for(int i=1;i<=n;i++){
13        
14        f = f * i;   //计算阶乘
15        result += f; //计算阶乘的和
16       }
17       
18       return result;   //返回结果
19       
20      }
21      public static void main(String[] args) {
22          TomText_10 t=new TomText_10();
23          long s=t.sumOfFactorial(3);
24          System.out.println(s);
25      }
26 
27 }
原文地址:https://www.cnblogs.com/borter/p/9418528.html