Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java:在两个类中使用相同的方法,但该方法只能在一个类中使用 笔记_Java_Variables_Methods - Fatal编程技术网

Java:在两个类中使用相同的方法,但该方法只能在一个类中使用 笔记

Java:在两个类中使用相同的方法,但该方法只能在一个类中使用 笔记,java,variables,methods,Java,Variables,Methods,一年后回顾这个问题,我认为这个问题问得不好,有点尴尬。我不确定是删除这个问题还是重写它,希望它能帮助那些处在我的位置上的人 尽管阅读了关于setter和getter的文章,但我对类、作用域、依赖关系实际上是如何工作的缺乏基本的理解。当时问这个问题对我把这两部分放在一起很有帮助,因为这是朝着正确的方向推进 编辑问题 我一直在通过整合自己的项目来学习Java。我目前正在做一个简单的游戏,然而,我遇到了一些让我完全困惑的事情。我相信答案很简单,但我不明白为什么会发生这种情况 我有一个方法: publi

一年后回顾这个问题,我认为这个问题问得不好,有点尴尬。我不确定是删除这个问题还是重写它,希望它能帮助那些处在我的位置上的人

尽管阅读了关于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;
            }
        }
    }           
    
}
结论 我希望一个类自动使用另一个类的变量,因为当时我不了解类、作用域和依赖项在Java中是如何工作的


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

动画类有两个变量x和y,当调用
Animation.inderty()
时,它们会发生变化

然而,当您在DogLogic类中调用
mochi.industry()
时,属于mochi类的x和y会发生变化。动画类x和y根本不会更改

因此,当仅调用
mochi.inderty()
时,动画类中的x和y保持不变

更新DogLogic类中的游戏更新:

 public void gameUpdate () {
     animation.setCurrentSprite();
     mochi.inertia();
     animation.updateXY(this.mochi);
 }
在动画课上:

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

请定义“不起作用”。您可以调用
mochi.institute(),但您似乎再也没有尝试提取mochi的x或y。那你怎么能说它不起作用呢?请解释一下细节。您希望看到哪些您没有看到的行为?
Mochi-Mochi=new-Mochi();intx=(int)mochi.getX();int y=(int)mochi.getY()
x
y
将没有任何价值感谢您的回复!是的,特别是x和y的值在调用mochi.institute()时不会更改,但在调用animation.institute()时会更改您没有显示
speedX
speedY
是如何声明或修改的-无法看到
Mochi
Animation
如何访问这些文件如果您再也不提取它们,您如何知道x或y是否发生了变化?请发布真实的代码,代码,否则我们只是在浪费时间。
public void  updateXY( Mochi mochi){
    x = (int) mochi.getX();
    y = (int) mochi.getY();
}