gpt4 book ai didi

c# - 继承-锻炼

转载 作者:太空宇宙 更新时间:2023-11-03 20:32:58 26 4
gpt4 key购买 nike

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

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

现在,在派生类中,我需要获取数组的平均值,但我做不到 arr.Average()错误提示:Error 1 'ConsoleApplication1.array.arr' is unaccessible due to its protection level C:\Users\x\AppData\Local\Temporary Projects\ConsoleApplication1\Program.cs 35 20 ConsoleApplication1
谁能告诉我我做错了什么?感谢各位 helper !

最佳答案

arr 被声明为私有(private)的,因为您没有指定可见性类型。如果您将其更改为 protected ,那么您的子类 array2 将能够访问它。

protected int [] arr;

关于c# - 继承-锻炼,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6517143/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com