c# - 在类中使用 ASP.Net Core 2.1 与 ASP.Net 4.1 MVC 的继承和实例化问题
问题描述
这两个类都在不同的类文件中,但在同一个文件夹中。这是我的代码。
public class Person
{
public String FirstName { get; set; }
public String LastName { get; set; }
public String Email { get; set; }
public DateTime DateOfBirth { get; set; }
}
public class Zodiac : Person
{
public Zodiac()
{
//person.DateOfBirth = DateTime.Now;
DateOfBirth = DateTime.Now;// this doesn't work either
}
//Person person = new Person();
}
即使我使用实例化,即使它们在同一个文件夹中也不起作用。它给了我这个错误。
严重性代码 描述 项目文件行抑制状态错误 CS0103 当前上下文中不存在名称“DateOfBirth” PersonExercise D:\Exercises\PersonExercise\PersonExercise\Models\Zodiac.cs 12 活动
如果我使用实例化,它会给我这个错误。
严重性代码描述项目文件行抑制状态错误 CS1061“Person”不包含“DateOfBirth”的定义,并且找不到接受“Person”类型的第一个参数的可访问扩展方法“DateOfBirth”(您是否缺少 using 指令或程序集参考?)PersonExercise D:\Exercises\PersonExercise\PersonExercise\Models\Zodiac.cs 12 Active
以下是有关该问题的屏幕截图:
解决方案
看起来像一些错字+不完全理解继承。检查以下代码中的注释。
public class Zodiac : Person
{
public Zodiac()
{
person.DateOfBirth = DateTime.Now; // This would not work, you have not declared an object named "person", note it is commented.
DateOfBirth = Datetime.Now;// this doesn't work. <-- Indeed, there is a typo
DateOfBirth = DateTime.Now;// this would work, note the uppercase Time.
}
//Person person = new Person();
}
推荐阅读
- javascript - 从下拉列表中选择一个值后 MVC 重新加载页面
- python-3.x - OptionError:“没有这样的键:'display.height'”
- python - 附加到 Pandas 多索引
- javascript - 使用 ESLint 防止在 IE11 中使用不支持的 JavaScript 功能?
- javascript - 对照片的 JavaScript/HTML 阴影框感到困惑
- kotlin - MutableList 映射扩展函数输出不可变列表
- sql - 在 SQL Server 2014 中使用 openrowset
- python - 如何使用 python regex 验证 hdfs 结果以确定它是文件夹、文件还是不存在
- regex - sas、正则表达式、数字、子字符串、prxchange
- apache - Firefox/chrome 显示 apache 请求被阻止/停止