C#“由于其保护级别而无法访问”。构造函数中的错误 [英] C# "is inaccessible due to its protection level" error in constructor

查看:462
本文介绍了C#“由于其保护级别而无法访问”。构造函数中的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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;
        }

    }
}

- ---------------------------新建Edit ----------------

----------------------------- 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);
        }


推荐答案

定义派生类构造函数时出错。如果要获取超类的 name type 值,则必须将它们作为附加构造函数传递arguments(派生类构造函数中总共有4个参数。)例如,将其更改为这样即可:

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.

这篇关于C#“由于其保护级别而无法访问”。构造函数中的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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