zoukankan      html  css  js  c++  java
  • 构造方法也可以实现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 }
  • 相关阅读:
    oracle 自动备份,删除历史
    oracle 系统语法
    myeclipse快捷键
    ANT教程
    新手 Spring 疑惑
    MyEclipse8.5整合Git (转)
    eclipse build path功能总结
    Apache Maven 入门篇(下)
    Mac OS下配置PHP Nginx PHP-FPM
    Python删除列表中元素
  • 原文地址:https://www.cnblogs.com/borter/p/9418528.html
Copyright © 2011-2022 走看看