属性声明 public int age { get; set; } 从功能上等价于 private int m_age; public int age {get { return m_age; }set { m_age = value; }} 在这里,属性和成员变量的区别不大. 等价于 public int age; 也就是一开始的时候, 无视 有限性.读写权限的情况下.先偷懒, 写成public int age { get; set; } 项目写到某个阶段了, 要求 age 数值必须有
之前一直在C#中使用这两者, 却一直不知道成员变量和属性还是不一样的两种概念. 不过说不一样, 也不是完全对. 简单举个例子: public class myclass { public string A; private sting B = ""; public string GetB { get { retrun B; } set { B = value; } } } 该代码中, A, B即为成员变量, 也叫做字段; GetB 即为属性; 其中 get{}和set{}被称作访问器.
例一: 一个Student pojo类: public class Student{ private String name; private int age; public String getName(){ return this.name; } public void setName(String name){ this.name = name; } public int getAge(){ return this.age; } public void setAge(int age){ t
在我们的程序中经常会出现以下的代码: 如: 成员变量 public string Name; 或者用属性 private string name public string Name() { get { return name; } set { name = value; } } 当然,如果属性中get{} 和 set{}的方法不是这么简单或两个不