Java

Java "return this"

2018-05-03  本文已影响4人  JaedenKil
public class Cat {
    String name;
    int age;
    String gender;

    Cat setName(String name) {
        this.name = name;
        return this;
    }

    Cat setAge(int age) {
        this.age = age;
        return this;
    }

    Cat setGender(String gender) {
        this.gender = gender;
        return this;
    }

    void show() {
        System.out.println("Cat " + name + ", age " + age + ", gender " + gender);
    }

    public static void main(String[] args) {
        Cat kitty = new Cat().setName("kitty").setAge(1).setGender("Male");
        kitty.show();
    }
}
Cat kitty, age 1, gender Male
上一篇下一篇

猜你喜欢

热点阅读