如何创建从动态类名访问变量的方法?

时间:2012-08-26 05:17:23

标签: java object dynamic

我有一个“扩展”类,用于处理动态使用的一般函数 - 也就是说,我希望能够将它与许多不同的类和变量一起使用(有点像你将如何使用像Math这样的东西类)。但我遇到了一个我似乎无法弄清楚的问题(我看了很多论坛和网站,但没有运气)。

这是我的Extension类中的函数:

public void setPlayerFriction(Object objectType, double hspeed, double vspeed){
    objectType obj = new objectType();

    if(obj.hspeed >= GameWindow.friction){
          obj.hspeed -= GameWindow.friction;
    }else if(obj.hspeed <= -GameWindow.friction){
          obj.hspeed += GameWindow.friction;
    }else{
          obj.hspeed = 0;
    }
    if(obj.vspeed >= GameWindow.friction){
          obj.vspeed -= GameWindow.friction;
    }else if(obj.vspeed <= -GameWindow.friction){
          obj.vspeed += GameWindow.friction;
    }else{
          obj.vspeed = 0;
    }
}

该函数在我的Player类中使用:

public void runPhysicsEngine(){
    Extensions ext = new Extensions();
    ext.setPlayerFriction(this,hspeed,vspeed);
}

显然做obj.var -= GameWindow.friction;之类的事情,其中​​objvar是方法的参数,不是我想要做的正确方法 - 它只是返回未解决的类型错误。那么正确的方法是什么?如何从动态类名访问变量?

1 个答案:

答案 0 :(得分:0)

我认为使用适当的abstract方法为您的objectType提供getter/setter课程可能有所帮助。例如:

public abstract class ParentObject {

    private double hSpeed;
    private double vSpeed;

    /** Getter/setter methods */

    public double gethSpeed() {
        return hSpeed;
    }
    public void sethSpeed(double hSpeed) {
        this.hSpeed = hSpeed;
    }
    public double getvSpeed() {
        return vSpeed;
    }
    public void setvSpeed(double vSpeed) {
        this.vSpeed = vSpeed;
    }
}

然后,您可以具体实施ObjectType extendParentObject课程。您可以按如下方式更改setPlayerFriction()方法:

public void setPlayerFriction(ParentObject objectType, double hspeed, double vspeed){

    double hSpeed = objectType.gethSpeed();
    if(hSpeed >= GameWindow.friction){
          objectType.sethSpeed(hSpeed - GameWindow.friction);
    }else if(hSpeed <= -GameWindow.friction){
          objectType.sethSpeed(hSpeed + GameWindow.friction);
    }else{
          objectType.sethSpeed(0);
    }

    double vSpeed = getvSpeed();
    if(vSpeed >= GameWindow.friction){
          objectType.setvSpeed(vSpeed - GameWindow.friction);
    }else if(obj.vspeed <= -GameWindow.friction){
          objectType.setvSpeed(vSpeed + GameWindow.friction);
    }else{
          objectType.setvSpeed(0);
    }
}

对于这种方法,你基本上会发送实际扩展你的ParentObject类的类的引用。例如:

public class ObjImpl extends ParentObject{
    //Concrete implementation
}

public class ObjImpl2 extends ParentObject{
    //Concrete implementation
}

public class TestClass{
    public void setPlayerFriction(ParentObject objectType, double hspeed, double vspeed){   
        double hSpeed = objectType.gethSpeed();
        if(hSpeed >= GameWindow.friction){
              objectType.sethSpeed(hSpeed - GameWindow.friction);
        }else if(hSpeed <= -GameWindow.friction){
              objectType.sethSpeed(hSpeed + GameWindow.friction);
        }else{
              objectType.sethSpeed(0);
        }

        double vSpeed = getvSpeed();
        if(vSpeed >= GameWindow.friction){
              objectType.setvSpeed(vSpeed - GameWindow.friction);
        }else if(obj.vspeed <= -GameWindow.friction){
              objectType.setvSpeed(vSpeed + GameWindow.friction);
        }else{
              objectType.setvSpeed(0);
        }
    }

    public static void main(String[] args) {
        ParentObject object1    = new ObjImpl();
        ParentObject object2    = new ObjImpl2();

        setPlayerFriction(object1, 1.0d, 2.0d);
        setPlayerFriction(object2, 2.25d, 2.0d);
    }
}