javaoop-打印组合bean信息的问题

kx1ctssn  于 2021-06-29  发布在  Java
关注(0)|答案(1)|浏览(308)

我有一个任务来创造一个种族。我为工厂方法创建了一个类,并为每个字段生成随机值,除了我的奖牌信息之外,一切似乎都正常。在生成比赛时,除了“奖牌”之外,我得到了所有的字符串值,我似乎找不到它的原因。
我的代码

public class RaceUtils {

// Create new Medals Method
public static Medal generateMedals() {
    int position = (int) ((Math.random() * 3) + 1);

    Medal medal = new Medal(position, "1500");
    return medal;

}

// Create new Driver Method
public static Driver generateDriver() {
    Medal[] medals = new Medal[4];
    int age = (int) (Math.random() * 30) + 10;
    for (int i = 0; i < medals.length; i++) {
        medals[i] = generateMedals();

    }

    return new Driver("Dan", age, medals);

}

// Create new Car Method
public static Car generateCar() {
    Driver[] drivers = new Driver[2];
    for (int i = 0; i < drivers.length; i++) {
        drivers[i] = generateDriver();

    }

    CarModels carModels = CarModels.values()[(int) (Math.random() * CarModels.values().length)];
    int year = (int) (Math.random() * 1990 - 2000) + 1990;

    return new Car(carModels, year, drivers);

}

// Create new Circuit Method
public static Circuit generateCircuit() {
    Car[] cars = new Car[10];
    for (int i = 0; i < cars.length; i++) {
        cars[i] = generateCar();

    }
    CircuitName names = CircuitName.values()[(int) (Math.random() * CircuitName.values().length)];
    Country country = Country.values()[(int) (Math.random() * Country.values().length)];
    int length = (int) (Math.random() * 30) + 10;

    return new Circuit(names, country, length, cars);

}

// Print Method
public static void print(Circuit[] circuit) {
    for (int i = 0; i < circuit.length; i++) {
        System.out.println(circuit[i].toString());

    }
}

public class Medal {

private int position;
private String string;

public Medal(int position, String string) {
    setPosition(position);
    this.string = string;
}

public int getPosition() {
    return position;
}

public void setPosition(int position) {
    if (this.position > 0 || this.position <= 3) {
        this.position = position;
    } else {
        System.out.println("Position is out of range");
    }
}

public String getString() {
    return string;
}

public void setString(String string) {
    this.string = string;
}

@Override
public String toString() {
    return "Medal [position=" + position + ", string=" + string + "]";
}

