C#中的类和基类继承

kon*_*rad 9 c# inheritance

我有一个C#类,如下所示:

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; }
    }
Run Code Online (Sandbox Code Playgroud)

我想创建一个名为Chart具有BarChart类所具有的所有属性的类.例如:

Chart someChart = new Chart(BarChart);
string name = someChart.UniqueName
Run Code Online (Sandbox Code Playgroud)

我对C#比较陌生,继承的概念对我来说有点陌生.在一天结束时,我将有多种不同的图表类型,如LineChart,BarChart等,但我也希望能够移动它们并按​​如下方式对它们进行排序:

List<Chart> groupedCharts = listOfCharts
.GroupBy(u => u.RowNumber)
.Select(grp => grp.ToList())
.ToList();
Run Code Online (Sandbox Code Playgroud)

...因此想把它们放到泛型Chart类中以便于LINQ使用.

我该如何设置呢?

Mar*_*ica 16

创建一个抽象的Chart类:

abstract class Chart
{
    // Shared properties
}
Run Code Online (Sandbox Code Playgroud)

然后继承它:

class BarChart : Chart
{
    // Bar chart properties
}
Run Code Online (Sandbox Code Playgroud)

然后你可以创建它:

Chart someChart = new BarChart();
Run Code Online (Sandbox Code Playgroud)

  • 如果没有类型(线条,条形图,饼图形),它对我来说看起来很抽象,尽管它与问题并不严格相关. (3认同)
  • 我改变了我的答案,我更喜欢一个抽象类的接口.我的个人意见. (2认同)
  • @DavidPine我个人不喜欢选择界面,因为它不允许使用字段,如果你在界面中指定属性,它会强制你"实现"它们,大部分时间都会违反DRY (2认同)

Dav*_*ine 10

你需要创建一个这样的界面:

    public interface IChart
    {
        string UniqueName { get; set; }

        string RowNumber { get; set; }
    }
Run Code Online (Sandbox Code Playgroud)

然后让其他类继承基类...

    public class BarChart : IChart
    {
        public BarData BarChartData { get; private set; }
        public BarStyle BarChartStyle { get; private set; }

        // Other custom members you desire for your bad chart implementation

        public BarChart(BarData data, BarStyle style)
        {
            BarChartData = data;
            BarChartStyle = style;
        }
    }
Run Code Online (Sandbox Code Playgroud)

MSDN示例在此处详述.就个人而言,我会避免使用抽象类,直到您确定所有可以封装的图表都有真正的通用逻辑.没有理由现在过度设计,只需使用界面.

我希望这是有帮助的!


Dan*_*one 5

您可能不希望Chart实例化您的基类,因为它的功能不伦不类,因此您希望它是一个抽象类。

public abstract class Chart
{
    // Public properties common to all charts
    public ChartData data;
    public ChartStyle style;
    public string RowNumber { get; set; }
    public string UniqueName { get; set; }

    // A common method    
    public void AddDataPoint () {}

    // A method all charts have that may change between different types of charts
    public virtual void DrawChart () {}

    // Constructor
    public Chart (ChartData cd, ChartStyle cs)
    {
        data = cd;
        style = cs;
    }

    // Protected method (like a private method, but gets inherited)
    protected void Finalize () {}
}
Run Code Online (Sandbox Code Playgroud)

您希望继承类看起来像这样:

public class BarChart : Chart
{
    // BarChart exclusive properties

    // A method all charts have that BarCharts implements differently
    public override void DrawChart () {}

    // Constructor that calls the base constructor
    public BarChart (ChartData cd, ChartStyle cs) : base (cd, cs)
    {

    }
}
Run Code Online (Sandbox Code Playgroud)

在方法上,您需要确保使用virtualoverride关键字,以便子类的方法可以覆盖对基类方法的调用。

抽象类与接口

与接口不同,抽象类允许您在其中定义方法。接口中的方法都只是签名。在抽象类中,您还可以拥有仅定义其签名的抽象方法。不过,抽象类在继承中的工作方式与常规类类似;你只能继承一个。您可以继承任意多个接口。如果您想BarChart继承Chart一个像 的接口IComparable,您可以首先使用抽象类来声明该类,例如public class BarChart : Chart, IComparable