简体   繁体   English

java:传递对象并返回相同对象的类型

[英]java: pass an object and return same object's type

I have an abstract class A 我有一个抽象类A

public astract class A{
   public abstract A f(A a) throws IllegalArgumentException;
};

but I want object a passed to f to have the same type of the object itself and return an object of same type, otherwise throw an IllegalArgumentException. 但我希望传递给f的对象具有与对象本身相同的类型,并返回相同类型的对象,否则抛出IllegalArgumentException。 How can I implement that one time for all subclasses? 如何一次为所有子类实现该功能? There is a way to do that at the class A level? 有办法在A级水平上做到这一点吗?

Java does not provide a way to state this requirement declaratively. Java没有提供一种声明式声明此要求的方法。 However, you can make a run-time check at the level of your abstract class by following the Template Method design pattern: 但是,您可以按照“ 模板方法”设计模式在抽象类级别进行运行时检查:

public abstract class A {
    public final A f(A a) {
        if (a.getClass() != this.getClass()) {
            throw new IllegalArgumentException();
        }
        A res = fImpl(a);
        if (res.getClass() != this.getClass()) {
            throw new InvalidOperationException();
        }
        return res;
    }
    protected abstract A fImpl(A a); // Implementing classes put functionality here
}

The above code changes f(A) from abstract method to template method by providing shared functionality for all subclasses of A . 上面的代码通过为A所有子类提供共享功能,将f(A)从抽象方法更改为模板方法 In this case the shared functionality is argument checking, and also the return type checking. 在这种情况下,共享功能是参数检查以及返回类型检查。

This is the method that the clients of your class are going to use. 这是类的客户端将要使用的方法。 The implementors of the abstract class, on the other hand, will be implementing fImpl method, which can assume that its argument A is a subclass that matches their own class. 另一方面,抽象类的实现者将实现fImpl方法,该方法可以假定其参数A是与自己的类匹配的子类。

public abstract class A
{
    public A f(A a) throws IllegalArgumentException
    {
        if (!this.getClass().equals(a.getClass()))
        {
            throw new IllegalArgumentException();
        }
        return a;
    }
}

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

相关问题 JAva:如何将自定义对象类型作为参数传递给方法,并返回相同的自定义对象类型值 - JAva:How to pass a custom object type as a parameter to a method and return the same custom object type value back 如何为同一方法传递不同的 object 类型,而不是在 java 中编写具有相同功能和返回类型的两个方法 - How to pass different object type for same method instead of writing two methods with same functionality and return type in java java:对象作为返回类型 - java: object as a return type Java:返回与一个 JSON 对象相同的类型值 - Java: Return same type values as one JSON object 如何返回扩展Java WebService中相同类型的几种对象类型 - how to return several object types extending the same type in java webservice Java构造函数可以返回已经存在的相同类型的对象吗? - Can a Java constructor return already existing object of same type? 与Java中的方法的返回类型和输入参数具有相同的对象 - having the same object as return type and input argument for a method in Java 将Class作为参数传递并返回相同的类对象 - Pass Class as parameter and return same class object 基于对象类型的Java getter返回类型 - Java getter return type based on object type 我可以通过Web服务中对象的返回类型传递xml对象吗? - can i pass the xml object with return type of object in web services
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM