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());
}
}
答案 0 :(得分:2)
以下是更正的代码,解释见注释:
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());
}
}
我认为您的错误观念是您没有意识到任何类都是“类型”。任何类都可以用作变量的类型或方法的返回类型。
答案 1 :(得分:0)
您可以采用与tracker
和name
相同的方式处理age
,其中存储了tracker
的引用,以便您可以在CarDriver
中使用它类
private CarDriver tracker;
public CarDriver(String name, int age, CarDriver tracker){
this.tracker = tracker;
this.name = name;
this.age = age;
}
public CarDriver getTracker(){
return tracker;
}