C# Return";这";来自超类C的子类的#
我需要从超类返回“this”或子类实例C# Return";这";来自超类C的子类的#,c#,oop,C#,Oop,我需要从超类返回“this”或子类实例 interface IA { IA Format(); void Print(); } interface IB { IA Format(); void Print(); void PrintB(); } abstract class A : IA { protected bool isFormated; public IA Format() { isFormated = tr
interface IA
{
IA Format();
void Print();
}
interface IB
{
IA Format();
void Print();
void PrintB();
}
abstract class A : IA
{
protected bool isFormated;
public IA Format()
{
isFormated = true;
return this;
}
virtual public void Print()
{
Console.WriteLine("this is A");
}
}
class B : A, IB
{
override public void Print()
{
Console.WriteLine("this is B");
}
public void PrintB()
{
if (isFormated)
{
Console.WriteLine("this is formated B");
}
else
{
Console.WriteLine("this is B");
}
}
}
class Program
{
static void Main(string[] args)
{
var x = new B();
x.Format().PrintB();
}
}
我有两个类,类A是超类,类B是从A继承的子类。
实现接口A和B的这两个类
我需要调用'x.Format().PrintB();'只是格式化字符串
换句话说,我需要在Format()函数中返回相同的对象,并根据Format()中的更改来更改PrintB行为
因此,如果我创建了新的类D并继承了一个,那么我也希望基于isFormatted以不同的行为实现PrintD。当您调用x.Next()
时,它返回一个实现IA
的实例。这可能是A
、B
,或者实现IA
的任何其他类,但由于它返回IA
,其他类不知道具体类型是什么。这通常是故意的。如果其他类不知道IA
的实现是什么,那么您可以用另一个替换一个
但是,由于Next
返回IA
,因此调用PrintB
的唯一方法是IA
具有PrintB
方法,如下所示:
public interface IA
{
void PrintB();
}
abstract class A : IA
{
public IA Next()
{
return this;
}
virtual public void Print()
{
Console.WriteLine("this is A");
}
public abstract void PrintB();
}
如果A
实现IA
,则它必须具有PrintB
方法。但是因为它是一个抽象类,所以方法可以是抽象的。这意味着A
并没有真正实现该方法。实现它需要一个从A
继承的非抽象类
看起来是这样的:
public interface IA
{
void PrintB();
}
abstract class A : IA
{
public IA Next()
{
return this;
}
virtual public void Print()
{
Console.WriteLine("this is A");
}
public abstract void PrintB();
}
PrintB
不必是抽象的<代码>一个
可以实现它。它可以是虚拟的,其他类可以覆盖它。或者它既不是抽象的,也不是虚拟的,其他类无法覆盖它
现在,当您声明B:A
时,编译器将需要B
来实现PrintB
然后您可以调用
Next().PrintB()
,因为Next()
返回IA
,IA
有一个PrintB
方法。我将a作为泛型类,并将其作为T返回
interface IA<T> where T : class
{
T Format { get; }
void Print();
}
abstract class A<T> : IA<T> where T : class
{
protected bool isFormated;
public T Format
{
get
{
isFormated = true;
return this as T;
}
}
virtual public void Print()
{
Console.WriteLine("this is A");
}
}
interface IB
{
void Print();
void PrintB();
}
class B : A<B>, IB
{
override public void Print()
{
Console.WriteLine("this is B");
}
public void PrintB()
{
if (isFormated)
{
Console.WriteLine("this is formated B");
}
else
{
Console.WriteLine("this is B");
}
}
}
class Program
{
static void Main(string[] args)
{
var x = new B();
x.Format.PrintB();
}
}
接口IA,其中T:class
{
T格式{get;}
作废打印();
}
抽象类A:IA,其中T:class
{
受保护的bool被格式化;
公共T格式
{
得到
{
isformatted=true;
将此作为T返回;
}
}
虚拟公共作废打印()
{
Console.WriteLine(“这是一个”);
}
}
接口IB
{
作废打印();
void PrintB();
}
B类:A、IB
{
覆盖公共作废打印()
{
Console.WriteLine(“这是B”);
}
公共无效打印B()
{
如果(已格式化)
{
Console.WriteLine(“这是格式化的B”);
}
其他的
{
Console.WriteLine(“这是B”);
}
}
}
班级计划
{
静态void Main(字符串[]参数)
{
var x=新的B();
x、 Format.PrintB();
}
}
您正在返回B
的实例-这里只有一个实例-但键入为IA
,因此它没有PrintB
方法。你能解释一下你到底想要实现什么吗?我试图返回B的“this”,但我的代码中缺少了一些东西。我是否也应该在子类中实现“Next()”?IA和IB之间是否存在任何关系?当您已经将实例存储在x
中时,为什么需要使用this
?@Svarr我有这个用例,但我试图简化示例。谢谢您的回答,我已经更新了问题并更改了示例。这种情况下,我需要在Format()函数中返回相同的对象,并且根据Format()中的更改,我需要更改PrintB行为。因此,如果我创建了新的类D并继承了一个类,我想实现PrintDIt,看起来您已经完成了大部分工作。一些建议。如果您确实需要这两个接口,也许其中一个可以从另一个继承。这样一来,IB
只声明了一个新方法,而不是IA
中的所有方法。接下来,我想知道x.Format().PrintB()有多复杂如果Format()
不返回任何内容,而您只需执行x.Format(),则代码>一半的问题可能会消失;x、 PrintB()代码>只是事后想一想-没什么大不了的-如果您使用稍微大一点的类名,它会有所帮助。在现实生活中,它们往往不止一个字母,因此A
和B
看起来可能有点混乱。这就是为什么很多人使用Foo和Bar,Car和Truck,或者别的什么。对此我很抱歉,我尽量让这个例子简单。但是,我已经找到了解决方案,请检查更新。