Java开发基础篇SE中this关键字

2021-08-08 05:57

阅读:588

public class Teacher {              private String name;        private int age = 30;        private String gender = “女”;              public Teacher() { // 无参构造器               this(“佟刚”, 35, “女”); // 调用其他构造器        }            public Teacher(String name, int age, String gender) {               this.name = name; // this表示对象, 右侧的name是形参, 是局部变量               this.age = age;               this.gender = gender;        }         public void setName(String name) {               this.name = name;        }        public String getName() {               return this.name; // 加上this也可以, 但是没有必须, 它暗含了this        }        public void setAge(int age) {               this.age = age;        }         public int getAge() {               return age;        }          public void setGender(String gender) {               this.gender = gender;        }           public String getGender() {               return gender;        }              public String say() {               return “姓名:” + this.name + “,年龄:” + this.age + “,性别:” + this.gender;        } }


评论


亲,登录后才可以留言!