1、设计一个Dog类,有名字、颜色、年龄等属性,定义构造方法来初始化类的这些属性,定义方法输出Dog信息,编写应用程序使用Dog类。
代码:
package core;
public class homework1 {
    public static void main(String[] args) {
        // TODO 自动生成的方法存根
        Dog dog=new Dog("黑贝","黑棕色",5);
          System.out.println(dog);
         }
        }
    class Dog {
        private String name;
        private String color;
        private int age;
    public String getName(){
        return name;
    }
    public String getColor(){
        return color;
    }
    public int getAge(){
        return age;
    }
    public void setName(String name){
        this.name=name;
    }
    public void setColor(String color){
        this.color=color;
    }
    public void setAge(int age){
        this.age=age;
    }
    public Dog(String name,String color,int age){
        super();
        this.name=name;
        this.color=color;
        this.age=age;
    }
    public String toString(){
        return "狗的名字:"+this.name+",狗的颜色:"+this.color+",狗的年龄:"+this.age;
     }
}

2、设计一个表示用户的User类,类中的变量有用户名、口令和记录用户个数的变量,定义类的3个构造方法(无参、为用户名赋值、为用户名和口令赋值)、获取和设置口令的方法和返回类信息的方法。
代码:
package core;
public class homework2 {
    public static void main(String[] args) {
        // TODO 自动生成的方法存根
        User user1=new User("大魔王","12345678");
        user1.print();
        User user2=new User("小魔王","87654321");
        user2.print();
        new User().count();
    }
}
class User
{
    private String name;
    private String password;
    private static int count;
    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+"\t"+"口令:"+password);
}
public void count(){
    System.out.println("用户个数:"+(count-1));
 }
}

 

posted on 2017-03-23 23:07  bgd160809208  阅读(654)  评论(2编辑  收藏  举报