構造方法中的問題
為什么在主函數中直接實例化對像后就能直接打印構造方法中的輸出語句?對于有參的構造方法,為什么要這樣做
public class InterPhone {
public static void main(String[] args) {
// TODO Auto-generated method stub
Telephone phone1=new Telephone();
Telephone phone2=new Telephone(5.0f,1.4f,2.0f);
}
}
才能打印有參構造方法中的輸出語句
2016-06-15
public?class?Dome{ ????public?String?str; ????public?Dome(String?str){//這是一個有參數的構造函數 ????this.str=str; ????System.out.print("這是構造方法。") ????} ???? ????public?static?void?main(String?[]args){ ????????Dome?d?=?new?Dome(str); ???????? ????} } /*構造函數又稱為構造方法,你可以將構造函數理解為一個特別的普通方法,普通方法能夠實現的在構造函數中也可以實現,只不過構造方法多了一個能夠實例化對象的功能。*/2016-06-15
你要知道只有main方法中的語句才會被執行,你可以在main方法中去調用其他方法。
如果你直接寫一個構造方法在類中,但你不去調用它,它是不會被執行的。
不知道你的問題是不是這個。