C#中的类和基类继承 [英] Classes and base class inheritance in C#
问题描述
我有一个C#类,如下所示:
I have a class in C# like so:
public class BarChart
{
public BarData BarChartData;
public BarStyle BarChartStyle;
public BarChart(BarData data, BarStyle style)
{
this.BarChartData = data;
this.BarChartStyle = style;
}
string _uniqueName;
public string UniqueName
{
get { return this._uniqueName; }
set { this._uniqueName = value; }
}
string _rowNumber;
public string RowNumber
{
get { return this._rowNumber; }
set { this._rowNumber = value; }
}
我想创建一个名为的图表
将包含BarChart类具有的所有属性。例如:
I want to create a class called Chart
that will have all of the properties that BarChart class has. For example:
Chart someChart = new Chart(BarChart);
string name = someChart.UniqueName
我对C#和继承的概念比较陌生对我来说有点陌生。在一天结束时,我将有多种不同的图表类型,如LineChart,BarChart等,但我也希望能够移动它们并对它们进行排序:
I am relatively new to C# and the concept of inheritance is a little bit foreign to me. At the end of the day I will have multiple different chart types like LineChart, BarChart, etc., but I also want to be able to move them around and sort them like so:
List<Chart> groupedCharts = listOfCharts
.GroupBy(u => u.RowNumber)
.Select(grp => grp.ToList())
.ToList();
...因此想把它们扔进通用图表
类,便于使用 LINQ 。
...hence the idea to throw them into generic Chart
class for easy use with LINQ.
我该如何设置呢?
推荐答案
创建一个抽象的Chart类:
Create an abstract Chart class:
abstract class Chart
{
// Shared properties
}
然后继承它:
class BarChart : Chart
{
// Bar chart properties
}
然后您可以将其创建为:
Then you can create it as:
Chart someChart = new BarChart();
这篇关于C#中的类和基类继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!