java,构造函数,满载
java,构造函数,重载
class Tree { int height; Tree() { print("Planting a seedling"); height = 0; } Tree(int i) { print("Creating new Tree that is "+ i + " feet tall"); height = i; } public void plant() { print("Tree is " + height + " feet tall"); } public void plant(String name) { System.out.println(s + ": Tree is "+ height + " feet tall"); } static void print(String s) { System.out.println(s); } } public class OverLoading { public static void main(String[] args) { for (int i = 0;i < 5;i++) { Tree t = new Tree(5); t.plant(); t.plant("OverLoading"); } Tree t = new Tree(); } }
如果传入的实际参数类型“小于”方法中声明的形式参数类型,实际参数的类型就会被“提升”。char 型略有不同,如果无法找到恰好接受 char参数的方法,就会把 char 直接提升至 int 型。
方法接受“较小”的基本类型作为参数。如果传入的实际参数“较大”,你就得在圆括号里写上类型名称,做必要的类型转换。如果不这样做,编译器就会报错.