C#“由于其保护级别而无法访问”构造函数中的错误

发布于 2024-10-23 20:58:38 字数 2419 浏览 6 评论 0原文

子类“caesar”的构造函数给出错误。它说名称、类型由于其保护级别而无法访问。怎么会?由于这是从“Cipher”类派生的子类,因此它不应该给出这样的错误。我该如何克服这种情况。但我希望这些变量是私有的。我不想将它们更改为公开的。

***第二个代码示例有效。有人能看出区别吗?

namespace Encrypter
{
    class Cipher
    {
        public Cipher(string name, string type)
        {
            setName(name);
            setType(type);

        }
        private string name;
        private string type;

        public void setName(string newName)
        {
            name = newName;
        }
        public string getName()
        {
            return name;
        }
        public void setType(string newType)
        {
            type = newType;
        }
        public string getType()
        {
            return type;
        }
        public string encrypt(string text)
        {
            return text;
        }
        public string decrypt(string text)
        {
            return text;
        }
    }
}




namespace Encrypter
{
    class Caesar : Cipher
    {

        private int shiftamount;
        private string shiftdirection;
        public Caesar(int shiftamount, string shiftdirection) : base(name, type)
        {
            setShiftamount(shiftamount);
            setShiftdirection(shiftdirection);
        }
        public void setShiftamount(int newShiftamount)
        {
            shiftamount = newShiftamount;
        }
        public int getShiftamount()
        {
            return shiftamount;
        }
        public void setShiftdirection(string newShiftdirection)
        {
            shiftdirection = newShiftdirection;
        }
        public string getShiftdirection()
        {
            return shiftdirection;
        }

    }
}

-------------------------------------- 新编辑 ----------------

class MyFile
    {
        public MyFile(int id, string name, int size, string type)
        {
            setId(id);
            setName(name);
            setSize(size);
            setType(type);

        }
        private int id;
        private string name;
        private string type;
        private int size;




class Movie : MyFile
    {
        private string director;
        private int release_year;
        public Movie(string director, int release_year, int id, string name, int size) : base( id,  name,  size, "m")
        {
            setDirector(director);
            setRelease_year(release_year);
        }

The constructor of the child class "caesar" gives an error. It says that name, type is inaccessible due to its protection level. How come? As this is a child class derived from "Cipher" class it shouldn't give an error like this. How can I overcome this situation. But I want those variables to be private. I don't want to change them as public.

***The second code example works. Can anybody see a difference?

namespace Encrypter
{
    class Cipher
    {
        public Cipher(string name, string type)
        {
            setName(name);
            setType(type);

        }
        private string name;
        private string type;

        public void setName(string newName)
        {
            name = newName;
        }
        public string getName()
        {
            return name;
        }
        public void setType(string newType)
        {
            type = newType;
        }
        public string getType()
        {
            return type;
        }
        public string encrypt(string text)
        {
            return text;
        }
        public string decrypt(string text)
        {
            return text;
        }
    }
}




namespace Encrypter
{
    class Caesar : Cipher
    {

        private int shiftamount;
        private string shiftdirection;
        public Caesar(int shiftamount, string shiftdirection) : base(name, type)
        {
            setShiftamount(shiftamount);
            setShiftdirection(shiftdirection);
        }
        public void setShiftamount(int newShiftamount)
        {
            shiftamount = newShiftamount;
        }
        public int getShiftamount()
        {
            return shiftamount;
        }
        public void setShiftdirection(string newShiftdirection)
        {
            shiftdirection = newShiftdirection;
        }
        public string getShiftdirection()
        {
            return shiftdirection;
        }

    }
}

----------------------------- New Edit ----------------

class MyFile
    {
        public MyFile(int id, string name, int size, string type)
        {
            setId(id);
            setName(name);
            setSize(size);
            setType(type);

        }
        private int id;
        private string name;
        private string type;
        private int size;




class Movie : MyFile
    {
        private string director;
        private int release_year;
        public Movie(string director, int release_year, int id, string name, int size) : base( id,  name,  size, "m")
        {
            setDirector(director);
            setRelease_year(release_year);
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

篱下浅笙歌 2024-10-30 20:58:38

看起来您在定义派生类构造函数时犯了一个错误。如果您想将 nametype 值传递给超类,则必须将它们作为附加构造函数参数传递(派生类中总共有 4 个参数)构造函数。)例如,将其更改为这样应该可行:

    public Caesar(int shiftamount, 
                  string shiftdirection, 
                  string name, 
                  string type) 
                  : base(name, type)

您可以采取许多其他策略。

It looks like you have made a mistake in defining the derived class constructor. If you want to get name and type values to the superclass, you'll have to pass them in as additional constructor arguments (for a total of 4 arguments in the derived class constructor.) For example, changing it to this should work:

    public Caesar(int shiftamount, 
                  string shiftdirection, 
                  string name, 
                  string type) 
                  : base(name, type)

There are a number of other strategies you could take.

时光倒影 2024-10-30 20:58:38

无法从派生类访问私有成员。受保护和公共可以。你必须保护它们。这样,只有该类及其“子级”才能访问。

访问权限摘要:

  • private:只能从该类方法访问,
  • protected:不能从该类及其子方法访问
  • internal:< /code> 只能从同一程序集中的方法访问
  • protectedinternal: 与来自其他程序集的派生类的内部 + 方法相同
  • public: 可以被所有人访问

Private members cannot be accessed from derived classes. protected and public can. You must make them protected. This way, only the class and its "children" will have access.

Summary of access rights:

  • private: can be accessed only from that class methods, nothing else
  • protected: can be accessed from that class's and its children's methods
  • internal: can be accessed only from methods within the same assembly
  • protected internal: same as internal + methods of derived classes from other assemblies
  • public: can be accessed by everyone
护你周全 2024-10-30 20:58:38
    public Caesar(int shiftamount, string shiftdirection)
        : base(name, type)
    {

问题是 private 字段名称和类型 - 子类无法访问它们,除非它们被标记为 protected。我怀疑你真正想要的是

    public Caesar(int shiftamount, string shiftdirection)
        : base("Caesar5", "Caesar")
    {
    public Caesar(int shiftamount, string shiftdirection)
        : base(name, type)
    {

The problem is the private fields name and type - the child class cannot access them unless they are marked as protected. What your really want I suspect is

    public Caesar(int shiftamount, string shiftdirection)
        : base("Caesar5", "Caesar")
    {
祁梦 2024-10-30 20:58:38

private 意味着只有声明类可以访问成员(这意味着继承类型也不能)。

protected 意味着声明类和任何后代都可以访问成员,但这些成员之外的类型则不能。

另一方面,.NET 语言中通常不使用 getter 和 setter 函数。属性封装了此功能,并且是应该使用的属性。例如;

private string name;

public string Name
{
    get { return name; }
    set { name = value; }
}

private means that only the declaring class can access the members (meaning that inherited types cannot, either).

protected means that the declaring class and any descendants can access the members, but types outside of those cannot.

On a different note, getter and setter functions are generally not used in .NET languages. Properties encapsulate this functionality and are what should be used instead. For example;

private string name;

public string Name
{
    get { return name; }
    set { name = value; }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文