在 Java 中创建不同类型的列表
我有一个超类 Vehicle
和三个在其上扩展的类:Bus
、Car
和 Truck
。我想要一个包含不同类型车辆的链接列表,我使用它
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当然,您可以创建一个包含
Vehicle
对象的List
:当您执行
System.out.println(list.get(2));
时它将获取索引 2 处的Vehicle
对象并对其调用toString()
,然后将此字符串打印到控制台(您当然可以轻松地自己尝试一下) 。请注意,如果您想调用特定于
Bus
、Truck
或Car
的方法(即,在这些类之一中定义并且不在超类中),那么如果不转换list.get(...)
的结果就无法做到这一点。您可以调用类
Vehicle
中声明的任何方法(或超类 -toString()
在类Object
中声明),而无需进行强制转换,并且该方法适合特定对象(Bus
、Truck
或Car
)将被调用 - 这就是 多态性就是这样。Ofcourse you can make a
List
that containsVehicle
objects:When you do
System.out.println(list.get(2));
then it will get theVehicle
object at index 2 and calltoString()
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
orCar
(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 oflist.get(...)
.You can call any method that's declared in class
Vehicle
(or superclasses -toString()
is declared in classObject
) without casting, and the method appropriate for the specific object (Bus
,Truck
orCar
) will be called - that's what polymorphism is all about.是的,只要差异是私有的,无需类型转换就可以工作。当您将一种方法添加到界面中未出现的方法时,情况就会发生变化。
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.