建立一個People型別,有年齡、工資、性別三個屬性。 定義一個方法叫做找物件,找物件方法傳過來一個人;
阿新 • • 發佈:2020-11-18
- 建立一個People型別,有年齡、工資、性別三個屬性。
- 定義一個方法叫做找物件,找物件方法傳過來一個人;
- 首先如果性別相同,就輸出“我不是同性戀”,
- 如果對方是男的,年齡小於28,工資大於10000,就輸出"我們結婚吧",
- 如果年齡太大就輸出“太老了,我們不合適!”,
- 如果錢太少了就輸出“小夥還需在努力,我們不合適”;
- 如果對方是女的,年齡比自己小,工資大於3000,就輸出“我們結婚吧”,
- 如果年齡太大就輸出“我不找比自己大的女性”,
- 如果工資太少就輸出“女士還需再努力,我們不合適”。
public class People { //屬性私有化 private int age; private double salary; private char gender; /** * 找物件方法 * * @param people */ public void find_friend(People people) { //people就是你要找的物件 //性別相同 你(呼叫者) 和傳入的物件性別相同 if (people.getGender() == this.gender) { //this.gender this.可以省略不寫 System.out.println("不是同性戀"); } if (people.getGender() == '男' && people.getGender() != gender) { if (people.getAge() < 28 && people.getSalary() >= 10000) { System.out.println("我們結婚吧"); } if (people.getAge() > 28) { System.out.println("我們不合適"); } if (people.getSalary() < 10000) { System.out.println("小夥還需在努力,我們不合適"); } } if (people.getGender() == '女' && people.getGender() != gender) { if (age >= people.getAge() && people.getSalary() >= 3000) { System.out.println("我們結婚吧"); } if (age < people.getAge()) { System.out.println("我不找比自己大的的女性"); } if (people.getSalary() < 3000) { System.out.println("女士還需再努把力,我們不合適"); } } } public People() { } public People(int age, double salary, char gender) { this.age = age; this.salary = salary; this.gender = gender; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public double getSalary() { return salary; } public void setSalary(double salary) { this.salary = salary; } public char getGender() { return gender; } public void setGender(char gender) { if (gender == '男' || gender == '女') { this.gender = gender; } else { this.gender = '男'; } } }