简体   繁体   English

如何在继承中实现封装

[英]how to achieve encapsulation in inheritance

I have two classes Test and Encap . 我有两个类TestEncap I have a private variable a and access via setter and getter method. 我有一个私有变量a ,可以通过setter和getter方法访问。 and i'm inheriting the class Test with Encap . 而且我继承了EncapTest

now i am able to change the value of a using setValue(int a) . 现在我能够改变的值, a使用setValue(int a) i want to restrict that option. 我想限制该选项。 i want it to make it as a read only value. 我希望它使其成为只读值。 please assist me in this. 请协助我。

class Test
{
    private int a;
    protected void setValue(int a)
    {
        this.a = a;
    }
    protected void getValue()
    {
        System.out.println("The assigned value of a is : "+this.a);
    }
}
public class Encap extends Test {
    public static void main(String [] args)
    {
        Test t = new Test();
        t.setValue(4);
        t.getValue();
        Encap e = new Encap();
        e.setValue(3);
        e.getValue();      
    }
}

One option would be to delete the method setValue() from the class Test : 一种选择是从Test类中删除 setValue()方法:

class Test
{
    private int a;

    protected void getValue()
    {
        System.out.println("The assigned value of a is : "+this.a);
    }
}

Edit: Why to do this? 编辑: 为什么要这样做? If Encap inherits from Test , it should be able to do the same actions as Test . 如果Encap继承自Test ,则它应该能够执行与Test相同的操作。 Otherwise, what's the purpose of inheriting? 否则,继承的目的是什么? If you still thinking that Test should be able to modify the value and Encap not, maybe your design is wrong. 如果您仍然认为Test应该能够修改该值而不能修改Encap ,则可能是您的设计错误。 You could try something like this instead: 您可以尝试这样的方法:

              BaseClass
              ---------
              +getValue
                /   \
               /     \
           Test       Encap
         --------   ---------
        +setValue   

If you mean that you want a derived class to not expose public methods of the superclass , then your code probably 'smells'... 如果您想让派生类不公开超类的公共方法 ,那么您的代码可能会“闻起来” ...

Remember that Inheritance models "Is A" 请记住,继承模型为“是”

So in your example an Encap is a Test and you should be able to do anything to an Encap that you can do to a Test. 因此,在您的示例中,Encap 测试,您应该能够对Encap进行任何测试。

However, if you simply must inherit from a class where you don't want to expose a parent-class method, you can override the method and have its body do nothing. 但是,如果您只是必须从不想公开父类方法的类继承,则可以重写该方法并使它的主体不执行任何操作。 But for simple getter and setter accessor methods this is potentially very confusing for clients of your code. 但是对于简单的getter和setter访问器方法,这可能会使您的代码客户端感到困惑。

If you can't reconcile things and calling setValue() on an Encap is never the right thing to do, i would recommend overriding the method, commenting it liberally and have it do nothing, or throw an exception to indicate to the client that they're doing something that doesn't make sense. 如果您无法调和事情,并且在Encap上调用setValue()永远都不是正确的事,我建议您重写该方法,对其进行自由注释,使其无所事事,或者抛出异常以向客户端表明他们正在做没有意义的事情。

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

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