为什么对象类需要受保护的方法? [重复]

时间:2021-02-27 21:31:45

This question already has an answer here:

这个问题在这里已有答案:

The Object is the base class from which every other class is derived. Among others it has methods with Protected access modifier (i.e. MemberwiseClone()).
protected means that the member is accessible from within the class in which it is declared, and from within any class derived from the class that declared this member.

Object是从中派生每个其他类的基类。其中它具有受保护的访问修饰符(即MemberwiseClone())的方法。 protected表示可以从声明它的类中以及从声明此成员的类派生的任何类中访问该成员。

Does that mean that for Object all protected members will be public in fact? And if yes, why is it implemented?

这是否意味着对于Object,所有受保护的成员实际上都是公开的?如果是,为什么要实施?

1 个解决方案

#1


-2  

No it does not mean they will be public. It means that any class that derives from that class can use the protected methods.

不,这并不意味着他们将公开。这意味着从该类派生的任何类都可以使用受保护的方法。

public class A
{
    private void PrivateMethod() {  /*can be seen only here */ }
    protected void ProtectedMethod() {  /*can be seen here and by anyone deriving from me (A) */ }
    internal void InternalMethod() {  /*can be seen here and by anyone in the same assembly with me. */ }
    public void PublicMethod() {  /*can be seen here and by anyone else. */ }
}

#1


-2  

No it does not mean they will be public. It means that any class that derives from that class can use the protected methods.

不,这并不意味着他们将公开。这意味着从该类派生的任何类都可以使用受保护的方法。

public class A
{
    private void PrivateMethod() {  /*can be seen only here */ }
    protected void ProtectedMethod() {  /*can be seen here and by anyone deriving from me (A) */ }
    internal void InternalMethod() {  /*can be seen here and by anyone in the same assembly with me. */ }
    public void PublicMethod() {  /*can be seen here and by anyone else. */ }
}