C# 继承后无法返回新的类类型 公共类抽象动物 { //多领域 公共字符串名称{get;set;} 公共int id{get;set;} //方法,返回self 公共抽象动物getAnimalByID(int\u id) { //数据库连接 //从数据库获取数据并返回动物类型 return animal;
如果我想在第二类中使用base,则不能将第一类的方法声明为abstractC# 继承后无法返回新的类类型 公共类抽象动物 { //多领域 公共字符串名称{get;set;} 公共int id{get;set;} //方法,返回self 公共抽象动物getAnimalByID(int\u id) { //数据库连接 //从数据库获取数据并返回动物类型 return animal;,c#,abstract,base,C#,Abstract,Base,如果我想在第二类中使用base,则不能将第一类的方法声明为abstract 在我去掉摘要摘要和基础后,剩下的工作正常。这是两个没有上下文的毫无意义的标记。用你正在使用的语言标记你的问题。我已经编辑了你的标题。请参见“”,其中的共识是“不,他们不应该”。这是对你的问题的补充解释还是回答?如果是前者,请编辑你的问题。这是我找到的答案,但我不确定这是否是最佳答案。 public class abstract animal { // many fields public string
在我去掉摘要
摘要
和基础
后,剩下的工作正常。这是两个没有上下文的毫无意义的标记。用你正在使用的语言标记你的问题。我已经编辑了你的标题。请参见“”,其中的共识是“不,他们不应该”。这是对你的问题的补充解释还是回答?如果是前者,请编辑你的问题。这是我找到的答案,但我不确定这是否是最佳答案。
public class abstract animal
{
// many fields
public string Name {get;set;}
public int id {get; set;}
// method, return self
public abstract animal getAnimalByID( int _id)
{
// databse connection
//get data from database and return an animal type
return animal; <-- cannot work, "return this" can work
}
}
public class dog : animal
{
public override dog getAnimalByID( int _id)
{
return (dog )base.getAnimalByID( _id);
}
}