简体   繁体   English

OOP接口和基类

[英]OOP interface and base class

This is just for my own knowledge. 这只是我的知识。 If a base class "A" implements an interface "I" would any derived classes of A (let's say B) also "is an I" type? 如果基类“ A”实现接口“ I”,则A的任何派生类(比如B)也将是“ I”类型吗? In other words "is" keyword returns true? 换句话说,“ is”关键字返回true吗? How about class C that is derived from B? 从B派生的C类怎么样?

The question is, once you implement an interface in a base class, is that class (and derived classes) stuck being of that interface type? 问题是,在基类中实现接口后,该类(和派生类)是否卡在该接口类型中? Any way to remove it so "is" return false? 有什么办法可以删除它,以便“ is”返回false?

Yes, all derived classes are of type of all their base ancestry. 是的,所有派生类都是其所有祖先的类型。

I'm not sure if there is a way to "remove" base implementation - never tried it. 我不确定是否有办法“删除”基本实现-从未尝试过。 This sounds like your OOD is broken in a major way. 听起来您的OOD发生了重大故障。

What you might be able to do (and again, never tried it), is to provide your own casting conversion and always return null when trying to cast to that base/interface. 您可能能够做的(也是一次,从未尝试过)是提供您自己的转换转换,并且在尝试转换为该基础/接口时始终返回null。 Not sure if it'll do the trick (probably won't work when access via reflection). 不知道它是否会成功(通过反射访问时可能无法正常工作)。

If a base class implements an interface, all derived classes will also implement that interface. 如果基类实现接口,则所有派生类也将实现该接口。 And actually, the fact that any derived class will also implement the interface is a key feature of object oriented programming. 实际上,任何派生类也将实现接口的事实是面向对象编程的关键特征。 (See Liskov substitution principle ) (请参阅Liskov替代原理

Once you implement an interface in a base class, is that class (and derived classes) stuck being of that interface type? 在基类中实现接口后,该类(和派生类)是否仍属于该接口类型?

Yes. 是。

Any way to remove it so "is" return false? 有什么办法可以删除它,以便“ is”返回false?

No. 没有。

If you need to extend a class that implements an interface without implementing that interface in the new class, the solution is to use encapsulation . 如果需要扩展实现接口的类而不在新类中实现该接口,则解决方案是使用封装 Wrap up the base class in your new class and explicitly expose any properties or methods you want to allow access to. 在新类中包装基类,并显式公开要允许访问的任何属性或方法。

Yes all subclasses will always return true for is when compared to the base or interface. 是对所有的子类将始终返回true is相对于基础或接口时。 If you want to check if an object is of a specific type, use 如果要检查对象是否为特定类型,请使用

if (obj.GetType() == typeof(MyClassName))
{

}

If you want to check a very particular inheritance point, like if an object has some depth of implementation but only so far, just combined is statements like so: 如果你想检查一个非常特别的继承问题,就像如果一个对象有实现一定的深度,但也仅此而已,只是结合is像这样的语句:

if (obj is FlyingThing && !(obj is Airplane))
{

}

Yes. 是。 You can think of inheritance as an "is a" relationship. 您可以将继承视为“是”关系。

class C1 : I {}
class C2 : C1 {}

Can be read as: 可以理解为:

C1 is a(n) I . C1是a(n) I C2 is a C1 C2C1

Therefore 因此

C2 is a C1 is a(n) I C2C1是a(n) I

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM