美文网首页Java
Java "return this"

Java "return this"

作者: JaedenKil | 来源:发表于2018-05-03 17:17 被阅读4次
    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
    

    相关文章

      网友评论

      本文标题:Java "return this"

      本文链接:https://www.haomeiwen.com/subject/qxdlrftx.html