【问题标题】:How do I create an instance of another class, load an object with data and print it out?如何创建另一个类的实例,用数据加载对象并将其打印出来?
【发布时间】:2014-12-08 02:43:17
【问题描述】:

我是一个完全的菜鸟(第 5 天编程),这解释了为什么我花了无数令人头疼的时间试图解决这个问题,但仍然没有弄清楚:

如何创建 CarAndBikes 的实例,将 3 辆汽车的信息加载并打印出来?

这是我的不完整代码,可让您了解问题:

    public class Vehicle {

        String manufacturer, model;
        int numberOfWheels;

        public Vehicle(String manufacturer, String model, int numberOfWheels) {

            this.manufacturer = manufacturer;
            this.model = model;
            this.numberOfWheels = numberOfWheels;
        }

        public String getManufacturer() {
            return manufacturer;
        }

        public String getModel() {
            return model;
        }

        public int getNumberOfWheels() {
            return numberOfWheels;
        }

        public String toString() {
            return "(" + numberOfWheels +") '" + manufacturer + ", " + model + "'";
        }
    }

    public class CarAndBikes {

        private Vehicle[] items;
        private int nextFreeItem = 0;

        CarAndBikes (int size) {
            items = new Vehicle[size];

            for(int i = 0; i < size; i++)
            items[i] = new Vehicle(manufacturer, model, numberOfWheels);
        }

        void addVehicle(String man, String mdl, int wheels) {
            items[nextFreeItem++].addVehicle(man, mdl, wheels);
        }

        public String toString() {
            return "(" + items + ")";
        }
    }

public class TestProgram extends CarAndBikes{
    TestProgram(int size) {
        super(size);
    }

    public static void main(String[] args) {
        Vehicle vehicle1 = new Vehicle("Seat", "Ibiza", 4);
        Vehicle vehicle2 = new Vehicle("Reliant", "Robin", 3);
        Vehicle vehicle3 = new Vehicle("Honda", "Fireblade", 2);

        System.out.println(vehicle1);
        System.out.println(vehicle2);
        System.out.println(vehicle3);
    }
}

【问题讨论】:

  • 我在 Vehicle 类中看不到任何 addVehicle() 方法。你的代码还能编译吗?
  • 将汽车和自行车分成单独的类,然后有一个类来存储它们的集合不是更容易吗?

标签: java constructor instance setter getter


【解决方案1】:

在我看来,你最好将汽车和自行车分成两个单独的类“汽车”和“自行车”,然后创建一个第三类来容纳这些对象的集合,这样你就可以使用 List 或 ArrayList 来存储它们并有一种方法可以将它们打印出来。

import java.util.ArrayList;

Public Class Garage
{
   private ArrayList<Vehicle> vehicles;

   Public Garage()
   {
     vehicles = new ArrayList<Vehicle>();
   }

   public void addVehicle(Vehicle v)
   {
      vehicles.add(v);
   }

   public void getVehicles()
   {
     for(Vehicle v : vehicles)
     {
       System.out.Println(v.getModel());
     }
   }
}

您会注意到 ArrayList 接受车辆类型的对象,汽车和自行车都扩展了车辆,因此它们将被接受。

【讨论】:

    【解决方案2】:

    我注意到CarAndBikes 中的toString() 方法在数组引用上使用toString()(并且数组不会覆盖toString())。你可以使用Arrays.toString(Object[]) 喜欢

    public String toString() {
        // return "(" + items + ")";
        return Arrays.toString(items);
    }
    

    您获得的Object.toString() 记录为,

    toStringObject 的方法返回一个字符串,该字符串由对象作为其实例的类的名称、at 符号字符“@”和哈希码的无符号十六进制表示形式组成。物体。换句话说,这个方法返回一个字符串等于:

    getClass().getName() + '@' + Integer.toHexString(hashCode())
    

    【讨论】:

    • 我不明白它有什么问题。你能帮我吗?我已经编辑了代码,但 CarAndBikes (int size) { AND items[nextFreeItem++].addVehicle(man, mdl,wheels); 仍然存在错误
    猜你喜欢
    • 1970-01-01
    • 2014-12-05
    • 1970-01-01
    • 1970-01-01
    • 2021-04-30
    • 2019-03-26
    • 2018-03-30
    • 2011-04-14
    • 2013-09-13
    相关资源
    最近更新 更多