[英]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.上面的代码片段是普通学生的成绩簿。
NameObject
defines a property Name
and an initializer, which is easy to understand. NameObject
定义了一个属性Name
和一个初始化器,很容易理解。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.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.name
parameter.name
参数有点困惑。 Does it refer to the property Name
in NameObject
?NameObject
中的属性Name
?public Book(string name): base(name)
is passing a reference name
as the last one. 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.提前致谢。
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
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 没有无参数构造函数。
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.