[英]C# - loading inherited class
I have a class called FieldDesc
. 我有一个叫做
FieldDesc
的类。
public class FieldDesc {
public FieldDesc() {
}
}
I also have another class that inherits from FieldDesc
called StandardHoursByCommunitySvcType
. 我还有另一个从
FieldDesc
继承的类,称为StandardHoursByCommunitySvcType
。
public class StandardHoursByCommunitySvcType: FieldDesc {
public StandardHoursByCommunitySvcType() {
}
}
In my control I have - 在我的控制下-
FieldDesc aTable;
aTable = new FieldDesc();
String TableName = "StandardHoursByCommunitySvcType";
What do I have to do to get aTable
to know that it is an object of type StandardHoursByCommunitySvcType
? 我该怎么做才能使
aTable
知道它是StandardHoursByCommunitySvcType
类型的对象?
Your question is unclear. 您的问题不清楚。 Are you trying to declare aTable as a StandardHoursByCommunitySvcType or trying to determine if it has been declared as one?
您是要声明aTable为StandardHoursByCommunitySvcType还是要确定它是否已被声明为一个?
If you're trying to declare: 如果您要声明:
FieldDesc aTable;
aTable = new StandardHoursByCommunitySvcType();
That'll work as long as StandardHoursByCommunitySvcType inherits from FieldDesc 只要StandardHoursByCommunitySvcType从FieldDesc继承就可以
If you're trying to determine type: 如果您要确定类型:
if(aTable is StandardHoursByCommunitySvcType)
{
//Do something
}
You can use is operator to find this out 您可以使用is运算符找出答案
if(someObject is StandardHoursByCommunitySvcType )
{
//it means is is object of StandardHoursByCommunitySvcType type
}
If you have the two classes 如果你有两个班级
public class FieldDesc
{
public FieldDesc()
{
}
public void A()
{
}
public virtual void V()
{
Console.WriteLine("V from FieldDesc");
}
}
public class StandardHoursByCommunitySvcType : FieldDesc
{
public StandardHoursByCommunitySvcType()
{
}
public void B()
{
}
public overrides void V()
{
Console.WriteLine("V from StandardHoursByCommunitySvcType");
}
}
You can do this 你可以这样做
FieldDesc fd = new StandardHoursByCommunitySvcType();
StandardHoursByCommunitySvcType svc = new StandardHoursByCommunitySvcType();
fd.A(); // OK
fd.B(); // Fails (does not compile)
((StandardHoursByCommunitySvcType)fd).B(); // OK
fd.V(); // OK, prints "V from StandardHoursByCommunitySvcType"
svc.A(); // OK
svc.B(); // OK
svc.V(); // OK, prints "V from StandardHoursByCommunitySvcType"
The derived class is assignment compatible to the base class; 派生类与基类的分配兼容; however, accessed through a variable typed as the base class you will only see the the members of the base class.
但是,通过类型为基类的变量访问时,您只会看到基类的成员。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.