简体   繁体   English

在没有吸气剂的情况下访问Model类的私有变量

[英]Accessing private variable of Model class without getters

I have a Model class defined in my project. 我在项目中定义了一个Model类。 and as usual it has some private variables and public getters and setters 和往常一样,它具有一些私有变量以及公共获取器和设置器

public class Person{

 private ArrayList<String> mark;

 public void setMark(ArrayList<String> mark){
  this.mark = mark;
 }

 public void getMark(){
  return this.mark;
 }
}

Suppose in some other class I am using this Model like 假设在其他班级,我正在使用这个Model

Person person = new Person();
ArrayList<String> mark = new ArrayList();
mark.add("10");
mark.add("15");
mark.add("18");
person.setMark();

then the private variable of person holds the value "my name", the I am accessing the variable using public getter of the class like 然后person的私有变量持有值“ my name”,我正在使用此类的公共getter访问该变量

ArrayList<String> localMark = person.getMark()

so as per my knowledge person.getMark() returns the reference of private variable name, so if I modify the local variable 'localMark', then it will effect the private variable of Person class, so there it breaks the private property of the variable 因此,据我所知,person.getMark()返回私有变量名称的引用,因此如果我修改局部变量“ localMark”,它将影响Person类的私有变量,因此会破坏该变量的私有属性

ex: 例如:

 ArrayList<String> localMark = person.getMark();
 System.out.println(localMark.get(0)); // will be "10"
 localMark.set(0,"25") // person.mark will be changed
 System.out.println(person.getMark().get(0)); //will be printing "25"

most of the developers following the same design pattern I guess, but what is the correct way to create Models 我猜大多数开发人员都遵循相同的设计模式,但是创建Models的正确方法是什么?

EDIT 编辑

As per the comment of vinod I checked, and Strings it passes value but not reference but for ArrayList... it returns reference. 根据我检查的vinod的注释,并通过Strings传递值但不传递引用,但对于ArrayList ...它返回引用。

You have a reference (name) to an object instance (the value of name). 您具有对象实例的引用(名称)(名称的值)。 As the reference is private, you're in full control of it. 由于引用是私有的,因此您可以完全控制它。

When you return a reference, you in fact return it 'by value' , meaning that a copy of the reference is returned. 当您返回引用时,实际上您是按“值” 返回 ,这意味着返回引用副本 Both references point to the same value (the String instance)). 两个引用都指向相同的值(String实例)。

An outside caller obtaining the reference can assign a new value, but your model's own reference is unaffected by that and still points to the value. 获得参考的外部调用者可以分配新值,但是模型自己的参考不受此影响,并且仍指向该值。

It's like a dog (object) on a leash (reference). 这就像一条拴在皮带(参考)上的狗(物体)。

  • When you return a reference you're returning a new leash onto the same dog. 当您返回参考时,您将向同一只狗返回一条新的皮带。
  • The owner of the new reference can modify your dog (pet it, shave it, whatever) when the dog is mutable (which Strings are not, so it cannot be modified) 当狗可变时,新引用的所有者可以修改您的狗(将其剃毛,剃毛,等等)(该字符串不是,因此无法对其进行修改)
  • ...or he can attach a new dog to his leash ...或者他可以把一条新狗拴在皮带上
  • ...but he can never (reflection aside) attach YOUR leach to another dog. ...但是他永远无法(撇开反射)将您的浸出物附在另一只狗上。

If the instance being exposed by call to get() is mutable , then whatever changes you make in some other place will be reflected in the instance everywhere it is used. 如果通过调用get()公开的实例是可变的 ,那么您在其他地方进行的任何更改都将在使用该实例的任何地方反映出来。

Example : 范例:

methodX classA - 
 List<String> locaNamesList = person.getNamesList();
 locaNamesList.clear();

Somewhere else
methodY classB -
List<String> locaNamesList = person.getNamesList(); // note the same person instance should be used.
//locaNamesList will be empty here

Just re-assigning the reference won' change anything. 只是重新分配参考不会改变任何事情。

List<String> locaNamesList = person.getNamesList();
locaNamesList = null; // won't change the actual list. You are setting local field locaNamesList to null and not the actual instance.

You have to use defensive-copies of mutable instances and pass them around if you don't want the original instance to be changed by external players (provided you can't make the instance itself immutable ) 如果您不希望外部玩家更改原始实例,则必须使用可变实例的防御副本并将其传递给他人(前提是您不能使实例本身不可变

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM