重载

/*

在同一类中一个方法名被用来定义多个方法。

重载必须遵守规则:

   参数表必须不同,以此区分不同的方法体,返回类型。修饰符可相同或不相同。*/

import java.util.*;

class Tree {
  int height;
  Tree() {
    prt("Planting a seedling");
    height = 0;
  }
  Tree(int i) {
    prt("Creating new Tree that is "
        + i + " feet tall");
    height = i;
  }
  void info() {
    prt("Tree is " + height
        + " feet tall");
  }
  void info(String s) {
    prt(s + ": Tree is "
        + height + " feet tall");
  }
  static void prt(String s) {
    System.out.println(s);
  }
}

public class Overloading {
  public static void main(String[] args) {
     new Tree();
 for(int i = 1; i <= 5; i++) {
      Tree t = new Tree(i);
      t.info();
      t.info("overloaded method");
    }
    // Overloaded constructor:
   
  }
} 

 

posted @ 2018-04-14 09:51  风雨长安  阅读(128)  评论(0编辑  收藏  举报
博客