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

从Java构造函数获取变量

从Java构造函数获取变量,java,constructor,Java,Constructor,我是Java编程新手,如果这是一个愚蠢的问题,很抱歉 我发现很难恰当地表达这个问题,但我有一个任务,创建一个飞机类,可以使飞机着陆、起飞等,并需要使用Testclass对其进行测试。当输入新对象时,它会自动为构造器中的飞机分配一个唯一的ID 我可以使用实例方法很好地实现这一点,因为它具有返回给Testclass的返回值。问题希望我在构造函数本身中这样做,但是,构造函数从不返回任何东西。因此变量永远不会被发送到Testclass。我显然没有正确理解OOP。即使当我尝试使用getter方法来获取在构

我是Java编程新手,如果这是一个愚蠢的问题,很抱歉

我发现很难恰当地表达这个问题,但我有一个任务,创建一个飞机类,可以使飞机着陆、起飞等,并需要使用Testclass对其进行测试。当输入新对象时,它会自动为构造器中的飞机分配一个唯一的ID

我可以使用实例方法很好地实现这一点,因为它具有返回给Testclass的返回值。问题希望我在构造函数本身中这样做,但是,构造函数从不返回任何东西。因此变量永远不会被发送到Testclass。我显然没有正确理解OOP。即使当我尝试使用getter方法来获取在构造函数中创建的ID时,它也会在构造函数处理该ID之前为我提供初始化变量。这是我到目前为止的代码,我知道这是完全错误的,但如果有人能给我指出正确的方向,或者告诉我如何更好地表达这个问题,这将是一个巨大的帮助

// I need to enter 3 aircraft into the system in the testclass

public class Aircraft {

  private int aircraftID;
  private static int lastID;
  private String airportcode;
  private int ID = 100;

  private int count;



  public Aircraft(int a, int b, int c){
    // Constructor


     // Assign ID
     this.ID = a;
     lastID = ID;
     ID++;

     this.ID =b;
     lastID = ID;
     ID++;
  }
}

正如前面所指出的,构造函数不会返回任何东西,简化的版本是,对于new,它返回一个对象实例。我有点猜你想完成什么,但无论如何我都会去做的。在我看来,您试图将3个对象的构造塞进一个构造函数中-这就是为什么您的构造函数有3个参数。你也在对ID进行破坏

我已经删除了所有我不太了解的变量,只留下ID随每个实例化的飞机而增加。@Override主要是为了作秀

public class Aircraft {
    private int aircraftID;
    private static int lastID = 0;

    @Override
    public String toString(){
        return "Aircraft_" + this.aircraftID;
    }

    public Aircraft() {
        lastID++;
        this.aircraftID = lastID;
    }
}
public class TestClass {
    private List<Aircraft> aircrafts;
    public TestClass(){
        aircrafts = new ArrayList<>();
    }

    public void addAircraft(Aircraft a){
        aircrafts.add(a);
    }

    public void printAircraft(){
        Iterator<Aircraft> it = aircrafts.iterator();
        while(it.hasNext()){
            System.out.println(it.next().toString());
        }
    }
}
我冒昧地编写了TestClass,只是想看看我们是否有同样的想法。同样,printAircraft方法是为了展示

public class Aircraft {
    private int aircraftID;
    private static int lastID = 0;

    @Override
    public String toString(){
        return "Aircraft_" + this.aircraftID;
    }

    public Aircraft() {
        lastID++;
        this.aircraftID = lastID;
    }
}
public class TestClass {
    private List<Aircraft> aircrafts;
    public TestClass(){
        aircrafts = new ArrayList<>();
    }

    public void addAircraft(Aircraft a){
        aircrafts.add(a);
    }

    public void printAircraft(){
        Iterator<Aircraft> it = aircrafts.iterator();
        while(it.hasNext()){
            System.out.println(it.next().toString());
        }
    }
}

如果您要编写TestClass,就会出现这种情况。如果给出了这一点,它将有助于了解它的外观-也许这将有助于我们更好地理解。

好的,您希望创建一个具有自动分配的唯一标识符的飞机,并且可以起飞和降落。这意味着您需要一个用于跟踪标识符的字段,一个用于跟踪标识符是否在空中的字段,以及起飞和着陆操作的方法。您还需要一个静态字段来生成唯一标识符。请注意,此实现不是线程安全的

private class Aircraft {

    private static int staticId = 0;
    private int uniqueId = 0;
    private boolean onGround = true; // Aircraft start on the ground in this implementation

    public Aircraft(){
        this.uniqueId = staticId; // putting this line first makes uniqueId zero-indexed in effect
        staticId++;
    }

    public void land(){
        onGround = true;
    }

    public void takeoff(){
        onGround = false;
    }

    public boolean isFlying(){
        return !onGround; // If it's not on the ground, it's flying
    }

    public int getUniqueId(){
        return uniqueId;
    }
}
单元测试检查所讨论类的所有方法和预期功能:

import org.junit.Test;
import static org.junit.Assert.*;
import Aircraft;

class Testclass {

    private final Aircraft aircraft = new Aircraft();

    @Test
    public void hasId(){
        aircraft.getUniqueId() >= 0;
    }

    @Test
    public void canLand(){
        assertTrue(aircraft.land());
    }

    @Test
    public void canTakeOff(){
        assertTrue(aircraft.takeOff());
    }

    @Test
    public void checkFlightOperationsAreTrackedCorrectly(){
        aircraft.land();
        assertFalse(aircraft.isFlying());
        aircraft.takeOff();
        assertTrue(aircraft.isFlying());
    }
}

您不能从构造函数返回任何内容。所以这个问题有缺陷,或者你误解了这个问题。构造器没有返回类型,当您执行Aircraft=new Aircraft 1、2、3时使用;所以,我想它会给你一个飞机的例子,如果你想用这种方式来看待它,你的沮丧是显而易见的,可以理解的;我们都去过那里。当你说你被告知要用Testclass测试它时,你是在说构建单元测试吗?我想我说错了,它不是Testclass,而是单元测试。当输入新飞机对象时,它们将被分配一个唯一的ID。因此,当我使用飞机ac=新飞机时;我想这是为了做到以上几点。是的,我更沮丧的是,我不知道如何/谷歌/问什么,我的讲师说OOP是一个很难掌握的概念,但一旦你掌握了它,你就掌握了它;我在赶时间*facepalm*哇,太棒了,非常感谢你。我相信我在这个问题上的措辞是错误的,因为我需要创建一个单元测试,而不是一个testclass。但它的伟大,通过别人的代码,了解发生了什么,所以谢谢你!真的非常感谢你。你回答了另一个问题,我需要用这个代码来回答。我真的很感激。我想我现在开始了解更多了,当我创建一个基本上类似于数据类型的飞机时,我需要在代码中描述该数据类型,而对于构造函数,我只是像普通方法一样对其进行编码。非常感谢比尔!不客气!我很乐意帮忙。请注意,您确实应该向飞机类添加文档,测试类应该是自文档化的。请随意接受我的回答