Superclass static method returning 2 different subclasses

时间:2021-06-11 00:07:36

This is bad programming practice but I've been asked to do it as part of a larger assignment.

这是糟糕的编程习惯,但我被要求将其作为更大任务的一部分。

I am creating a superclass and then 2 subclasses. There is a static method in the superclass that should return either of the 2 subclasses depending on the result. How would I go about writing this method?

我正在创建一个超类,然后是2个子类。超类中有一个静态方法,它应该根据结果返回2个子类中的任何一个。我该如何编写这种方法?

For example I need to do something like this

例如,我需要做这样的事情

public abstract class Superclass{
    public static subclass? createFromFilename(String fileName){
            if(1==1)
               return subclass1;
            else
                   return subclass2;
    }
}

Is this even possible?

这有可能吗?

3 个解决方案

#1


1  

You can do it like this

你可以这样做

public abstract superClass
{
     public static superClass getBaseClass(...)
     {
           if(...)
           {
                 return new baseClass1();//baseClass1 should derive from superClass
           }
           else
           {
                 return new baseClass2();//baseClass2 should derive from superClass
           }
     }
}

You can now do this

你现在可以这样做

superClass sc=superClass.getBaseClass(..);

if(sc instanceof baseClass1)
{
     baseClass1 bc1=(baseClass1)sc;
     //work with bc1...
}

if(sc instanceof baseClass2)
{
     baseClass2 bc2=(baseClass2)sc;
     //work with bc2...
}

#2


4  

I am not sure if this is what you are looking for, but if you want to return the class type, you can do that by writing the subclass name and appending a .class to get the class type. A proper return type would be the type Class with a generic limiting the result to Superclass and its subclasses.

我不确定这是否是您正在寻找的,但如果您想返回类类型,您可以通过编写子类名称并附加.class来获取类类型。正确的返回类型是类型Class,其泛型将结果限制为Superclass及其子类。

public static Class<? extends Superclass> createFromFileName(String fileName) {
     if (fileName.equals("A")) {
          return SubclassA.class;
     } else {
          return SubclassB.class;
     }
}

If, however, you want to return an object of the respective class, you can do that by simply return a new instance and set the return type to Superclass:

但是,如果要返回相应类的对象,只需返回一个新实例并将返回类型设置为Superclass即可:

public static Superclass createFromFileName(String fileName) {
     if (fileName.equals("A")) {
          new SubclassA();
     } else {
          new SubclassB();
     }
}

#3


0  

You are trying to determine the return type as a result of the execution of the method. This is not possible to do. So you have to bear having the common superclass return type. BTW if you know at compile time the proper return type, why dont you change (having 2 methods?) the method to reflect that?

您正在尝试确定执行该方法的结果的返回类型。这是不可能的。所以你必须承担共同的超类返回类型。顺便说一句,如果你在编译时知道正确的返回类型,为什么不改变(有两种方法?)方法来反映?

#1


1  

You can do it like this

你可以这样做

public abstract superClass
{
     public static superClass getBaseClass(...)
     {
           if(...)
           {
                 return new baseClass1();//baseClass1 should derive from superClass
           }
           else
           {
                 return new baseClass2();//baseClass2 should derive from superClass
           }
     }
}

You can now do this

你现在可以这样做

superClass sc=superClass.getBaseClass(..);

if(sc instanceof baseClass1)
{
     baseClass1 bc1=(baseClass1)sc;
     //work with bc1...
}

if(sc instanceof baseClass2)
{
     baseClass2 bc2=(baseClass2)sc;
     //work with bc2...
}

#2


4  

I am not sure if this is what you are looking for, but if you want to return the class type, you can do that by writing the subclass name and appending a .class to get the class type. A proper return type would be the type Class with a generic limiting the result to Superclass and its subclasses.

我不确定这是否是您正在寻找的,但如果您想返回类类型,您可以通过编写子类名称并附加.class来获取类类型。正确的返回类型是类型Class,其泛型将结果限制为Superclass及其子类。

public static Class<? extends Superclass> createFromFileName(String fileName) {
     if (fileName.equals("A")) {
          return SubclassA.class;
     } else {
          return SubclassB.class;
     }
}

If, however, you want to return an object of the respective class, you can do that by simply return a new instance and set the return type to Superclass:

但是,如果要返回相应类的对象,只需返回一个新实例并将返回类型设置为Superclass即可:

public static Superclass createFromFileName(String fileName) {
     if (fileName.equals("A")) {
          new SubclassA();
     } else {
          new SubclassB();
     }
}

#3


0  

You are trying to determine the return type as a result of the execution of the method. This is not possible to do. So you have to bear having the common superclass return type. BTW if you know at compile time the proper return type, why dont you change (having 2 methods?) the method to reflect that?

您正在尝试确定执行该方法的结果的返回类型。这是不可能的。所以你必须承担共同的超类返回类型。顺便说一句,如果你在编译时知道正确的返回类型,为什么不改变(有两种方法?)方法来反映?