2012年5月31日 星期四

Java Basic(42)-ToString


1首先創建新class稱為potpie




public class potpie {
private int month;
private int day;
private int year;
public potpie(int m, int d, int y){
month = m;
day = d;
year = y;
System.out.printf("The constructor for this is %s\n", this); 使用當前對象
}
public String toString(){
return String.format("%d/%d/%d", month,day,year);
}
}


In apples.jave:

class apples{
public static void main(String args[]){
potpie potObject = new potpie(4,5,6);
}
}

這是運行時的結果:
The construtor for this is 4/5/6

沒有留言:

張貼留言