如何有效地使用重载和多态

时间:2015-07-10 12:58:21

标签: java polymorphism overloading

我有BaseObj

public abstract class BaseObj {
    String name;
    public BaseObj(String _name) {
        name = _name;
    }

    public void report(){
        System.out.println(name + " is " + getType());
    }

    public abstract String getType();

}

和两个子类Sample1Sample2

public class Sample1 extends BaseObj{
    private float var;
    public Sample1(String name,float _var){
        super(name);
        var = _var;
    }

    @Override
    public String getType() {
        return "Float: " + Float.toString(var);
    }
}

public class Sample2 extends BaseObj{
    private int var;
    public Sample2(String name , int _var){
        super(name);
        var = _var;
    }

    @Override
    public String getType() {
        return "Integer: " + Integer.toString(var);
    }

}

在主要的calss中:

public class Poly {
    public static void main(String[] args){
        BaseObj mObj[] = new BaseObj[4];

        // Hard-definition of the object tyte
        mObj[0] = new Sample1("X1",(float)12.34);
        mObj[1] = new Sample2("X2",12);
        mObj[2] = new Sample2("X3",12);
        mObj[3] = new Sample1("X4",(float)1.2);

        for(BaseObj x:mObj){
            x.report();
        }
    }
}

我必须硬定义mObj元素的类型。但我正在寻找一种使用overload来避免这种严格定义的方法,例如:在一个新类中,我使用重载来根据其输入获得正确的对象:

public class Sample{
    public Sample(String _name , int _var){
        // get Sample2 object
    }
    public Sample(String _name , float _var){
        // get Sample1 object
    }
}

然后我可以按如下方式更改主要代码:

public class Poly {
    public static void main(String[] args){
        BaseObj mObj[] = new BaseObj[4];

        mObj[0] = new Sample("X1",(float)12.34);
        mObj[1] = new Sample("X2",12);
        mObj[2] = new Sample("X3",12);
        mObj[3] = new Sample("X4",(float)1.2);

        for(BaseObj x:mObj){
            x.report();
        }
    }
}

输出目前如下:

X1 is Float: 12.34
X2 is Integer: 12
X3 is Integer: 12
X4 is Float: 1.2

编辑:我需要的是将mObj的元素定义为new Sample("X1",var),而不是将其中一些定义为new Sample1("X1",(float)12.34),将某些元素定义为new Sample2("X1",12)。为此我决定在类Sample的构造函数中我的对象的类型。 谁有任何想法? 感谢

3 个答案:

答案 0 :(得分:1)

我认为你在这里寻找工厂方法。

public class BaseObjFactory {
   public static BaseObj create(String name, int value) {
      return new Sample2(name, value);
   }

   public static BaseObj create(String name, float value) {
      return new Sample1(name, value);
   }
}

并以这种方式使用

mObj[0] = BaseObjFactory.create("X1",12.34f);
mObj[1] = BaseObjFactory.create("X2",12);
mObj[2] = BaseObjFactory.create("X3",12);
mObj[3] = BaseObjFactory.create("X4",1.2f);

顺便说一下。无需使用强制转换(float)1.2,只需附加f即可使其成为float字面值。

答案 1 :(得分:1)

使用工厂方法。

class SampleFactory {
    Sample1 create(String name, int value) {
        return new Sample1(name, value);
    }

    Sample2 create(String name, float value) {
        return new Sample2(name, value);
    }
}

然后你可以用它作为

mObj[0] = SampleFactory.create("X1",(float)12.34);
mObj[1] = SampleFactory.create("X2",12);
mObj[2] = SampleFactory.create("X3",12);
mObj[3] = SampleFactory.create("X4",(float)1.2);

答案 2 :(得分:0)

我建议你从最简单的解决方案入手。

public class Poly {
    public static void main(String[] args) {
        Sample[] mObj = {
                new Sample("X1", 12.34f),
                new Sample("X2", 12),
                new Sample("X3", 12),
                new Sample("X4", 1.2f)
        };

        for (Sample x : mObj) 
            x.report();
    }
}

class Sample {
    private final String desc;
    private final Number value;

    public Sample(String desc, Number value) {
        this.desc = desc;
        this.value = value;
    }

    public void report() {
        System.out.println(desc + " is a " + value.getClass().getSimpleName() + ": " + value);
    }
}

打印

X1 is a Float: 12.34
X2 is a Integer: 12
X3 is a Integer: 12
X4 is a Float: 1.2

这使用多态,因为Float和Integer是Number的子类。