Package five;
public class Five {
public static void Main (string[] args) {
User P1=new user ();
P1.tell ();
User P2=new User ("XPQ");
P2.tell ();
User P3=new User ("Xpq", "mima123");
P3.tell ();
}
}
Class user{
Private String UserName;
private String password;
private static int num; Use the static property to count how many instantiated objects a class produces
Public User () {//define no parameter construction method
num++;
};
Public User (String userName) {//define single parameter construction method
num++;
This.username=username;
}
Public User (String username,string password) {//define double-parameter construction method
num++;
This.username=username;
This.password=password;
}
Public String GetPassword () {//method to get the password
return password;
}
public void SetPassword (String password) {//How to set the password
This.password=password;
}
public void Tell () {
System.out.println ("User named" +username+ "corresponding password is" +password+ "the number of users is" +num);
}
}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
"Java Combat Development classic" chapter fifth 5.5