关于遗产的新手问题 [英] Novice question about inheritence
问题描述
嗨
我刚开始学习C#而我正试图绕过遗产。
我也在做我最好不要重复代码以便于维护,所以如果我需要更新一个功能,那么我只需要在一个地方这样做。
现在我的问题。
我一直在玩遗产来实现所提到的项目,同时也保持课程自我遏制。
就像你在大多数地方使用ToString()一样,我想要一个DisplayInfo()函数,但是再次只定义一次。
我尝试过以下方法:
Hi
I am just beginning to learn C# and I''m trying to wrap my head around inheritence.
I''m also doing my best not to repeat code in order to make it easy to maintain so if I need to update a function then I only need to do so in one place.
Now to my problem.
I''ve been playing around with inheritence to achieve the items mentioned earler whilst also keeping classes self containted.
Just like to you use "ToString()" in most places I''d like to have a "DisplayInfo()" function but again, only defined once.
I''ve tried the following:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace InheritenceTest
{
class Program
{
static void Main(string[] args)
{
Hardware MyHardware = new Hardware();
MyHardware.DisplayInfo();
BIOS MyBIOS = new BIOS();
MyBIOS.DisplayInfo();
Console.ReadKey();
}
}
class Hardware
{
public string classHeader;
public Hardware()
{
classHeader = "This is the Hardware class";
}
public void DisplayInfo()
{
Console.WriteLine(classHeader);
}
}
class BIOS : Hardware
{
public string classHeader;
public BIOS()
{
classHeader = "This is the BIOS class";
}
}
}
问题是基类中定义的函数将只使用基类中的属性classHeader。
我认为子类中的classHeader属性会覆盖基类,但似乎不会发生。
所以我该如何实现:
- 只定义一次功能
- 制作功能适用于所有实例
- 使功能显示当前实例/类的属性
再次,我是新手如果我没有使用正确的条款,那么竞技场对我很光。
问候
理查德
The problem is that the function defined in the base class will only use the property "classHeader" in the base class.
I thought that the "classHeader" property in the subclass would override the base class but that does not seem to happen.
So how can I achieve:
- defining the function only once
- make the function available for all instances
- make the function display the properties for the current instance/class
Again, I''m a novice in this arena so bare with me if I''m not using the correct terms.
Regards
Richard
推荐答案
- 只定义一次函数
- 使函数可用对于所有实例
- defining the function only once
- make the function available for all instances
您实际上可以通过继承来实现。
You may actually achieve that with inheritance.
- 使该函数显示当前实例/类的属性
- make the function display the properties for the current instance/class
这会打破你的第一个要求。
通过继承你可以编写 incremental 代码:在基类中定义公共行为并在派生类中扩展这种行为。
This would break your very first requirement.
With inheritance you can write incremental code: define common behaviour in the base class and extend such a behaviour in the derived ones.
如果你编译它,你会得到一个警告说''BIOS.classHeader''隐藏继承成员''Hardware.classHeader''。如果要隐藏,请使用new关键字。如果您使用新的它将正常运行但不使用单一方法。 这给你一个线索。
如果你从子类中删除以下行BIOS
您的代码功能正如您所期望的那样:
If you compile this you get a warning saying that "''BIOS.classHeader'' hides inherited member ''Hardware.classHeader''. Use the new keyword if hiding was intended. If you use the new it will function correctly but not use a single method. " which gives you a clue.
If you remove the following line from the subclassBIOS
your code functions as you''d expect:
public string classHeader;
在原始代码中(上面有声明) BIOS中的
隐藏 classHeaderPropety
硬件
类中的基础 classHeader
。作为 DisplayInfo
硬件类中定义的,它使用基础中的
不是将其隐藏在 classHeader
硬件 BIOS
中的方法,该方法不知道,为您提供结果。
为了帮助解释(不是实际用途!),请尝试更改原始 BIOS
类所以它有一个辅助方法直接写出显示,通过超类,例如这个:
In your original code (with the above declaration in place) the classHeaderPropety
in BIOS
hides the underlying classHeader
in the Hardware
class. As the DisplayInfo
method in your code is defined in the Hardware
class it uses the classHeader
from the base Hardware
not the one hiding it in BIOS
which the method is "unaware" of, giving you your results.
To help explain (not for real use!), try changing your original BIOS
class so it has a secondary method that writes out the display directly, nit via the superclass, e.g. to this:
class BIOS : Hardware
{
public string classHeader;
public BIOS()
{
classHeader = "This is the BIOS class";
}
public void DisplayInfoBIOS()
{
Console.WriteLine(classHeader);
}
}
并将此行添加到您的主要功能之前读取密钥:
and adding this line to your main function before the readkey:
MyBIOS.DisplayInfoBIOS();
由于新的 DisplayInfoBIOS
是在 BIOS
类中定义的,所以使用 BIOS
的 classHeader
版本,然后获得BIOS文本。
作为说服自己的最后练习,请尝试从以下位置更改新的 DisplayInfoBIOS
:
As the new DisplayInfoBIOS
is defined in the BIOS
class, it uses the BIOS
''s version of classHeader
, and you get the BIOS text.
As as final exercise to convince yourself, try changing the new DisplayInfoBIOS
from:
Console.WriteLine(classHeader);
到
Console.WriteLine(base.classHeader);
并尝试预测结果。
显然要做的事情是删除我上面提到的那一行,以便超级和子类访问相同的成员而不是在以下方法中破解新方法:)
and try and predict the result.
Obviously the thing to do is remove the line I mentioned above so both super and sub classes access the same member rather than hacking an new method in :)
变量classHeader
将由硬件
类和仅从其继承的类使用。因此它不应该是public
,而是protected
。protected
类似于private
,但允许派生类访问基类的变量/属性/函数。
在派生类中,删除变量的声明 - 您现在可以访问基类的变量。
另一个技巧:您可能想要改变行为派生类中的属性/函数。然后建议在基类中添加virtual
关键字,例如public virtual void DisplayInfo()
。在派生类中,您可以使用覆盖
关键字进行更改,例如
The variableclassHeader
is to be used by theHardware
class and classes inheriting from it only. Hence it should not bepublic
, butprotected
.protected
is similar toprivate
, but allows for derived classes to access that variable/property/function of the base class.
In the derived class, remove the declaration of the variable - you can now access the variable of the base class.
And another trick: you might want to change the behavior of a property/function in a derived class. Then it is advisable to add thevirtual
keyword in the base class, e.g.public virtual void DisplayInfo()
. In the derived class, you can change it with theoverride
keyword, e.g.
public override void DisplayInfo()
{
Console.WriteLine("DisplayInfo() called from the derived class");
base.DisplayInfo();
}
只需玩这些东西,看一下结果。
Just play with these things, and look at the results.
这篇关于关于遗产的新手问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!