public class java1 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Dog dog =new Dog("大黄","黄",10); dog.tell(); } } class Dog{ private String name; private String colour; private int age; public Dog(String name,String colour,int age){ this.setName(name); this.setColour(colour); this.setAge(age); } public void tell(){ System.out.println("名字:"+getName()+",颜色:"+getColour()+",年龄:"+getAge()); } public String getName(){ return name; } public void setName(String n){ name = n; } public String getColour(){ return colour; } public void setColour(String c){ colour = c; } public int getAge(){ return age; } public void setAge(int a){ age = a; } }
public class Java2 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub User user1=new User("李四","131415"); user1.print(); User user2=new User("张三","215451"); user2.print(); new User().count(); } } class User { private String name; private String password; private static int count;//count用于统计个数必须用static 声明成全局属性 public User(){ count++; } public User(String name){ this.name=name; count++; } public User(String name,String password){ this.name=name; this.password=password; count++; } public String getName(){ return name; } public void setName(String name){ this.name=name; } public String getPassword(){ return password; } public void setPassword(String password){ this.password=password; } public void print(){ System.out.println("用户名:"+name+" "+"口令:"+password); } public void count(){ System.out.println("用户个数:"+(count-1)); } }