【问题标题】:Java: Same method in two classes, however method only works in one classJava:两个类中的相同方法,但是方法只适用于一个类
【发布时间】:2018-01-30 04:21:53
【问题描述】:

注意事项

一年后回顾这个问题,我认为这是一个关于 SO 的糟糕问题,而且有点尴尬。我不确定是删除这个问题还是重写它,希望它可以帮助那些发现自己处于我的位置的人。

尽管阅读了有关 setter 和 getter 的内容,但我对类、范围、依赖项的实际工作方式缺乏基本的了解。当时问这个问题对我把这两部分放在一起很有帮助,因为这是朝着正确的方向推进。

已编辑的问题

我一直在通过整理自己的项目来研究 Java。我目前正在开发一个简单的游戏,但是,我遇到了一些让我完全难过的事情。我确信答案很简单,但是,我无法弄清楚为什么会发生这种情况。

我有一个方法:

public void inertia () {
    y = speedY+y;
    x= speedX+x;
}

但是它在我尝试过的两个类中表现不同。

当在 [仅] 一个类中调用它时,变量 y [在另一个类中] 似乎没有改变。当在[只是]另一个(负责动画该类中使用的局部变量的类)中调用它时,精灵会像人们期望的那样从屏幕上掉下来[但是,原始类不会更新]。我只是想知道是什么导致了这种行为。

我包含了一个 JLabel 来查看另一个类中的变量,它没有改变。

期望的结果是相应的 x 和 y 值发生变化,以便精灵将围绕 JPanel 移动

这是我想调用它的类:

    public class Mochi {
        
        private float x;
        private float y;
        private float speedX = 0;
        private float speedY = 3;
        private float gravity = 3;  
        boolean mRunR = true;
        
    
        public float getSpeedY(){
                return this.speedY;
        }
        
        public float getSpeedX() {
            return this.speedX;
        }
        
        public float getX() {
                return this.x;
            }
            public float getY() {
                return this.y;
            }
       // problem method below:
        public void inertia () {
            y = speedY+y;
            x= speedX+x;
        }
        
    }

在测试动画时,我在这个类中写了它,发现它在这里可以工作:

public class Animation {
    Image ms = new ImageIcon("mochirs.png").getImage();
    Image mws = new ImageIcon("mochiws.png").getImage();
    
    Image currentSprite;
        
    int aniTime = 1;
    int sW = 21;
    int sH = 14;
    Mochi mochi = new Mochi();
    int x = (int) mochi.getX();
    int y = (int) mochi.getY();
    int speedY = (int) mochi.getSpeedY();
    int speedX = (int) mochi.getSpeedX();
    boolean mRunR = mochi.mRunR;

    public void inertia() {
        y = speedY+y;
        x = speedX+x;
    }
    

    public void draw (Graphics g) {
        Graphics2D g2 = (Graphics2D) g.create();
        g2.setClip(x, y, sW, sH);
        g2.drawImage(currentSprite, x, y, sW, sH, null);
    }
    
    public void setCurrentSprite (){
        if (mRunR == true) {
            if (aniTime <= 5) {
                currentSprite = mws;
                aniTime ++;
            }else if (aniTime <= 10) {
                currentSprite = ms;
                aniTime++;
            }else  {
                aniTime = 1;
            }
        }
    }           
    
}

下面是调用方法:

public class DogLogic extends JPanel {
    Animation animation = new Animation();
    Mochi mochi = new Mochi();
    int refreshRate = 30;


public DogLogic () {
    JPanel testPain = new JPanel();
    JLabel testLabel2= new JLabel (Integer.toString((int)mochi.getX()));
    JLabel tl3 = new JLabel (Integer.toString((int) mochi.getY()));
    JLabel tl4 = new JLabel (Integer.toString((int) mochi.getSpeedY()));
    testPain.add(testLabel2, BorderLayout.SOUTH);
    testPain.add(tl3);
    testPain.add(tl4);
    add (testPain);         
        gameStart();

    }

    public void gameStart() {
        Thread gameThread = new Thread() {
            public void run() {
                while (true) {
                    gameUpdate();
                    repaint();
                    try {
                        Thread.sleep(1000/refreshRate);
                    }catch (InterruptedException e) {
                        System.out.println("An error in gameThread has occured");
                    }
                }
            
            }
        }; 
        gameThread.start(); 
    }
    
    public void gameUpdate () {
        animation.setCurrentSprite();
        mochi.inertia(); 
        animation.intertia(); 
    }
    


        @Override
        public void paintComponent (Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D) g.create();
            animation.draw(g2);
            
            }
    
}

结论

我希望一个类自动使用另一个类的变量,因为当时我不了解 Java 中的类、作用域和依赖项是如何工作的。

如果您发现自己有类似的问题、研究范围、依赖注入和基本的面向对象设计模式。

【问题讨论】:

  • 请定义“不起作用”。您致电mochi.inertia();,但您似乎再也不会尝试提取mochi 的x 或y。那你怎么能说它不起作用呢?请详细说明。您期望哪些行为是您没有看到的?
  • Mochi mochi = new Mochi(); int x = (int) mochi.getX(); int y = (int) mochi.getY(); xy 在 mochi 中不会有任何值
  • 感谢您的回复!是的,特别是 x 和 y 的值在调用 mochi.inertia() 时不会改变,但是在调用 animation.inertia() 时它们会改变
  • 您没有显示speedXspeedY 是如何声明或修改的——不可能看到MochiAnimation 是如何访问这些的
  • 如果不再提取 x 或 y,如何知道它们是否发生变化?请发布真实代码,minimal reproducible example 代码,否则我们只是在这里浪费时间。

标签: java variables methods


【解决方案1】:

Animation 类有两个变量 x 和 y,它们在调用 animation.inertia() 时会发生变化。

但是,当您在 DogLogic 类中调用 mochi.inertia() 时,属于 Mochi 类的 x 和 y 会发生变化。动画类 x 和 y 完全没有变化。

因此,当仅调用 mochi.inertia() 时,Animation 类中的 x 和 y 保持不变。

在 DogLogic 类中更新 gameUpdate:

 public void gameUpdate () {
     animation.setCurrentSprite();
     mochi.inertia();
     animation.updateXY(this.mochi);
 }

在动画类中:

public void  updateXY( Mochi mochi){
    x = (int) mochi.getX();
    y = (int) mochi.getY();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-01
    • 1970-01-01
    • 2014-11-24
    • 2017-11-05
    • 2020-02-29
    • 2013-01-21
    相关资源
    最近更新 更多