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());
}
}
我认为你的错误观念是你没有意识到任何一个类都是“类型”。任何类都可以用作变量的类型或方法的返回类型。
您可以像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;
}