为什么Window.top无法访问

时间:2017-08-08 20:22:06

标签: c#

通过C#教科书工作,对语言完全陌生,并且不知道如何修复此错误:我收到的错误如下: Window.top由于其保护级别而无法访问,这只发生在添加Button类之后,我对ListBox类没有任何问题:下面是我的代码:

using System;

public class Window {
    private int top;
    private int left;

    public Window(int top, int left) {
        this.top = top;
        this.left = left;
    }
    public virtual void DrawWindow() {
        Console.WriteLine("Drawing Window at {0}, {1}", top, left);
    }
}

public class ListBox : Window {
    private string mListBoxContents;
    public ListBox(int top, int left, string theContents): base(top, left) {
        mListBoxContents = theContents;
    }
    public override void DrawWindow() {
        base.DrawWindow();
        Console.WriteLine("Wrting string to the listbox: {0}", mListBoxContents);
    }
}

public class Button : Window {
    public Button(int top, int left): base(top, left) {
    }
        public override void DrawWindow() {
            base.DrawWindow();
            Console.WriteLine("Drawing a button at {0}, {1}", top, left);
        }
    }

public class WindowDriver {
    public static void Main() {

        Window w = new Window(5,10);
        ListBox lb = new ListBox(20,30,"Hello World!");
        Button b = new Button(5,6);

        Window[] winArray = new Window[3];
        winArray[0] = w;
        winArray[1] = lb;
        winArray[2] = b;

        for (int i = 0; i < 3; i++) {
            winArray[i].DrawWindow();
        }
    }
}

5 个答案:

答案 0 :(得分:0)

您可能尝试从派生类Button访问Window.top。子类无法访问其父类的私有变量。如果要从Button访问Window.top,则应将其访问说明符更改为internal或protected。

答案 1 :(得分:0)

问题是Button类正在尝试访问Window的私有成员。

public class Window {
    private int top;  //these two members are private which means only the containing class can access them.
    private int left;

    public Window(int top, int left) {
        this.top = top;
        this.left = left;
    }
    public virtual void DrawWindow() {
        Console.WriteLine("Drawing Window at {0}, {1}", top, left);
    }
}

public class Button : Window {
    public Button(int top, int left): base(top, left) {
    }

    public override void DrawWindow() {
        base.DrawWindow();
        Console.WriteLine("Drawing a button at {0}, {1}", top, left); // Here you are trying to access the private members of the base class which is not allowed.  This is what is causing your error
    }
}

解决问题的最简单方法是将top和left的声明更改为protected。派生类可以访问受保护的成员,例如

public class Window {
    protected int top;  //these two members are now protected private which means derived classes can access them.
    protected int left;  

    public Window(int top, int left) {
        this.top = top;
        this.left = left;
    }
    public virtual void DrawWindow() {
        Console.WriteLine("Drawing Window at {0}, {1}", top, left);
    }
}

如果您想了解有关c#访问修饰符的更多信息,请查看:C# Access Modifiers

答案 2 :(得分:0)

更改Window类中的行:

private int top;
private int left;

为:

protected int top;
protected int left;

答案 3 :(得分:0)

Console.WriteLine("Drawing a button at {0}, {1}", top, left);行正在尝试访问top基类中left的{​​{1}}和private字段。 Window访问修饰符阻止子类访问其父级字段。

如果您只想通过基类的构造函数设置字段,那么我将重构private以使用属性替换两个私有字段,您可以在其中为getter和getter决定访问修饰符设定器:

Window

现在,您将能够访问任何派生类中的public class Window { protected int Top { get; private set; } protected int Left { get; private set; } public Window (int top, int left) { Top = top; Left = left; } public virtual void DrawWindow() { Console.WriteLine("Drawing Window at {0}, {1}", Top, Left); } } Top属性:

Left

答案 4 :(得分:0)

好像你可能想要使用只读属性而不是实例变量:

public class Window 
{
    protected int Top { get; }
    protected int Left { get; }

    public Window(int top, int left) 
    {
        Top = top;
        Left = left;
    }

    // Rest of code...
}

然后,您可以从继承Window的任何类中访问它:

public class Button : Window
{
    public Button(int top, int left): base(top, left) 
    {
    }

    public override void DrawWindow() 
    {
        base.DrawWindow();
        Console.WriteLine("Drawing a button at {0}, {1}", Top, Left);
    }
}

C#中有不同的access modifiers

我在我的示例中使用了protected(仅表示类,并且从其继承的类可以访问该成员)。如果您需要可以从任何类访问该属性,请将修饰符更改为public

如果你想在最初设置之后改变Top / Left的值,那么你想要添加一个&#34; setter&#34;到你的财产:

protected int Top { get; set; }
相关问题