1.设计一个Dog类,有名字、颜色、年龄等属性,定义构造方法来初始化类的这些属性,定义方法输出Dog信息,编写应用程序使用Dog类。
public class Dog {
private String name;
private String color;
private int age;
public Dog(String name, String color, int age) {
this.name = name;
this.color = color;
this.age = age;
}
public void print() {
System.out.print("名字: " + this.name + "
" + "颜色:" + this.color + "
"
+ "年龄:" + this.age);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
2. 设计一个表示用户的User类,类中的变量有用户名、口令和记录用户个数的变量,定义类的3个构造方法(无参、为用户名赋值、为用户名和口令赋值)、获取和设置口令的方法和返回类信息的方法。
public class User {
private String name;
private String password;
private static int num;
public User() {
num++;
}
public User(String name) {
this.name = name;
num++;
}
public User(String name, String password) {
this.name = name;
this.password = password;
num++;
}
public void print() {
System.out.print("用户个数:" + num + "
");
System.out.println("名字: " + name + " " + "口令:" + password + "
");
}
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;
}
}