问题标签 [subtyping]
For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.
java - 没有继承的子类型和没有子类型的继承?
我的导师发布了一个示例中期供我们做,我对以下问题感到困惑:
显示没有子类型的继承和没有继承的子类型的示例。
我们使用以下定义,并且使用 Java 进行编程:http ://www.cmi.ac.in/~madhavan/courses/pl2006/lecturenotes/lecture-notes/node28.html
代表这两个想法的好方法是什么?
oop - OOP 中的子集与子类型化
如果“A”是“B”的子类型,则表示“A”继承了“B”的所有字段和方法,此外“A”还有自己的方法和字段。那么'A'应该被认为是'B'的超集吗?子类型的子集解释如何有效?不应该反过来吗?
但是,如果有两个类“汽车”和“车辆”,那么“汽车”集合是“车辆”集合的子集是有道理的。然而,“汽车”的领域和方法是“车辆”的超集。所以我对此有点困惑。
java - Can a method using super take a subtype of an object and add it to the list?
So the function below is supposed to be a function that takes a Car object and adds it to a Col<> object.
The question asked whether if adding a variable n of type Nissan object to a variable c of type List<Car> would work (ie. insertCar(c, n))
The answer is yes but I'm not sure why. I thought adding subtypes of an object Car would not be possible because of the use of super. That it would only take types of type Car or any supertype of Car.
Anyone able to me understand?
EDIT Is it that...
I wouldn't be able to add a Nissan if the List<> itself was of some other subtype being passed in? For example, if List<? super Car> was actually List<? super Ford>
There seems to be conflicting answers below but this is a question provided for exam review so pretty sure the question and answer provided are correct. It's just my understanding of it is what I'm not sure about.
java - 子类型化 Java 类
如果我有一个类 Instructor 并且我希望类人成为它的子类型,我不确定如何进行子类型化,我该怎么做?我试过 class Instructor {} extends Person 而且我每次都收到错误
scala - @uncheckedVariance 在 Kotlin 中?
在他的演讲Compilers are Databases中,Martin Odersky 提出了一个有趣的方差极端案例:
TTree[Type]被定义为逆变的,因为将类型树 ( ) 视为无类型树 ( ) 的子类型很有用Tree[Nothing],但反过来则不然。
通常,Scala 编译器会抱怨T显示为tpe方法的返回类型。这就是 Martin 用注解关闭编译器的@uncheckedVariance原因。
这是翻译成 Kotlin 的示例:
正如预期的那样,Kotlin 编译器抱怨T出现在“出局”位置。Kotlin 有类似的东西@uncheckedVariance吗?还是有更好的方法来解决这个特定问题?
rust - Rust 中的类子类型化
C++ 允许类子类型化,这非常方便,因为您可以使用为基类实现的函数和派生类。Rust 似乎没有这样的东西。该功能似乎在某个时候可用,但此后已被删除。这在 Rust 中是不可能的吗?如果是这样,是否有计划提供此功能?
我想要做的是定义一个从另一个结构继承的结构,在 C++ 中它看起来像:
在我看来,在 Rust 中,您必须编写类似这样的内容,以便为所有“派生”结构提供相同的功能:
我认为这样做impl<T> for Base<T>会产生大量相同功能的副本,因此组合并不是一个真正的选择。
我可能应该指出,选择上述实现的原因很简单,即它允许更安全的向上转换版本,无论如何我都需要这样做。
scala - Scala 泛型子类型参数
为什么编译器将 t1 ++ t2 结果视为 List[Any]?连接两个 S 类型的列表应该只返回一个 S 类型的列表。
typescript - TypeScript:为什么数字可以分配给 Object 类型的引用?
为什么这是合法的 TypeScript?
当然,数字不是Object. 有人可能会怀疑 x 被隐式强制(自动装箱)到一个对象,但不是:
印刷
作为记录:
typescript - TypeScript:子类型和协变参数类型
常识表明子类型应该在返回类型方面是协变的,但在参数类型方面应该是逆变的。因此,由于 的严格协变参数类型,应拒绝以下内容E.f:
确实,运行程序会打印
所以:(1)这里的理由是什么(大概有一个,但是不令人满意和JavaScripty);(2) 编译器在这种情况下不能忽略警告是否有任何实际原因?