java第四次作業

來源:互聯網
上載者:User

標籤:system   dog   表示   text   super   屬性   java   blog   span   

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)); }}

 

java第四次作業

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.