2012年5月31日 星期四

Java Basic(43)-Composition


public class tuna {
private String name;
private potpie birthday;
public tuna(String theName, potpie theDate){
name=theName;
birthday=theDate; 這個字符串是在potpie.java
}
public String toString(){
return String.format("My name is %s, my birthday is %s", name, birthday);
}
}

class apples{
public static void main(String args[]){
potpie potObject = new potpie(4,5,6);
tuna tunaObject = new tuna("Greg",potObject);
System.out.println(tunaObject);
}
}

這是運行時的結果:
The construtor for this is 4/5/6
My name is Greg, my birthday is 4/5/6

沒有留言:

張貼留言