如何在私有和受保护的访问修饰符之间进行选择,以封装基类和子类之间的成员? [英] How to choose between private and protected access modifier to encapsulate members between base and childs classes?

查看:98
本文介绍了如何在私有和受保护的访问修饰符之间进行选择,以封装基类和子类之间的成员?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试一个项目以在内部函数中使用私有值。过去我只使用公共参数,但是我注意到使用尽可能多的私有参数时,混淆处理的效果更好。

I am trying on a project to use private values in my internal functions. In past I used only public ones, but I noticed that obfuscation is working much better when using as much as possible private parameters.

我的问题是关于父/子类的。

My question is regarding Parent/Child classes.

在我的主类中,我定义所有参数如下:

In my main class I define all the parameters as following :

public class MyFatherClass
{
    private long id = -1;
    public long ID { get { return this.id; } set { this.id = value; } }    
    ...
}

因此在所有内部函数中,我都可以访问

So in all internal functions I access to my private value instead of the public one.

然后在我的女儿类中,我只添加了特定于子类的参数。

Then in my daughter class I just add parameters specific to the child class.

public class MyChildClass : MyFatherClass
{
    private long anotherParameter = -1;
    public long AnotherParameter { get { return this.anotherParameter; } set { this.anotherParameter = value; } }    
    ...
}

只是,我在我的父类,我可以毫无问题地访问 id ID ,但是从子类中,我只能访问 ID (因为 id 是私有的)。

Just, I see that in my Parent class, I can access to id and ID without problem, but from daughter classes I can only access ID(as id is private).

如果我理解正确,我需要在父文档中将所有 private 替换为 protected ,这样才能解决问题?
我不理解的是,即使我保留了代码,代码仍然可以正常工作。
为什么在子类中设置 ID 值时,我没有错误消息,句子 this.id = value 被执行了,但是如果我的孩子班级是私人的,我怎么能从我的孩子班级访问它呢?

If I understood correct, I would need to replace all private by protected in my parent lass, so it would solve the problem? What I don't understand is the code is working even if I leave it so. Why don't I have an error message, when I set ID value in daughter class, the sentence this.id=value is executed, but how can can I access to it from my child class if it is private?

我现在很犹豫,我是否可以添加每个子类中都有一个私有ID ,或者我可以在父类中将 id 设置为受保护吗?

I am now hesitating, may I just add a private id in each child class, or may I set id to protected in my parent class?

谢谢您的解释。

编辑,仅在混淆后添加我的反向代码的屏幕截图,这样您就可以理解其中的区别如何混淆私有/公共方法/字段

Edit, just adding a screenshot of my reversed code after obfuscation, so you could understand difference on how are obfuscated private/public methods/fields

推荐答案


为什么没有错误消息,当我在子类中设置ID值时,句子 this.id = value 已执行,但是我如何才能访问它rom我的孩子班级是否是私有的?

Why don't I have an error message, when I set ID value in daughter class, the sentence this.id=value is executed, but how can can I access to it from my child class if it is private?

在类上调用公共方法时,该方法可以访问该孩子的私有成员类:

When you call a public method on a class, that method can access private members of that class:

public class Foo
{
    public void Bar()
    {
        Baz();
    }

    private void Baz()
    {
        // private method called by public method
    }
}   

var foo = new Foo();
foo.Bar();

这样编译就可以了。您的设置方法是相同的:它是公共的,因此即使访问私有成员也可以在任何地方调用。

This compiles just fine. Your setter is the same: it's public, so callable from everywhere, even if it accesses private members.

关于使您的字段(私有长id = -1; )保护:是的,这意味着您可以在派生类中访问它。但是,是否愿意是另一个问题。

As for making your field (private long id = -1;) protected: yes, that will mean you can access it in derived classes. But whether you want to is another question.

您宣布公共财产是有原因的。也许您想在其setter或getter中进行一些验证。否则,如果您只是使用属性来访问私有字段,则可以抛弃整个私有字段并使用自动实现的属性:

You have declared a public property for a reason. Perhaps you want to do some validation in its setter or getter. If not, if you're just using a property to access a private field, you could just ditch the entire private field and use an auto-implemented property:

public long ID { get; set; } = -1;

然后,您可以从任何地方访问属性,无论是其内部,派生类还是使用此类的代码。

Then you can access the property everywhere, from within itself, from derived classes and from code using this class.

另请参见:

  • What is the difference between a field and a property?
  • What are Automatic Properties in C# and what is their purpose?

这篇关于如何在私有和受保护的访问修饰符之间进行选择,以封装基类和子类之间的成员?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