Java學習之物件導向四

來源:互聯網
上載者:User

標籤:結束   對象   color   關鍵字   class   java學習   許可權   size   初始化   

1、構造方法的格式是什嗎?有哪些注意事項?
  許可權修飾符 類名(參數列表){方法體}
 注意事項:
 ?構造方法沒有傳回值類型。也不需要寫傳回值。因為它是為構建對象的,對象建立完,方法就執行結束。
 ?構造方法名稱必須和類型保持一致。
   ?構造方法沒有具體的傳回值。


2、構造方法可以重載嗎? 裡邊可以有return語句嗎?
    可以重載 可以有return,但不能有返回。


3、給成員變數賦值有幾種方式?有什麼區別?
 get/set
 直接賦值
 帶參構造器賦值
 初始化
 static 代碼塊賦值
 預設構造器賦值

4.編寫一個完整的Person類,使用this關鍵字給成員變數進行賦值.
在測試類別中測試

public class Person {    private String name;    private int age;        public Person(String name, int age) {        this.name = name;        this.age = age;    }        public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public int getAge() {        return age;    }    public void setAge(int age) {        this.age = age;    }        }
/** * 編寫一個完整的Person類,使用this關鍵字給成員變數進行賦值.     在測試類別中測試 * @author vanguard * */public class Test01 {    public static void main(String[] args) {        Person p = new Person("張三", 30);                System.out.println(p.getName() + "---" +p.getAge());    }}

5、根據需求,完成如下代碼(按照標準格式寫),並在測試類別中進行測試。
標準格式包含: 私人屬性 無參構造 有參構造 setter 和getter 需求中的方法
需求一:
員工類Employee
屬性:姓名name,工號id,工資salary
行為:顯示所有成員資訊的方法show()

需求二:
動物類Animal
屬性:姓名name,年齡age
行為:吃飯eat,睡覺sleep

需求三:
人類Person
屬性:姓名name,年齡age,性別gender
行為:學習study,睡覺sleep

/**    員工類Employee    屬性:姓名name,工號id,工資salary     行為:顯示所有成員資訊的方法show() * @author vanguard * */public class Employee {    private String name;    private String Id;    private int salary;        public Employee() {            }        public Employee(String name, String Id, int salary) {        this.name = name;        this.Id = Id;        this.salary = salary;    }        public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public String getId() {        return Id;    }    public void setId(String id) {        Id = id;    }    public int getSalary() {        return salary;    }    public void setSalary(int salary) {        this.salary = salary;    }        public void show() {        System.out.println(name + "---" + Id + "---" + salary);    }}

 

/** * 動物類Animal     屬性:姓名name,年齡age     行為:吃飯eat,睡覺sleep * @author vanguard * */public class Animal {    private String name;    private int age;        public Animal() {}        public Animal(String name, int age) {        this.name = name;        this.age = age;    }        public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public int getAge() {        return age;    }    public void setAge(int age) {        this.age = age;    }        public void eat() {        System.out.println("動物吃飯");    }        public void sleep() {        System.out.println("動物睡覺");    }    }
/** * 動物類Animal     屬性:姓名name,年齡age     行為:吃飯eat,睡覺sleep * @author vanguard * */public class Animal {    private String name;    private int age;        public Animal() {}        public Animal(String name, int age) {        this.name = name;        this.age = age;    }        public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public int getAge() {        return age;    }    public void setAge(int age) {        this.age = age;    }        public void eat() {        System.out.println("動物吃飯");    }        public void sleep() {        System.out.println("動物睡覺");    }    }
/** * 測試類別 * @author vanguard * */public class Test {    public static void main(String[] args) {        Employee em = new Employee("張三", "001", 5000);        em.show();                Animal a = new Animal("小白", 2);        a.eat();        a.sleep();                Person p = new Person("李四", 20, "男");        p.study();        p.sleep();            }}

 

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.