构造方法,方法重载

来源:互联网 发布:淘宝靠谱的奢侈品代购 编辑:程序博客网 时间:2024/05/19 12:12
默认构造方法无参数
带参数的构造方法进行初始化
重载参数不同
public class Tree {static int height;Tree(){System.out.println("Plant a seedling");}Tree(int initialHeight){height = initialHeight;System.out.println("Create new Tree that is "+height+" feet tall");}void info(){System.out.println("Tree is "+height+" feet tall");}void info(String s){System.out.println(s+":Tree is "+height+" feet tall");}public static void main(String[] args) {for(int i = 0 ; i < 5 ; i++){Tree t = new Tree(i);t.info();t.info("overloaded method");}new Tree();System.out.println(height);}}