有没有办法实现一个抽象类,并用注释更改方法的模板?像这样:
public abstract class A {
/// <template>
/// return string.Empty;
/// </template>
abstract public string getSomething();
}
所以,当你&#34;实现抽象类&#34;使用visual studio,您将获得一个返回空字符串的方法,而不是抛出NotImplementedException的方法。
public class B : A {
public string getSomething() {
return string.Empty;
}
}
我不想更改IDE的模板,因为并非所有方法都必须返回相同的
答案 0 :(得分:1)
您可以使用虚方法来允许您可以在派生类中覆盖的默认实现。
public abstract class A {
//virtual provides default implementation
public virtual string getSomething() {
return string.Empty;
}
}
public class B : A {
//no need to implement getSomething() it will default to base class (string.Empty)
}
public class C : A {
//we can override the default implementation of getSomething()
public override string getSomething() {
return "Not empty!";
}
}