java 包装一个对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6138787/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 14:31:54  来源:igfitidea点击:

wrapping an Object

javaobjectwrapperword-wrap

提问by Will

I have an Object which comes and has a bunch of public attributes and no getters and setters. BAD! So I created a class with the attributes and created getters and setters for them. My plan is to wrap the object in my class so it means no direct access to attributes. I'm kind of unsure how to do this. I understand casting fine. How exactly could I wrap the class with my safe class with getters and setters and gain access to the attributes via my getters and setters?

我有一个对象,它有一堆公共属性,没有 getter 和 setter。坏的!所以我创建了一个具有属性的类,并为它们创建了 getter 和 setter。我的计划是将对象包装在我的类中,因此这意味着不能直接访问属性。我有点不确定如何做到这一点。我理解铸造很好。我究竟如何用我的安全类和 getter 和 setter 包装类,并通过我的 getter 和 setter 访问属性?

回答by Bala R

Maybe like this?

也许像这样?

class MyCar implements ICar{

    private final Car car;
    public MyCar(Car car)
    {
         this.car = car;
    }

    public string getModel()
    {
          return car.model;
    }

    public void setModel(string value)
    {
          car.model = value;
    }

}

now instead of passing around an instance of Car, you can either pass around MyCarinstance which has getters and setters or a reference to ICarwhich will let you control exactly what you would like to expose (for example you could just expose the getters).

现在Car,您可以传递MyCar具有 getter 和 setter的实例,而不是传递 的实例,也可以传递ICar可以让您准确控制想要公开的内容的引用(例如,您可以只公开 getter)。

回答by hvgotcodes

Use composition. If you class with public properties is called Exposed, just do

使用组合。如果您使用公共属性的类称为 Exposed,只需执行

public class ExposedProtector {
    private Exposed exposed;  // private means it can't be accessed directly from its container

    //public/protected methods here to proxy the access to the exposed.



}

Note that nothing will prevent other people from creating instances of Exposed. You will have to modify the actual exposed class itself, which might be the better way to do this, if possible.

请注意,没有什么可以阻止其他人创建 Exposed 的实例。如果可能,您将不得不修改实际公开的类本身,这可能是更好的方法。

You should look at the java access modifiers. There are varying levels of access, from private to protected to public.

您应该查看 java 访问修饰符。有不同级别的访问权限,从私有到受保护再到公共。

回答by Ted Hopp

If you want your class to be plug-compatible with the original class (meaning that client code does not need to change variable types), then your class will have to be a subclass of the class that the client code expects. In that case, you cannot hide the public variables, although you can easily add getters and setters. Even if you subclass, however, that won't help if the original class has other subclasses; they won't see those getters and setters.

如果您希望您的类与原始类插件兼容(意味着客户端代码不需要更改变量类型),那么您的类必须是客户端代码期望的类的子类。在这种情况下,您无法隐藏公共变量,尽管您可以轻松添加 getter 和 setter。但是,即使您创建子类,如果原始类有其他子类,那也无济于事;他们不会看到那些 getter 和 setter。

If you can introduce an unrelated class, then the solution is to delegate everything:

如果可以引入一个不相关的类,那么解决方案是委托所有内容:

public class BetterThing {
    private Thing thing;
    public BetterThing(Thing thing) {
        this.thing = thing;
    }
    public int getIntProperty1() {
        return thing.property1;
    }
    public void setIntProperty1(int value) {
        thing.property1 = value;
    }
    // etc.
}