继承 - 运动

Ric*_*ter 1 c#

好吧,所以我正在学习继承,我做了一些事情,这是代码:

class array
{
    int [] arr;
    public array(int[] arr)
    {
        this.arr = arr;
    }
    public int Biggest()
    {
        return arr.Max();
    }
    public int Min()
    {
        return arr.Min();
    }
    public int SumArr()
    {
        return arr.Sum();
    }
}
class array2 : array
{
    public array2(int [] arr):base(arr)
    {
    }
    public double Average()
    {
        return 
    }
}
Run Code Online (Sandbox Code Playgroud)

现在,在派生类中我需要得到数组的平均值而我不能做arr.Average()错误说:Error 1 'ConsoleApplication1.array.arr' is inaccessible due to its protection level C:\Users\x\AppData\Local\Temporary Projects\ConsoleApplication1\Program.cs 35 20 ConsoleApplication1 任何人都可以告诉我我做错了什么?谢谢你的帮助!

Tro*_*yen 6

由于您未指定可见性类型,因此将arr声明为私有.如果将其更改为protected,那么您的子类array2将能够访问它.

protected int [] arr;
Run Code Online (Sandbox Code Playgroud)