java - 如何从构造函数中获取实例对象?

标签 java constructor instance

Java 新手,我正在研究如何使用对象和类。在下面的示例中,我尝试创建一场赛车比赛,其中一些车手可能已被其他一些车手跟踪/跟随。特别是,我很难理解我是否正确使用了第二个构造函数。

public class CarDriver {
 // Here I define some features of that class
private String name;  
private int age;
// Here I create the first constructor
public CarDriver(String name, int age){
    this.age = age;
    this.name = name;
}
    // Here I create the second constructor, where I try to use an instance of CarDriver to involve a tracker later on.
public CarDriver(String name, int age, CarDriver tracker){
    tracker = new CarDriver(name,age);
    this.name = name;
    this.age = age;
}

public String getName(){
    return  this.name;
}
public int getAge(){
    return this.age;
}


// Issues starting here. Since I dont know how to extract the information about the tracker
public  getTracker(){
    return tracker
}
// Basically I would need that information to figure out whether some drivers are beeing pursued by others. 
public boolean hasPursuer(CarDriver driver)
{
    if (driver.getTracker() == 0){
      return true
    }  else {
      return false
    }  
}

public static void main(String[] args) {
    CarDriver driver1 = new CarDriver("Hamilton", 25);
    CarDriver driver2 = new CarDriver("Schumacher", 23, driver1);
    CarDriver driver3 = new CarDriver("Rosberg", 27, driver2);
    CarDriver driver4 = new CarDriver("Susi", 27, driver3);

    System.out.println(driver1.hasPursuer() + " " + driver2.hasPursuer());
    }
}

最佳答案

这是更正后的代码,解释在注释中:

public class CarDriver {

    private String name;  
    private int age;

    // You need to store the tracker as a field!
    private CarDriver tracker;

    public CarDriver(String name, int age){
        this.age = age;
        this.name = name;
    }

    public CarDriver(String name, int age, CarDriver tracker){
        this.name = name;
        this.age = age;
        // the above two lines can be simplfied to:
        // this(name, age);

        // assign the tracker passed in to the field
        this.tracker = tracker;
    }

    public String getName(){
        return  this.name;
    }
    public int getAge(){
        return this.age;
    }

    // Use CarDriver as the return type
    public CarDriver getTracker(){
        return tracker;
    }

    // I have edited this method slightly because it doesn't really makes sense
    // to do something like "driver.hasPursuer(driver)". It makes much more
    // sense to do "driver.hasPursuer()"
    public boolean hasPursuer()
    {
        // you don't actually need an if statement here. "!=" already evaluates to a boolean
        // also note that when there is no pursuers, the value is "null", not "0"
        return getTracker() != null;
    }

    public static void main(String[] args) {
        CarDriver driver1 = new CarDriver("Hamilton", 25);
        CarDriver driver2 = new CarDriver("Schumacher", 23, driver1);
        CarDriver driver3 = new CarDriver("Rosberg", 27, driver2);
        CarDriver driver4 = new CarDriver("Susi", 27, driver3);

        System.out.println(driver1.hasPursuer() + " " + driver2.hasPursuer());
    }

}

我认为您的误解是您没有意识到任何类都是“类型”。任何类都可以用作变量的类型或方法的返回类型。

关于java - 如何从构造函数中获取实例对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48016073/

相关文章:

Haskell - 匹配类型实例

java - 为什么我的 equals 方法无法识别对象变量?

Java深度克隆问题

c# - 如何在构造函数完成后立即调用方法?

c++ - 如何在 C++ 中使用基类的构造函数和赋值运算符?

java - 实现只有一个实例的类

centos - Openstack 实例 - 没有可启动设备

java - maven 打包app jar,创建项目类路径,目标

java - 具有 Spring 安全功能的 Spring Boot : Error creating bean with name 'securityFilterChainRegistration'

java - 在 Debug模式下运行 eclipse 是否比在 "normal"模式下运行需要更多内存?