  public class Driver {

private String name;
private int age;
private Medal[] medals;

public Driver(String name, int age, Medal[] medals) {
    this.name = name;
    this.age = age;
    this.medals = medals;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public int getAge() {
    return age;
}

public void setAge(int age) {
    this.age = age;
}

public Medal[] getMedals() {
    return medals;
}

public void setMedals(Medal[] medals) {
    this.medals = medals;
}

@Override
public String toString() {
    return "Driver: Name = " + name + ", Age = " + age + ", Medals = " + medals;
}

public class Car {

private CarModels models;
private int year;
private Driver[] drivers;

public Car(CarModels models, int year, Driver[] drivers) {
    this.models = models;
    this.year = year;
    this.drivers = drivers;
}

public CarModels getModels() {
    return models;
}

public void setModels(CarModels models) {
    this.models = models;
}

public int getYear() {
    return year;
}

public void setYear(int year) {
    this.year = year;
}

public Driver[] getDrivers() {
    return drivers;
}

public void setDrivers(Driver[] drivers) {
    this.drivers = drivers;
}

@Override
public String toString() {
    return "Car: Model = " + models + ", Car Year = " + year + ", Drivers: " + Arrays.toString(drivers);
}

public class Circuit {

private CircuitName names;
private Country country;
private int length;
private Car[] cars;

public Circuit(CircuitName names, Country country, int length, Car[] cars) {
    this.names = names;
    this.country = country;
    this.length = length;
    this.cars = cars;
}

public CircuitName getNames() {
    return names;
}

public void setNames(CircuitName names) {
    this.names = names;
}

public Country getCountry() {
    return country;
}

public void setCountry(Country country) {
    this.country = country;
}

public int getLength() {
    return length;
}

public void setLength(int length) {
    this.length = length;
}

public Car[] getCars() {
    return cars;
}

public void setCars(Car[] cars) {
    this.cars = cars;
}

@Override
public String toString() {
    return "Circuit: Circuit Name: " + names + ", Country: " + country + ", Circuit Length: " + length
            + " Kilometers" + Arrays.toString(cars);
}

public class RaceProgram {

private static final int LEN = 4;

public static void main(String[] args) {

    Circuit[] circuit = new Circuit[LEN];

    for (int i = 0; i < circuit.length; i++) {
        circuit[i] = RaceUtils.generateCircuit();

    }

    RaceUtils.print(circuit);

}

我得到了什么
赛道:赛道名称:usaraceopen,国家:美国,赛道长度:13公里[车:车型=三菱,车年=1457,车手:[车手:姓名=丹,年龄=12,奖牌=[loop.classes.nir\u ex.ex14\u race.medal@782830e,车手:姓名=丹,年龄=20,奖牌=[loop.classes.nir\u ex.ex14\u race.dedal@470e2030],汽车:车型=法拉利,汽车年份=1831,驾驶员:[驾驶员:姓名=丹,年龄=37,奖牌=[loop.classes.nir\u ex.ex14\u race.medal@3fb4f649,车手:姓名=丹,年龄=21,奖牌=[loop.classes.nir\u ex.ex14\u race.medal@33833882],汽车:型号=马自达,汽车年份=628,驾驶员:[驾驶员:姓名=丹,年龄=27,奖牌=[loop.classes.nir\u ex.ex14\u race.madal@200a570f,车手:姓名=丹,年龄=19,奖牌=[loop.classes.nir\u ex.ex14\u race.dedal@16b3fc9e],汽车:车型=菲亚特,车年=1002,驾驶员:[驾驶员:姓名=丹,年龄=19,奖牌=[loop.classes.nir\u ex.ex14\u race.medal@e2d56bf,车手:姓名=丹,年龄=10,奖牌=[loop.classes.nir\u ex.ex14\u race.medal@244038d0],汽车:车型=法拉利,汽车年份=441,驾驶员:[驾驶员:姓名=丹,年龄=22,奖牌=[loop.classes.nir\u ex.ex14\u race.madal@5680a178,车手:姓名=丹,年龄=24,奖牌=[loop.classes.nir\u ex.ex14\u race.medal@5fdef03a],汽车:车型=菲亚特,车年=1651,驾驶员:[驾驶员:姓名=丹,年龄=30,奖牌=[loop.classes.nir\u ex.ex14\u race.madal@3b22cdd0,车手:姓名=丹,年龄=16,奖牌=[loop.classes.nir\u ex.ex14\u race.medal@1e81f4dc],汽车:型号=铃木,汽车年份=602,驾驶员:[驾驶员:姓名=丹,年龄=38,奖牌=[loop.classes.nir\u ex.ex14\u race.medal@4d591d15,车手:name=dan,age=32,奖牌=[loop.classes.nir\u ex.ex14\u race.dedal@65ae6ba4],汽车:车型=菲亚特,汽车年份=982,驾驶员:[驾驶员:姓名=丹,年龄=31,奖牌=[loop.classes.nir\u ex.ex14\u race.madal@48cf768c,车手:姓名=丹,年龄=18,奖牌=[loop.classes.nir\u ex.ex14\u race.medal@59f95c5d],汽车:车型=铃木,汽车年份=693,驾驶员:[驾驶员:姓名=丹,年龄=24,奖牌=[loop.classes.nir\u ex.ex14\u race.madal@5CD43C2,车手:姓名=丹,年龄=21,奖牌=[loop.classes.nir\u ex.ex14\u race.dedal@4aa8f0b4],汽车:型号=三菱,汽车年份=1595,驾驶员:[驾驶员:姓名=丹,年龄=37,奖牌=[loop.classes.nir\u ex.ex14\u race.madal@7960847b,车手:姓名=丹,年龄=17,奖牌=[loop.classes.nir\u ex.ex14\u race.dedal@6a6824be]]
我需要什么
如你所见,我把奖牌的所有信息都分开了。

col17t5w

col17t5w1#

问题出在driver类中,请尝试将方法tostring更改为如下内容

public class Driver {
    @Override
    public String toString() {
        return "Driver: Name = " + name + ", Age = " + age + ", Medals = " + Arrays.toString(medals);
    }
}

在所有类中使用array.tostring来显示数组的内容,但在驱动程序类中使用array.tostring。
输出

Circuit: Circuit Name: null, Country: null, Circuit Length: 17 Kilometers[Car: Model = null, Car Year = 65, Drivers: [Driver: Name = Dan, Age = 12, Medals = [Medal [position=3, string=1500], Medal [position=1, string=1500], Medal [position=3, string=1500], Medal [position=1, string=1500]], Driver: Name = Dan, Age = 22, Medals = [Medal [position=1, string=1500], Medal [position=2, string=1500], Medal [position=3, string=1500], Medal [position=1, string=1500]]], Car: Model = null, Car Year = 1747, Drivers: [Driver: Name = Dan, Age = 18, Medals = [Medal [position=1, string=1500], Medal [position=1, string=1500], Medal [position=2, string=1500], Medal [position=3, string=1500]], Driver: Name = Dan, Age = 24, Medals = [Medal [position=1, string=1500], Medal [position=3, string=1500], Medal [position=2, string=1500], Medal [position=3, string=1500]]], Car: Model = null, Car Year = 1781, Drivers: [Driver: Name = Dan, Age = 18, Medals = [Medal [position=2, string=1500], Medal [position=1, string=1500], Medal [position=1, string=1500], Medal [position=3, string=1500]], Driver: Name = Dan, Age = 21, Medals = [Medal [position=2, string=1500], Medal [position=3, string=1500], Medal [position=3, string=1500], Medal [position=1, string=1500]]], Car: Model = null, Car Year = 28, Drivers: [Driver: Name = Dan, Age = 16, Medals = [Medal [position=3, string=1500], Medal [position=3, string=1500], Medal [position=2, string=1500], Medal [position=3, string=1500]], Driver: Name = Dan, Age = 39, Medals = [Medal [position=2, string=1500], Medal [position=3, string=1500], Medal [position=3, string=1500], Medal [position=3, string=1500]]], Car: Model = null, Car Year = 1789, Drivers: [Driver: Name = Dan, Age = 37, Medals = [Medal [position=1, string=1500], Medal [position=3, string=1500], Medal [position=3, string=1500], Medal [position=3, string=1500]], Driver: Name = Dan, Age = 38, Medals = [Medal [position=3, string=1500], Medal [position=2, string=1500], Medal [position=2, string=1500], Medal [position=3, string=1500]]], Car: Model = null, Car Year = 1413, Drivers: [Driver: Name = Dan, Age = 30, Medals = [Medal [position=2, string=1500], Medal [position=1, string=1500], Medal [position=1, string=1500], Medal [position=3, string=1500]], Driver: Name = Dan, Age = 18, Medals = [Medal [position=3, string=1500], Medal [position=2, string=1500], Medal [position=2, string=1500], Medal [position=1, string=1500]]], Car: Model = null, Car Year = 965, Drivers: [Driver: Name = Dan, Age = 33, Medals = [Medal [position=1, string=1500], Medal [position=1, string=1500], Medal [position=2, string=1500], Medal [position=3, string=1500]], Driver: Name = Dan, Age = 34, Medals = [Medal [position=1, string=1500], Medal [position=2, string=1500], Medal [position=3, string=1500], Medal [position=1, string=1500]]], Car: Model = null, Car Year = 1380, Drivers: [Driver: Name = Dan, Age = 38, Medals = [Medal [position=3, string=1500], Medal [position=3, string=1500], Medal [position=2, string=1500], Medal [position=3, string=1500]], Driver: Name = Dan, Age = 16, Medals = [Medal [position=1, string=1500], Medal [position=1, string=1500], Medal [position=2, string=1500], Medal [position=2, string=1500]]], Car: Model = null, Car Year = 581, Drivers: [Driver: Name = Dan, Age = 31, Medals = [Medal [position=2, string=1500], Medal [position=2, string=1500], Medal [position=1, string=1500], Medal [position=1, string=1500]], Driver: Name = Dan, Age = 34, Medals = [Medal [position=1, string=1500], Medal [position=1, string=1500], Medal [position=3, string=1500], Medal [position=3, string=1500]]], Car: Model = null, Car Year = 429, Drivers: [Driver: Name = Dan, Age = 12, Medals = [Medal [position=2, string=1500], Medal [position=2, string=1500], Medal [position=2, string=1500], Medal [position=1, string=1500]], Driver: Name = Dan, Age = 28, Medals = [Medal [position=2, string=1500], Medal [position=2, string=1500], Medal [position=1, string=1500], Medal [position=2, string=1500]]]]

相关问题