公共类无法访问由于其保护级别

编程入门 行业动态 更新时间:2024-10-26 11:19:37
本文介绍了公共类无法访问由于其保护级别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有以下类:

namespace Bla.Bla { public abstract class ClassA { public virtual void Setup(string thing) { } public abstract bool IsThingValid(); public abstract void ReadThings(); public virtual void MatchThings() { } public virtual void SaveThings() { } public void Run(string thing) { Setup(thing); if (!IsThingValid()) { } ReadThings(); MatchThings(); SaveThings(); } } } namespace Bla.Bla { public class ClassB : ClassA { ClassB() { } public override void IsThingValid() { throw new NotImplementedException(); } public override void ReadThings() { throw new NotImplementedException(); } } }

现在我尝试做以下内容:

Now I try to do the following:

public class ClassC { public void Main() { var thing = new ClassB(); ClassB.Run("thing"); } }

它返回以下错误:ClassB的是人迹罕至因它的保护级别。

Which returns the following error: ClassB is inaccessible due to its protection level.

但他们都是公开的。

推荐答案

此错误是 ClassB的的保护级别,因此小号的构造的,而不是 ClassB的本身。由于构造函数的名称相同的类的名称 * ,该错误可能被错误地解释。既然你没有指定你的构造的保护级别,它被认为是内部默认情况下。声明构造公共将解决此问题:

This error is a result of the protection level of ClassB's constructor, not ClassB itself. Since the name of the constructor is the same as the name of the class* , the error may be interpreted incorrectly. Since you did not specify the protection level of your constructor, it is assumed to be internal by default. Declaring the constructor public will fix this problem:

public ClassB() { }

* 你也可以说,构造函数没有名字,只有一种;这不会改变问题的本质。

* One could also say that constructors have no name, only a type; this does not change the essence of the problem.

更多推荐

公共类无法访问由于其保护级别

本文发布于:2023-11-26 19:58:55,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1634979.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:于其   无法访问   级别

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!