且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何同时访问私人和私人在类中声明的公共成员继承?

更新时间:2023-02-15 16:07:03

错误非常明显:无法访问在类中声明的私有成员"

这就是为什么将它们声明为Private的原因,这样您就无法访问它们并弄乱类的内部.
如果确实需要,那么您需要与创建该类的任何人进行交谈,解释为什么它对您如此重要,然后进行更改.


或者找到另一种方法,这可能容易得多.
The error is pretty explicit: "cannot access private member declared in class"

That is why they are declared as Private - so that you can''t access them and mess up the internals of the class.
If you really need to, then you need to talk to whoever created the class, explain why it is so essential for you, and get it changed.


Or find another approach, which is probably a lot easier.


私有成员是私有的,因此继承的类无法访问它们.这是设计使然.受保护将可以从继承的类中进行访问,但在使用该类时则不能.

如果特定的类成员曾经是可见的,但现在已经隐藏了,则可以使用一种非常隐蔽和肮脏的方式.通过显式转换为仍可看到类成员的类型并从那里访问它的方式来工作.但这实际上不应该使用,因为它是错误的代码设计(显然).

祝你好运!
private members are private so inherited classes can''t access them. This is by design. Protected would make it possible to access from inherited classes but not when using the class.

There is a very sneaky and dirty way you could use if the specific class member was once visible but now hidden. This works by explicitly casting to the type where the class member was still visible and access it through there. But this shouldn''t actually be used because it is bad code design (obviously).

Good luck!


好.那么如何访问在类继承中声明的私有成员和公共成员?
Ok .So how to access both private & public member declared in class inherit?