Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
有一个带有一些私有数据 b 的类 A。如果我尝试使用 Ab 来获取数据,则会出现编译错误,因为我正在尝试对私有数据使用点运算符。
如果我为这个 A 类编写一个复制构造函数,我可以使用点运算符从我想要复制的对象中获取私有值 b。
为什么?
如果您想访问私有数据,您必须使用 set 和 get 方法,因为私有变量只能在同一个类中访问(外部类无法访问它)。get 方法返回变量值,set 方法设置值。
例如,如果类的实例称为a并且您要访问的数据称为b,您可以使用a.getB()ora.setB(value)
a.getB()
a.setB(value)