【问题标题】:Java: Instance of Class B in Class A where Class B uses variables declared in Class AJava:A 类中 B 类的实例,其中 B 类使用 A 类中声明的变量
【发布时间】:2019-10-27 23:58:49
【问题描述】:

AutoRedBuilding 类声明了实例变量leftMotor、rightMotor 和foundationServo。 RobotMover 类包含使用这些实例变量的方法,我想在 AutoRedBuilding 中调用 RobotMover 方法。

具体来说,AutoRedBuilding 中声明的实例变量是电机和伺服系统等执行器。 RobotMover 是一个类,其中包含以某种方式移动执行器的方法,例如向前行驶和转弯。我想以某种方式调用 AutoRedBuilding 中的 RobotMover 方法。

我已尝试公开 AutoRedBuilding 变量,然后在 AutoRedBuilding 中创建 RobotMover 的实例,但没有奏效(代码如下)。错误消息示例(我得到所有执行器变量的相同错误消息):

RobotMover.java - cannot find symbol
symbol: leftMotor
// Copyright (c) 2019 Terrace BroBots. All rights reserved.

package org.firstinspires.ftc.teamcode;

import com.qualcomm.robotcore.eventloop.opmode.Autonomous;
import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode;
import com.qualcomm.robotcore.hardware.DcMotor;
import com.qualcomm.robotcore.hardware.Servo;

@Autonomous(name="Autonomous: Red Building Zone", group="SkyStone")

public class AutoRedBuilding extends LinearOpMode {

    // declare hardware variables
    public DcMotor leftMotor;
    public DcMotor rightMotor;
    public Servo foundationServo;

    @Override
    public void runOpMode() {
        // initialise hardware variables
        leftMotor = hardwareMap.get(DcMotor.class, "leftMotor");
        rightMotor = hardwareMap.get(DcMotor.class, "rightMotor");
        foundationServo = hardwareMap.get(Servo.class, "foundationServo");

        // wait for game to start
        waitForStart();

        /* 
        THIS IS WHERE RobotMover CLASS METHODS ARE CALLED
        */
        RobotMover mover = new RobotMover();
        mover.driveForward(10, 1);
        mover.driveReverse(10, 1);
        mover.turnRight90();
        mover.turnLeft90();
        mover.clipFoundation();
        mover.unclipFoundation();
    }
}

// Copyright (c) 2019 Terrace BroBots. All rights reserved.

import java.lang.Math; 

public class RobotMover {

    // unit conversion rates
    private double robotWidthCm = 0;
    private double wheelRadiusCm = 0;
    private double degree90ToCm = (2 * Math.PI * robotWidthCm) / 4;
    private double cmToTicks = 2240 / (2 * Math.PI * wheelRadiusCm);

    public void setMotorPowers(double leftPower, double rightPower) {
        leftMotor.setPower(leftPower);
        rightMotor.setPower(rightPower);
    }

    public void driveMotorDistances(double cmLeftDistance, double cmRightDistance, double power) {
        // reset encoders
        leftMotor.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
        rightMotor.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);

        // convert cm to ticks and set target position
        int tickLeftDistance = (int) Math.round(cmLeftDistance * cmToTicks);
        int tickRightDistance = (int) Math.round(cmRightDistance * cmToTicks);
        leftMotor.setTargetPosition(tickLeftDistance);
        rightMotor.setTargetPosition(tickRightDistance);

        // drive until position is reached 
        setMotorPowers(power, power);
        while(leftMotor.isBusy() && rightMotor.isBusy()) {}
        setMotorPowers(0, 0);
    }

    /*
    THESE ARE THE METHODS CALLED IN AutoRedBuilding CLASS
    */

    public void driveForward(double cmDistance, double power) {
        driveMotorDistances(cmDistance, cmDistance, power);
    }

    public void driveReverse(double cmDistance, double power) {
        driveMotorDistances(-cmDistance, -cmDistance, power);
    }

    public void turnRight90() {
        driveMotorDistances(degree90ToCm, 0, 0.8);
    }

    public void turnLeft90() {
        driveMotorDistances(0, degree90ToCm, 0.8);
    }

    public void clipFoundation() {
        foundationServo.setPosition(0.5);
    }

    public void unclipFoundation() {
        foundationServo.setPosition(0);
    }
}

【问题讨论】:

  • 但这不起作用 - 你的意思是什么?你有什么错误吗,如果有请分享。
  • @ScaryWombat 添加错误信息
  • 查看minimal reproducible example 定义——我们要求您的问题只包含可能的最短代码,以便其他人重现特定问题。正确地做到这一点通常意味着删除与您尝试构建的更大程序相关的任何内容,并专注于重现您提出问题的问题。 (如果可以在没有机器人特定细节的情况下重现语言级别的问题,则应在提出问题之前删除与机器人相关的元素)。

标签: java class robotics


【解决方案1】:

如果您希望 RobotMover 了解这些变量,则需要在构造函数中或作为 setter 方法将它们传递给此类

例如

RobotMover mover = new RobotMover(DCMotor left, DCMotor right); 

RobotMover 需要有相应的字段

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-06
    • 1970-01-01
    • 2012-10-24
    • 2018-07-03
    • 1970-01-01
    • 2022-12-18
    相关资源
    最近更新 更多