简体   繁体   English

抽象超类将超类型传递给返回类型

[英]Abstract superclass passing supertype to return type

I'm trying to figure out how to (if it's even possible) change the base return type, when the type has yet to be passed to an abstract class. 我试图弄清楚当类型还没有传递给抽象类时如何(如果可能的话)改变基返回类型。 (I'm sorry for such a bland explanation, but I really have no clue how to better explain this) (对不起如此温和的解释,我感到抱歉,但是我真的不知道如何更好地解释这一点)

// Base Profile and Repository
public abstract class BaseProfile { }
public abstract class BaseRepository<T extends BaseProfile> {
    public abstract T doSomething(String name);
}

// Enhanced Profile and Repository
public abstract class EnhancedProfile extends BaseProfile {
    public abstract String getName();
}
public abstract class EnhancedRepository<T extends EnhancedProfile> extends BaseRepository<T> {
}

// Instance of Repository
public class InstanceProfile extends EnhancedProfile {
    @Override
    public String getName() { return "Hello World"; }
}
public class InstanceRepository extends EnhancedRepository<EnhancedProfile> {
    public EnhancedProfile doSomething() { return null; }
}

Now what I want is to store an EnhancedRepository without knowing it's inherited class and be able to access the EnhancedProfile, not BaseProfile, see below: 现在我想要的是存储一个EnhancedRepository,而不知道它是继承的类,并且能够访问EnhancedProfile,而不是BaseProfile,请参见下文:

// What I want
EnhancedRepository repo = new InstanceRepository();
EnhancedProfile enProfile = repo.doSomething();
// Does not work because the doSomething() method actually returns
// BaseProfile, when I need it to at least return the EnhancedProfile

// What I know works, but can't do
EnhancedRepository<InstanceProfile> repo2 = new InstanceRepository();
EnhancedProfile enProfile2 = repo2.doSomething();
// This works because I pass the supertype, but I can't do this because I need
// to be able to access EnhancedProfile from the doSomething() method
// from a location in my project which has no access to InstanceProfile

How can I get the EnhancedProfile from doSomething(), instead of the base-most type BaseProfile, without knowing the supertype of EnhancedRepository? 在不知道EnhancedRepository的超类型的情况下,如何从doSomething()获取EnhancedProfile,而不是最基本的类型BaseProfile?

One simple way is to not use generics (seems pointless in your example). 一种简单的方法是不使用泛型(在您的示例中似乎毫无意义)。 Instead override the method with a more specific one 而是使用更具体的方法覆盖该方法

// Base Profile and Repository
public abstract class BaseProfile {
}

public abstract class BaseRepository {
    public abstract BaseProfile doSomething(String name);
}

// Enhanced Profile and Repository
public abstract class EnhancedProfile extends BaseProfile {
    public abstract String getName();
}

public abstract class EnhancedRepository extends BaseRepository {
    @Override
    public abstract EnhancedProfile doSomething(String name);
}

// Instance of Repository
public class InstanceProfile extends EnhancedProfile {
    @Override
    public String getName() {
        return "Hello World";
    }
}

public class InstanceRepository extends EnhancedRepository {
    public EnhancedProfile doSomething(String name) {
        return null;
    }
}

void whatIWant() {
    EnhancedRepository repo = new InstanceRepository();
    EnhancedProfile enProfile = repo.doSomething("");
}

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

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