在 Java 中创建不同类型的列表

发布于 2024-11-16 06:22:58 字数 1525 浏览 2 评论 0原文

我有一个超类 Vehicle 和三个在其上扩展的类:BusCarTruck。我想要一个包含不同类型车辆的链接列表,我使用它

list = new LinkedList<Vehicle>()

,当我使用 System.out.println(list.get(2)) 时它似乎可以工作,但我不这样做不明白为什么?我已将 toString() 函数添加到不同的 Vehicle 类中作为实验,但它仍然使用扩展类的 toString()。什么时候用父亲的功能,什么时候用儿子的功能?

所有不同的类都有相同的功能,但私有变量不同。

类别是:

public class Vehicle {

    protected String maker;
    protected int year;
    private int fuel; //0 1 2 3 4

    public Vehicle (String maker, int year) {
        this.maker = maker;
        this.year = year;
        this.fuel = 0;
    }

    public void drive () {...}

    public void fill () {...}
}

公共汽车:

public class Bus extends Vehicle{

    private int sits;

    public Bus (String maker, int year, int sits) {
        super (maker, year);
        this.sits = sits;
    }

    public String toString () {...}
}

卡车:

public class Truck extends Vehicle{

    private int weight;

    public Truck (String maker, int year, int weight) {
        super (maker, year);
        this.weight = weight;
    }

    public String toString () {...}
}

汽车:

public class Car extends Vehicle{

    private float volume;

    public Car (String maker, int year, float volume) {
        super (maker, year);
        this.volume = volume;
    }

    public String toString () {...}
}

I have a super class Vehicle and three classes that extend on it: Bus, Car and Truck. I want to have a linked list that will contain vehicles of different types, I use

list = new LinkedList<Vehicle>()

and it seems to work when I use System.out.println(list.get(2)), but I don't understand why? I've added as an experiment toString() function to Vehicle class which is deferent, but it still uses the extended classe's toString(). When will it use the father's function, and when it's sons?

All the different classes have the same functions, but different private variables.

the classes are:

public class Vehicle {

    protected String maker;
    protected int year;
    private int fuel; //0 1 2 3 4

    public Vehicle (String maker, int year) {
        this.maker = maker;
        this.year = year;
        this.fuel = 0;
    }

    public void drive () {...}

    public void fill () {...}
}

Bus:

public class Bus extends Vehicle{

    private int sits;

    public Bus (String maker, int year, int sits) {
        super (maker, year);
        this.sits = sits;
    }

    public String toString () {...}
}

Truck:

public class Truck extends Vehicle{

    private int weight;

    public Truck (String maker, int year, int weight) {
        super (maker, year);
        this.weight = weight;
    }

    public String toString () {...}
}

Car:

public class Car extends Vehicle{

    private float volume;

    public Car (String maker, int year, float volume) {
        super (maker, year);
        this.volume = volume;
    }

    public String toString () {...}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

黄昏下泛黄的笔记 2024-11-23 06:22:58

当然,您可以创建一个包含 Vehicle 对象的 List

List<Vehicle> list = new LinkedList<Vehicle>();

当您执行 System.out.println(list.get(2)); 时它将获取索引 2 处的 Vehicle 对象并对其调用 toString() ,然后将此字符串打印到控制台(您当然可以轻松地自己尝试一下) 。

请注意,如果您想调用特定于 BusTruckCar 的方法(即,在这些类之一中定义并且不在超类中),那么如果不转换 list.get(...) 的结果就无法做到这一点。

您可以调用类 Vehicle 中声明的任何方法(或超类 - toString() 在类 Object 中声明),而无需进行强制转换,并且该方法适合特定对象(BusTruckCar)将被调用 - 这就是 多态性就是这样。

Ofcourse you can make a List that contains Vehicle objects:

List<Vehicle> list = new LinkedList<Vehicle>();

When you do System.out.println(list.get(2)); then it will get the Vehicle object at index 2 and call toString() on it, and then this string is printed to the console (you can ofcourse easily try that out yourself).

Note that if you want to call a method that is specific to a Bus, Truck or Car (i.e., defined in one of those classes and not in a superclass), then there is no way to do that without casting the result of list.get(...).

You can call any method that's declared in class Vehicle (or superclasses - toString() is declared in class Object) without casting, and the method appropriate for the specific object (Bus, Truck or Car) will be called - that's what polymorphism is all about.

内心激荡 2024-11-23 06:22:58

是的,只要差异是私有的,无需类型转换就可以工作。当您将一种方法添加到界面中未出现的方法时,情况就会发生变化。

Yes, that'll work without type casting as long as the differences are private. That'll change the moment you add a method to one that doesn't appear in the interface.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文