简体   繁体   English

C# OOP 实践混淆 inheritance

[英]C# OOP practice confusion regarding inheritance

I am reading one of my C# programming course material.我正在阅读我的 C# 编程课程材料之一。 I understand the code itself, but I don't quite understand why the tutor organises the class this way.我理解代码本身,但我不太明白为什么导师会这样组织class。

namespace GradeBook
{



public delegate void GradeAddedDelegate(object sender, EventArgs args);
public class NamedObject 
{
    public NamedObject(string name)
    {
        Name = name;
    }

    public string Name
    {
        get;
        set;
    }
}

public interface IBook
{
    void AddGrade(double grade);
    Statistics GetStatistics();
    string Name { get; }
    event GradeAddedDelegate GradeAdded;
}

public abstract class Book : NamedObject, IBook
{
    public Book(string name) : base(name)
    {
    }

    public abstract event GradeAddedDelegate GradeAdded;
    public abstract void AddGrade(double grade);
    public abstract Statistics GetStatistics();
}

public class DiskBook : Book
{
    public DiskBook(string name) : base(name)
    {
    }

    public override event GradeAddedDelegate GradeAdded;

    public override void AddGrade(double grade)
    {
        using(var writer = File.AppendText($"{Name}.txt"))
        {                
            writer.WriteLine(grade);
            if(GradeAdded != null)
            {
                GradeAdded(this, new EventArgs());
            }
        }
    }

    public override Statistics GetStatistics()
    {
        var result = new Statistics();

        using(var reader = File.OpenText($"{Name}.txt"))
        {
            var line = reader.ReadLine();
            while(line != null)
            {
                var number = double.Parse(line);
                result.Add(number);
                line = reader.ReadLine();
            }
        }

        return result;
    }
}

The code snippet above is a GradeBook for normal students.上面的代码片段是普通学生的成绩簿。

  1. class NameObject defines a property Name and an initializer, which is easy to understand. class NameObject定义了一个属性Name和一个初始化器,很容易理解。
  2. He put all method in an interface IBook , and there is another class called Book , which is inherited from NameObject class and interface IBook .他把所有方法放在一个接口IBook中,还有一个 class 叫Book ,它继承自NameObject class 和接口IBook I am assuming he deliberately separate properties and methods, event.我假设他故意将属性和方法、事件分开。 Is this correct?这个对吗?
  3. In the Book class, the initializer public Book(string name): base(name) .Book class 中,初始化程序public Book(string name): base(name) I know the base is defining a reference type.我知道基础定义了一个引用类型。 I am getting a bit confused with the name parameter.我对name参数有点困惑。 Does it refer to the property Name in NameObject ?它是否引用NameObject中的属性Name
  4. DiskBook is a subclass of Book, the initializer public Book(string name): base(name) is passing a reference name as the last one. DiskBook 是 Book 的子类,初始化器public Book(string name): base(name)将引用name作为最后一个传递。 Does the name here refers to the name property in NameObject as well?这里的name是否也指 NameObject 中的name属性?

Thanks in advance.提前致谢。

  1. Logically, a lot of things could be a NamedObject .从逻辑上讲,很多东西都可能是NamedObject He's saying a Book is an IBook , and also a NamedObject .他说IBook Book也是NamedObject Imagine adding a Person class.想象一下添加一个Person class。 That Person would be a NamedObject , but not an IBook .Person将是NamedObject ,但不是IBook So you can reuse the base class without tying it to a Book因此,您可以重用基础 class 而无需将其绑定到Book

  2. using : base(name) says call the constructor on the base class with this parameter . using : base(name)表示call the constructor on the base class with this parameter So you can be sure that NamedObject 's constructor will be called with the string value, setting the Name property.因此,您可以确定将使用字符串值调用NamedObject的构造函数,并设置Name属性。 Excluding the : base(name) from the Book constructor will cause a compiler error, as the base class does not have a parameterless constructor.Book构造函数中排除: base(name)将导致编译器错误,因为基础 class 没有无参数构造函数。

  3. In the same manner, DiskBook is using : base(name) to call the constructor on Book , which in turn calls the constructor on NamedObject .同样, DiskBook使用: base(name)调用 Book 上的构造函数,而Book又调用NamedObject上的构造函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM