代码之家  ›  专栏  ›  技术社区  ›  Pops Atula

什么是协变返回类型?

  •  90
  • Pops Atula  · 技术社区  · 15 年前

    Java中的协变返回类型是什么?一般来说,在面向对象编程中?

    7 回复  |  直到 7 年前
        1
  •  124
  •   Sanghyun Lee    10 年前

    协变返回,意味着当一个重写方法时,重写方法的返回类型可以是被重写方法的返回类型的子类型。

    为了举例说明这一点,一个常见的例子是 Object.clone() -声明为返回 Object . 您可以在自己的类中重写它,如下所示:

    public class MyFoo
    {
    
       ...
    
       // Note covariant return here, method does not just return Object
       public MyFoo clone()
       {
           // Implementation
       }
    }
    

    这里的好处是,任何对myfoo对象具有显式引用的方法都可以调用 clone() 并且知道(不进行强制转换)返回值是 MyFoo . 如果没有协变返回类型,则必须声明myfoo中的重写方法以返回 对象 -因此,调用代码必须显式地降低方法调用的结果(甚至认为双方都“知道”它只能是myfoo的一个实例)。

    注意,没有什么特别的 克隆() 任何被重写的方法都可以有协变返回——我在这里用它作为例子,因为它是一个标准方法,在这里它通常是有用的。

        2
  •  33
  •   JHH    7 年前

    下面是另一个简单的例子:

    Animal

    public class Animal {
    
        protected Food seekFood() {
    
            return new Food();
        }
    }
    

    Dog

    public class Dog extends Animal {
    
        @Override
        protected Food seekFood() {
    
            return new DogFood();
        }
    }
    

    可以修改 艾斯 seekFood() 方法到 DogFood 一个子类 Food ,如下所示:

    @Override
    protected DogFood seekFood() {
    
        return new DogFood();
    }
    

    这是一种完全合法的覆盖,返回类型为 艾斯 搜索食物() 方法被称为 协变返回类型 .

        3
  •  6
  •   Community CDub    7 年前

    从JDK 1.5的发布中,Java中引入了协变类型。我会用一个简单的例子向你解释: 当我们重写一个函数时,该函数允许对其行为进行更改。 这是你在大多数书中都能读到的,但是他们遗漏的是我们也可以改变返回类型。 请检查下面的链接以获得澄清,我们可以更改返回类型,只要它可以分配给方法的基本版本的返回类型。

    所以这个返回派生类型的特征称为协变…

    Can overridden methods differ in return type?

        4
  •  6
  •   Avinash Anand    7 年前

    协变返回类型只意味着返回自己的类引用或其子类引用。

    class Parent {
     //it contain data member and data method
    }
    
    class Child extends Parent { 
    //it contain data member and data method
     //covariant return
      public Parent methodName() {
         return new Parent();
              or 
         return Child();
      }
    
    }
    
        5
  •  1
  •   ImtiazeA    7 年前

    协变返回类型指定返回类型可能与子类的方向相同

    class One{  
        One get(){return this;}  
    }  
    
    class Two extends One{  
      Two get(){return this;}  
    
    void message(){
      System.out.println("After Java5 welcome to covariant return type");
    }  
    
    public static void main(String args[]){  
        new Two().get().message();  
    }  
    }
    

    在Java 5之前,不可能重写任何方法。 通过更改返回类型。但现在,既然java5,

    可以通过更改返回类型来重写方法 如果子类重写任何方法 其返回类型为非基元 但它将返回类型更改为子类类型。

        6
  •  1
  •   Arun Raaj    7 年前
    • 它有助于避免混淆类层次结构中存在的类型转换 从而使代码可读、可用和可维护。
    • 我们可以在重写时使用更具体的返回类型
      方法。

    • 帮助防止退货时出现运行时ClassCastExceptions

    参考文献: 网址:www.geeksforgeks.org

        7
  •  0
  •   Soner from The Ottoman Empire    7 年前
    • Java中的协变返回类型,允许缩小返回类型。 重写方法的。
    • 此功能有助于避免向下铸造 在客户端。它允许程序员无需编程 类型检查和铸造。
    • 协变返回类型始终 仅适用于非基元返回类型。
    interface Interviewer {
        default Object submitInterviewStatus() {
            System.out.println("Interviewer:Accept");
            return "Interviewer:Accept";
        }
    }
    class Manager implements Interviewer {
        @Override
        public String submitInterviewStatus() {
            System.out.println("Manager:Accept");
            return "Manager:Accept";
        }
    }
    class Project {
        public static void main(String args[]) {
            Interviewer interviewer = new Manager();
            interviewer.submitInterviewStatus();
            Manager mgr = new Manager();
            mgr.submitInterviewStatus();
        }
    }
    

    另一个例子是Java,

    一元运算符.java

    @FunctionalInterface
    public interface UnaryOperator<T> extends Function<T, T> {
    
        /**
         * Returns a unary operator that always returns its input argument.
         *
         * @param <T> the type of the input and output of the operator
         * @return a unary operator that always returns its input argument
         */
        static <T> UnaryOperator<T> identity() {
            return t -> t;
        }
    }
    

    函数

    @FunctionalInterface
    public interface Function<T, R> {
    
        ........
        ........
        ........
        ........
    
        static <T> Function<T, T> identity() {
            return t -> t;
        }
    }