java中的对象如何使用方法相互交互?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23817359/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 01:23:09  来源:igfitidea点击:

how objects interacts with each other in java using methods?

javaobjectmethodsinteraction

提问by dganesh2002

I am learning java and can do pretty much coding myself without any issues. But I have been always reading in the books - In java, objects interact with each other by invoking methods of other objects?

我正在学习 Java,并且可以自己编写代码而没有任何问题。但是我一直在看书——在java中,对象通过调用其他对象的方法来相互交互?

I am unsure if I got that clearly. Example is like, a Robotclass which has methods like moveForward(), comeToBase(),increaseSpeed()etc. Now if there are two robot objects, then how will they interact with each other to avoid clash? I can understand very well that each robot object can invoke its own methods independently and run independently but how does the interaction between the object happens? Can someone explain based on the above example?

我不确定我是否明白这一点。实例是等时,Robot其具有类似的方法类moveForward()comeToBase()increaseSpeed()等。现在,如果有两个机器人对象,那么他们将如何彼此交互,以避免冲突?我可以很好地理解每个机器人对象可以独立调用自己的方法并独立运行,但是对象之间的交互是如何发生的?有人可以根据上面的例子解释一下吗?

采纳答案by Ian R. O'Brien

Objects typically talk to one another via use of references. For example:

对象通常通过使用引用相互交谈。例如:

class Robot {
    private String m_name;

    public void SetName(String name) {
       m_name = name;
    }

    public String GetName() {
       return m_name;
    }

    public void TalkTo(Robot robot, String speech){
        console.writeline(robot.GetName + " says " + speech " to you.");
    }
}

void MyMethod() {
    Robot robotOne = new Robot();  // variable robotOne contains a reference to a robot
    Robot robotTwo = new Robot();  // variable robotTwo contains a reference to another robot
    robotTwo.SetName("Robert");

    // the first robot says hi to the second
    robotOne.TalkTo(robotTwo, "hello");

   // output
   // Robert says hello to you
}

回答by Ryan D

I would take

我会采取

In java, objects interact with each other by invoking methods of other objects

在java中,对象通过调用其他对象的方法来相互交互

to mean that the RobotFleet Object contains many Robot objects and can manipulate them through the methods on the Robot object.

表示 RobotFleet 对象包含许多 Robot 对象,并且可以通过 Robot 对象上的方法操作它们。

回答by khriskooper

I think @Richard Tingle is right - for two Robot objects to react with one another, it makes sense for there to be a higher level class to control them both, and deal with moving them, what to do if they will collide, increasing speed based on certain conditions, etc.

我认为@Richard Tingle 是对的 - 对于两个 Robot 对象相互反应,有一个更高级别的类来控制它们是有意义的,并处理移动它们,如果它们碰撞怎么办,提高速度基于某些条件等。

You'd probably have a higher level class with a main loop that upon each loop calls methods that deal with user inputs, physical timings, object behaviors, object collisions, rendering objects, and so on, until the whole scenario ends and control is passed to another part of your code. So this class with the main loop could invoke a method that iterates through all Robot objects, and calls certain methods on the Robot objects to determine how to respond based on the current physically modeled environment.

您可能有一个带有主循环的更高级别的类,在每个循环中调用处理用户输入、物理时间、对象行为、对象碰撞、渲染对象等的方法,直到整个场景结束并传递控制权到代码的另一部分。所以这个带有主循环的类可以调用一个方法来遍历所有的 Robot 对象,并调用 Robot 对象上的某些方法来根据当前的物理建模环境确定如何响应。

A similar level of class would be responsible for creating and initialising the Robot objects, and passing control through your system to manage the whole program. Everything you model in Java can be regarded as an Object, even higher level modelling such as game initialisation and game flow management.

类似级别的类将负责创建和初始化 Robot 对象,并通过您的系统传递控制以管理整个程序。您在 Java 中建模的所有内容都可以被视为一个对象,甚至更高级别的建模,例如游戏初始化和游戏流程管理。

For a very basic example:

对于一个非常基本的例子:

public class Game {

    private List<Robot> robots = new ArrayList<Robot>();
    private boolean GameEnded = false;

    public static void main(String [ ] args) {
        initiliaseGame();
        startGameLoop();
        System.exit(0);
    }

    private void initialiseGame() {
        Robot robot1 = new Robot();
        robots.add(robot1);
        Robot robot2 = new Robot();
        robots.add(robot2);
    }

    private void StartGameLoop() {
        while (!gameEnded) {
            /* would need time control logic here for more manageable and consistent simulation */
            updateRobots(); // could check for collisions between other Robots here
            drawRobots();
        }
    }

    private updateRobots() {
        for (Robot robot : robots) {
            robot.update(); // do each robots behaviour (physics, decisions, etc)
        }
    }

    private drawRobots() {
        for (Robot robot : robots) {
            robot.draw(); // draw each robot
        }
    }
}