[英]Using an object from main class in another class
So we are trying to create an AI robot and I found out that Java isn't that easy as I tought. 因此,我们正在尝试创建一个AI机器人,但我发现Java并不像我想的那么简单。 What I am trying to do is to get an object from another class.
我想做的是从另一个类中获取一个对象。
Main class: 主班:
package main;
import lejos.hardware.Brick;
import lejos.hardware.BrickFinder;
import lejos.hardware.motor.EV3LargeRegulatedMotor;
import lejos.robotics.RegulatedMotor;
import movement.Forward;
public final class Runner {
public static void main(String[] args) {
//create brick
Brick brick = BrickFinder.getDefault();
// create motors
RegulatedMotor left = new EV3LargeRegulatedMotor(brick.getPort("A"));
RegulatedMotor right = new EV3LargeRegulatedMotor(brick.getPort("D"));
//initialize motors
left.setAcceleration(400);
right.setAcceleration(400);
left.setSpeed(400);
right.setSpeed(400);
Forward forward = new Forward();
forward.moveForward();
}
}
but the other class is this 但是另一个班级是这个
package movement;
import lejos.utility.Delay;
import main.Runner;
public class Forward{
public int speed;
public void moveForward(){
//moves forward for 5 seconds, then floats until motor stops and then closes the motors
left.backward();
right.backward();
Delay.msDelay(5000);
left.flt();
right.flt();
left.close();
right.close();
}
}
I would like to use the object left and right to make a method which would move forward. 我想使用左右对象来制作可以向前移动的方法。
You could pass left and right into Forward: 您可以左右传递前进:
Forward forward = new Forward(left, right);
and change Forward constructor: 并更改正向构造函数:
public class Forward{
public int speed;
RegulatedMotor left;
RegulatedMotor right;
public Forward(RegulatedMotor l, RegulatedMotor r) {
left = l;
right = r;
}
Your Forward
class obviously needs a reference to the two motor instances. 您的
Forward
类显然需要引用两个电机实例。 You should forward (no pun intended) them at construction time: 您应该在施工时转发它们(不要双关语):
public class Forward {
private final RegulatedMotor left;
private final RegulatedMotor right;
public Forward(RegulatedMotor left, RegulatedMotor right) {
this.left = left;
this.right = right;
}
public void moveForward() { ... }
}
Your foward
metho now has access to them. 您的
foward
方法现在可以访问它们。 Constructing such a Forward
now must be done as following: 现在必须按照以下步骤构造此类
Forward
:
Forward forward = new Forward(left, right);
Firstly, you'd have to initialise them outside the "main" subroutine. 首先,您必须在“ main”子例程之外初始化它们。 And then you'd have to make them public and static.
然后,您必须将它们公开和静态化。
public static RegulatedMotor left;
public static RegulatedMotor right;
public static void main(String[] args)
{
left = new EV3LargeRegulatedMotor(brick.getPort("A"));
right = new EV3LargeRegulatedMotor(brick.getPort("D"));
//rest of code as normal.
}
And then to access them from another class I believe you'd use: 然后从另一个类访问它们,我相信您会使用:
Runner.left.Whateverfunction();
For the "left" object and; 对于“左”对象;
Runner.right.Whateverfunction();
For the "right" object. 对于“正确”的对象。
It's possible that I got a couple of syntax errors because I'm mainly a C# programmer, but I believe this should more or less be accurate. 因为我主要是C#程序员,所以可能会出现一些语法错误,但是我相信这应该或多或少是正确的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.