In object-oriented programming, a covariant return type of a method is one that can be replaced by a "narrower" type when the method is overridden in a subclass. A notable language in which this is a fairly common paradigm is C++. C# supports return type covariance as of version 9.0. Covariant return types have been (partially) allowed in the Java language since the release of JDK5.0, so the following example wouldn't compile on a previous release: Another example of covariance with the help of built in Object and String class of Java:
Attributes | Values |
---|
rdfs:label
| - Covariant return type (en)
- 协变返回值类型 (zh)
|
rdfs:comment
| - In object-oriented programming, a covariant return type of a method is one that can be replaced by a "narrower" type when the method is overridden in a subclass. A notable language in which this is a fairly common paradigm is C++. C# supports return type covariance as of version 9.0. Covariant return types have been (partially) allowed in the Java language since the release of JDK5.0, so the following example wouldn't compile on a previous release: Another example of covariance with the help of built in Object and String class of Java: (en)
- 在面向对象程序设计中,实例函数的协变返回值类型指的是子类中的成员函数的返回值类型不必严格等同与该函数所重写的父类中的函数的返回值类型,而可以是更 "狭窄" 的类型。在 C++ 程序设计中。这是一种常见的编程范式,如下面的例子: class BaseClass {};class SubClass: public BaseClass {};class BaseGenerator { virtual BaseClass* generate;};class SubGenerator: public BaseGenerator { virtual SubClass* generate;}; 但是协变类型不能兼容于泛型,如使用到智能指针或泛型容器的场合,即如下的程序是不合法的: class BaseClass {};class SubClass: public BaseClass {};class BaseGenerator { virtual std::auto_ptr<BaseClass> generate;};class SubGenerator: public BaseGenerator { virtual std::auto_ptr<SubClass> generate;}; (zh)
|
dcterms:subject
| |
Wikipage page ID
| |
Wikipage revision ID
| |
Link from a Wikipage to another Wikipage
| |
Link from a Wikipage to an external page
| |
sameAs
| |
dbp:wikiPageUsesTemplate
| |
has abstract
| - In object-oriented programming, a covariant return type of a method is one that can be replaced by a "narrower" type when the method is overridden in a subclass. A notable language in which this is a fairly common paradigm is C++. C# supports return type covariance as of version 9.0. Covariant return types have been (partially) allowed in the Java language since the release of JDK5.0, so the following example wouldn't compile on a previous release: // Classes used as return types:class A {}class B extends A {}// "Class B is narrower than class A"// Classes demonstrating method overriding:class C { A getFoo { return new A; }}class D extends C { // Overriding getFoo in parent class C B getFoo { return new B; }} More specifically, covariant (wide to narrower) or contravariant (narrow to wider) return type refers to a situation where the return type of the overriding method is changed to a type related to (but different from) the return type of the original overridden method. The relationship between the two covariant return types is usually one which allows substitution of the one type with the other, following the Liskov substitution principle. This usually implies that the return types of the overriding methods will be subtypes of the return type of the overridden method. The above example specifically illustrates such a case. If substitution is not allowed, the return type is invariant and causes a compile error. Another example of covariance with the help of built in Object and String class of Java: class Parent { public Object getFoo { return null; }}class Child extends Parent { // String is child of the greater Object class public String getFoo { return "This is a string"; } // Driver code public static void main(String[] args) { Child child = new Child; System.out.println(child.getFoo); }} (en)
- 在面向对象程序设计中,实例函数的协变返回值类型指的是子类中的成员函数的返回值类型不必严格等同与该函数所重写的父类中的函数的返回值类型,而可以是更 "狭窄" 的类型。在 C++ 程序设计中。这是一种常见的编程范式,如下面的例子: class BaseClass {};class SubClass: public BaseClass {};class BaseGenerator { virtual BaseClass* generate;};class SubGenerator: public BaseGenerator { virtual SubClass* generate;}; 但是协变类型不能兼容于泛型,如使用到智能指针或泛型容器的场合,即如下的程序是不合法的: class BaseClass {};class SubClass: public BaseClass {};class BaseGenerator { virtual std::auto_ptr<BaseClass> generate;};class SubGenerator: public BaseGenerator { virtual std::auto_ptr<SubClass> generate;}; 其原因是为了防止通过协变机制绕过类型机制,使得某个指针指向了一个不兼容类型的对象。如: class BaseClass {};class SubClassA: public BaseClass {};class SubClassB: public BaseClass {};int main(void){ std::auto_ptr<SubClassA> subp(new SubClassA); std::auto_ptr<BaseClass>& basep = subp; // 如果此句可以通过编译 basep.reset(new SubClassB); // 此句合法. 此时 subp 内的对象实际类型为与 SubClassA 不兼容的 SubClassB return 0;} (zh)
|
prov:wasDerivedFrom
| |
page length (characters) of wiki page
| |
foaf:isPrimaryTopicOf
| |
is Link from a Wikipage to another Wikipage
of | |
is Wikipage redirect
of | |
is foaf:primaryTopic
of | |