Thinking in java 里有这么一句话:so private allows you to freely change that member without concern that it will affect another class in the same package;
我的理解就是我可以随意改变private成员,不必考虑这么做是否会影响其他类!
但有这样的情况:
一个类a中有private属性,public方法,方法中调用了private属性;然后另一个类b调用此public方法,此时可以运行,但如果把a中private属性更改了,b中就会报错吧,那么private的作用何在?
很是不解,求大神指导!
public方法是自己写的吧,,,加上private,,就可以灵活的访问数据
举个例子:某个属性 name,,,我设置了private
我需要让它只读,,可以设置一个,,public的getName,方法,,
按照题主的说法,我把private换成public,,我的只读该如何设置尼??
private属性不是为了不让别人用,,而是让别人按照要求用,,不知道题主能不能理解。
private属性是可以读写的,,但是需要通过本类中的public方法(方法中,就可以加入读写规定。)
有问题还可以追问,,对你有帮助,,请采纳。