Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/393.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/2/jsf-2/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_Class_Methods - Fatal编程技术网

如何从单独的java类调用方法

如何从单独的java类调用方法,java,class,methods,Java,Class,Methods,我试图在java中从一个单独的类调用一个方法,但它似乎不起作用,或者我做错了什么。我想要实现的是将Race类中的Race方法调用到我的主程序(检查主程序中的注释) 下面是课堂: import java.util.Random; public class RacingEvent { private SimpleWindow w; private RaceTrack track; private Turtle t1 = new Turtle(w, 200, 400);

我试图在java中从一个单独的类调用一个方法,但它似乎不起作用,或者我做错了什么。我想要实现的是将
Race
类中的
Race
方法调用到我的主程序(检查主程序中的注释)

下面是课堂:

import java.util.Random;

public class RacingEvent {
    private SimpleWindow w;
    private RaceTrack track;
    private Turtle t1 = new Turtle(w, 200, 400);
    private Turtle t2 = new Turtle(w, 300, 400);

    public RacingEvent(RaceTrack track, Turtle t1, Turtle t2) {
        this.t1 = t1;
        this.t2 = t2;
        this.track = track;
 }

    public void Race() {
        t1.penDown();
        t2.penDown();
        Random rand = new Random();

        Turtle t1 = new Turtle(w, 200, 400);
        Turtle t2 = new Turtle(w, 300, 400);

        while (t1.getY() > track.getyFinish() && t2.getY() > track.getyFinish()) {
            int turtle1 = rand.nextInt(3);
            int turtle2 = rand.nextInt(3);
            t1.forward(turtle1);
            t2.forward(turtle2);

            SimpleWindow.delay(10);
        }


        int diff1 = t1.getY() - track.getyFinish();
        int diff2 = t2.getY() - track.getyFinish();

        SimpleWindow w = new SimpleWindow(200, 100, "Winner");

        if (t1.getY() <= track.getyFinish()) {
            w.moveTo(20, 40);
            w.writeText("T1 won with a " + diff2 + " step(s) lead!");
        } else if (t2.getY() <= track.getyFinish()) {
            w.moveTo(20, 40);
            w.writeText("T2 won with a " + diff1 + " step(s) lead!");
        }
    }
}
当你这么做的时候

RacingEvent event = new RacingEvent(track, t1, t2);
您只是在创建和实例化RacingEvent类的一个对象

函数调用尚未进行

您应该编写此代码来调用函数

event.Race();

希望它能解决问题。

RacingEvent
不会初始化其
w
字段。您可能希望将调用者可用的值(在那里也被称为
w
)传递给构造函数,并将
w
设置为传入的参数

详细内容:

将构造函数更改为

public RacingEvent(SimpleWindow w, RaceTrack track, Turtle t1, Turtle t2) {
        this.w = w;
        this.t1 = t1;
        this.t2 = t2;
        this.track = track;
 }
这样称呼它:

 RacingEvent event = new RacingEvent(w, track, t1, t2);
 event.race();
简化解决方案:

import java.awt.*;
import java.util.Random;

class SimpleWindow {
  public SimpleWindow(int i, int i1, String winner) {
    //To change body of created methods use File | Settings | File Templates.
  }

  public static void delay(int i) {
    //To change body of created methods use File | Settings | File Templates.
  }

  public void moveTo(int i, int i1) {
    //To change body of created methods use File | Settings | File Templates.
  }

  public void writeText(String s) {
    //To change body of created methods use File | Settings | File Templates.
  }

  public void waitForMouseClick() {
    //To change body of created methods use File | Settings | File Templates.
  }
}
class ColorTurtle extends Turtle {
  public ColorTurtle(SimpleWindow w, int i, int yStart, Color red) {
    //To change body of created methods use File | Settings | File Templates.
  }

  public ColorTurtle(SimpleWindow w, int i, int i1) {
    super(w, i, i1);
  }
}

class RaceTrack {
  private int yFinish;

  public RaceTrack(SimpleWindow w, int yStart, int yFinish) {
    //To change body of created methods use File | Settings | File Templates.
  }

  public int getyFinish() {
    return yFinish;
  }

  public void draw(SimpleWindow w) {
    //To change body of created methods use File | Settings | File Templates.
  }
}
class Turtle {
  private int y;

  public Turtle(SimpleWindow w, int i, int i1) {
    //To change body of created methods use File | Settings | File Templates.
  }

  Turtle() {
  }

  public void penDown() {
    //To change body of created methods use File | Settings | File Templates.
  }

  public int getY() {
    return y;
  }

  public void forward(int turtle1) {
    //To change body of created methods use File | Settings | File Templates.
  }
}
class RacingEvent {
  private SimpleWindow w;
  private RaceTrack track;
  private Turtle t1 = new Turtle(w, 200, 400);
  private Turtle t2 = new Turtle(w, 300, 400);

  public RacingEvent(RaceTrack track, Turtle t1, Turtle t2) {
    this.t1 = t1;
    this.t2 = t2;
    this.track = track;
  }

  public void Race() {
    t1.penDown();
    t2.penDown();
    Random rand = new Random();

    Turtle t1 = new Turtle(w, 200, 400);
    Turtle t2 = new Turtle(w, 300, 400);

    while (t1.getY() > track.getyFinish() && t2.getY() > track.getyFinish()) {
      int turtle1 = rand.nextInt(3);
      int turtle2 = rand.nextInt(3);
      t1.forward(turtle1);
      t2.forward(turtle2);

      SimpleWindow.delay(10);
    }


    int diff1 = t1.getY() - track.getyFinish();
    int diff2 = t2.getY() - track.getyFinish();

    SimpleWindow w = new SimpleWindow(200, 100, "Winner");

    if (t1.getY() <= track.getyFinish()) {
      w.moveTo(20, 40);
      w.writeText("T1 won with a " + diff2 + " step(s) lead!");
    } else if (t2.getY() <= track.getyFinish()) {
      w.moveTo(20, 40);
      w.writeText("T2 won with a " + diff1 + " step(s) lead!");
    }
  }
}
public class TurtleRace {
  public static void main(String[] args) {
    SimpleWindow w = new SimpleWindow(600, 600, "TurtleRace");
    int yStart = 400;
    int yFinish = 100;

    RaceTrack track = new RaceTrack(w, yStart, yFinish);
    ColorTurtle t1 = new ColorTurtle(w, 250, yStart, java.awt.Color.RED);
    ColorTurtle t2 = new ColorTurtle(w, 350, yStart, java.awt.Color.BLUE);

    track.draw(w);
    w.waitForMouseClick();

    RacingEvent event = new RacingEvent(track, t1, t2);
    /*Call Race Method*/
    event.Race();
  }
} 
import java.awt.*;
导入java.util.Random;
类SimpleWindow{
公共SimpleWindow(inti,inti1,字符串赢家){
//要更改已创建方法的主体,请使用文件|设置|文件模板。
}
公共静态无效延迟(int i){
//要更改已创建方法的主体,请使用文件|设置|文件模板。
}
公共无效移动到(int i,int i1){
//要更改已创建方法的主体,请使用文件|设置|文件模板。
}
public void writeText(字符串s){
//要更改已创建方法的主体,请使用文件|设置|文件模板。
}
public void waitForMouseClick(){
//要更改已创建方法的主体,请使用文件|设置|文件模板。
}
}
甲鱼类{
公共彩色甲鱼(简单黑白、整数i、整数Y、红色){
//要更改已创建方法的主体,请使用文件|设置|文件模板。
}
公共彩色海龟(SimpleWindow、int i、int i1){
super(w,i,i1);
}
}
职业赛马场{
私家侦探;
公共赛马场(SimpleWindow、int yStart、int yFinish){
//要更改已创建方法的主体,请使用文件|设置|文件模板。
}
public int getyFinish(){
返回yFinish;
}
公共无效绘图(简单窗口){
//要更改已创建方法的主体,请使用文件|设置|文件模板。
}
}
甲鱼{
私营企业;
公乌龟(SimpleWindow,int i,int i1){
//要更改已创建方法的主体,请使用文件|设置|文件模板。
}
海龟(){
}
公共空间{
//要更改已创建方法的主体,请使用文件|设置|文件模板。
}
公共int getY(){
返回y;
}
公共空位前进(内线1){
//要更改已创建方法的主体,请使用文件|设置|文件模板。
}
}
类RacingEvent{
私人住宅区;
私人马场;;
私人海龟t1=新海龟(w,200400);
私人海龟t2=新海龟(w,300,400);
公共赛马项目(赛马场、海龟t1、海龟t2){
这是1.t1=t1;
这1.t2=t2;
this.track=轨道;
}
公开竞逐(){
t1.penDown();
t2.penDown();
Random rand=新的Random();
海龟t1=新海龟(w,200400);
海龟t2=新海龟(w,300,400);
而(t1.getY()>track.getyFinish()&&t2.getY()>track.getyFinish()){
int turtle1=兰特nextInt(3);
int turtle2=兰特nextInt(3);
t1.前锋(套头衫1);
t2.向前(套头衫2);
简单延时(10);
}
int diff1=t1.getY()-track.getyFinish();
int diff2=t2.getY()-track.getyFinish();
SimpleWindow=新的SimpleWindow(200100,“获胜者”);

如果(t1.getY())什么似乎不起作用?显示并使用您尝试过的内容?您不能调用
event.Race();
?尝试过,但我得到了一个java.lang。NullPointerException@Rob你从哪里得到NPE?NPE的调用堆栈是什么?+1哦,你是对的。它被用在'Turtle t1=new Turtle(w,200400)在初始化之前。对我的RacingEvent类做了一些小的修改,但后来我让它工作了。谢谢大家!酷的是,接受的答案并不能解决问题:D
import java.awt.*;
import java.util.Random;

class SimpleWindow {
  public SimpleWindow(int i, int i1, String winner) {
    //To change body of created methods use File | Settings | File Templates.
  }

  public static void delay(int i) {
    //To change body of created methods use File | Settings | File Templates.
  }

  public void moveTo(int i, int i1) {
    //To change body of created methods use File | Settings | File Templates.
  }

  public void writeText(String s) {
    //To change body of created methods use File | Settings | File Templates.
  }

  public void waitForMouseClick() {
    //To change body of created methods use File | Settings | File Templates.
  }
}
class ColorTurtle extends Turtle {
  public ColorTurtle(SimpleWindow w, int i, int yStart, Color red) {
    //To change body of created methods use File | Settings | File Templates.
  }

  public ColorTurtle(SimpleWindow w, int i, int i1) {
    super(w, i, i1);
  }
}

class RaceTrack {
  private int yFinish;

  public RaceTrack(SimpleWindow w, int yStart, int yFinish) {
    //To change body of created methods use File | Settings | File Templates.
  }

  public int getyFinish() {
    return yFinish;
  }

  public void draw(SimpleWindow w) {
    //To change body of created methods use File | Settings | File Templates.
  }
}
class Turtle {
  private int y;

  public Turtle(SimpleWindow w, int i, int i1) {
    //To change body of created methods use File | Settings | File Templates.
  }

  Turtle() {
  }

  public void penDown() {
    //To change body of created methods use File | Settings | File Templates.
  }

  public int getY() {
    return y;
  }

  public void forward(int turtle1) {
    //To change body of created methods use File | Settings | File Templates.
  }
}
class RacingEvent {
  private SimpleWindow w;
  private RaceTrack track;
  private Turtle t1 = new Turtle(w, 200, 400);
  private Turtle t2 = new Turtle(w, 300, 400);

  public RacingEvent(RaceTrack track, Turtle t1, Turtle t2) {
    this.t1 = t1;
    this.t2 = t2;
    this.track = track;
  }

  public void Race() {
    t1.penDown();
    t2.penDown();
    Random rand = new Random();

    Turtle t1 = new Turtle(w, 200, 400);
    Turtle t2 = new Turtle(w, 300, 400);

    while (t1.getY() > track.getyFinish() && t2.getY() > track.getyFinish()) {
      int turtle1 = rand.nextInt(3);
      int turtle2 = rand.nextInt(3);
      t1.forward(turtle1);
      t2.forward(turtle2);

      SimpleWindow.delay(10);
    }


    int diff1 = t1.getY() - track.getyFinish();
    int diff2 = t2.getY() - track.getyFinish();

    SimpleWindow w = new SimpleWindow(200, 100, "Winner");

    if (t1.getY() <= track.getyFinish()) {
      w.moveTo(20, 40);
      w.writeText("T1 won with a " + diff2 + " step(s) lead!");
    } else if (t2.getY() <= track.getyFinish()) {
      w.moveTo(20, 40);
      w.writeText("T2 won with a " + diff1 + " step(s) lead!");
    }
  }
}
public class TurtleRace {
  public static void main(String[] args) {
    SimpleWindow w = new SimpleWindow(600, 600, "TurtleRace");
    int yStart = 400;
    int yFinish = 100;

    RaceTrack track = new RaceTrack(w, yStart, yFinish);
    ColorTurtle t1 = new ColorTurtle(w, 250, yStart, java.awt.Color.RED);
    ColorTurtle t2 = new ColorTurtle(w, 350, yStart, java.awt.Color.BLUE);

    track.draw(w);
    w.waitForMouseClick();

    RacingEvent event = new RacingEvent(track, t1, t2);
    /*Call Race Method*/
    event.Race();
  }
}