0

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

这是我的扩展类中的函数:

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;, whereobjvarare arguments 的方法并不是做我想做的事情的正确方法——它只是返回未解决的类型错误。那么正确的方法是什么?如何从动态类名访问变量?

4

1 回答 1

0

我认为用适当的方法abstract为你上一堂课可能会有所帮助。例如:objectTypegetter/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此类extend进行具体实现ParentObject。你会改变你的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);
    }
}
于 2012-08-26T07:33:24.060 回答