重载
/*
在同一类中一个方法名被用来定义多个方法。
重载必须遵守规则:
参数表必须不同,以此区分不同的方法体,返回类型。修饰符可相同或不相同。*/
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: } }
你一定会喜欢那个因为喜欢她而发光的自己!
个人博客:http://www.yanghelong.top
个人博客:http://www.yanghelong.top