2012年5月31日 星期四

Java Basic(46)-Static


public class tuna {
private String first;
private String last;
private static int members = 0; 可以使用在所有的對象
public tuna(String fn,String ln){
first = fn;
last = ln;
members++;
System.out.printf("Constructor for %s %s,members in the club%d\n",first,last,members);
}
}

class apples{
public static void main(String[] args){
tuna member1 = new tuna("Megan","Fox");
tuna member2 = new tuna("Natalie","Portman");
tuna member3 = new tuna("Taylor","Swift");
}
}
這是運行時的結果:
Constructor for Megan Fox,members in the club: 1
Constructor for Natalie Portman,members in the club: 2
Constructor for Taylor Swift,members in the club: 3




沒有留言:

張貼留言