我有两个名为CalIntEvent和CalExtEvent的子类,它们有两个名为custId和roomNumber的属性。这些类继承自CalEvent。
我有以下代码但无法访问派生类属性。我如何访问这些?
CalEvent newEvent;
while (userAns != '1' && userAns != '2')
{
Console.WriteLine("\nPlease enter 1 for Internal Event or 2 for External Event: ");
userAns = Console.ReadKey().KeyChar;
}
if (userAns == '1')
{
newEvent = new CalIntEvent();
}
else
{
newEvent = new CalExtEvent();
newEvent.custId = 2;// issue is here<<<<<<
}
newEvent.location = GetUserInput(Field.Location, "Please enter a location");
newEvent.title = GetUserInput(Field.Title, "Please enter a title");
newEvent.description = GetUserInput(Field.Description, "Please enter a description");
答案 0 :(得分:2)
由于custId
上定义了CalExtEvent
,因此您无法从CalEvent
类型的变量中访问它。
您可以做的是在构建CalExtEvent
时初始化该属性。这是一个例子:
newEvent = new CalExtEvent() { custId = 2};