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?
当我使用System.out.println(list.get(2))时似乎工作,但我不明白为什么?我作为实验将toString()函数添加到了不同的Vehicle类,但它仍然使用扩展的classe的toString()。什么时候会使用父亲的功能,什么时候是儿子?
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 () {...}
}
2 个解决方案
#1
3
Ofcourse you can make a List
that contains Vehicle
objects:
当然,你可以创建一个包含Vehicle对象的List:
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).
当你做System.out.println(list.get(2));然后它将在索引2处获取Vehicle对象并在其上调用toString(),然后将此字符串打印到控制台(您可以轻松地自己尝试)。
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(...)
.
请注意,如果要调用特定于Bus,Truck或Car的方法(即,在其中一个类中定义而不是在超类中定义),则在不转换列表结果的情况下无法执行此操作。得到(...)。
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.
您可以调用在类Vehicle中声明的任何方法(或在类Object中声明超类 - toString())而不进行强制转换,并且将调用适合特定对象(Bus,Truck或Car)的方法 - 这就是多态性的全部内容关于。
#2
3
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.
是的,只要差异是私密的,那么只要不进行类型转换就行。这将改变您将方法添加到未出现在界面中的方法的那一刻。
#1
3
Ofcourse you can make a List
that contains Vehicle
objects:
当然,你可以创建一个包含Vehicle对象的List:
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).
当你做System.out.println(list.get(2));然后它将在索引2处获取Vehicle对象并在其上调用toString(),然后将此字符串打印到控制台(您可以轻松地自己尝试)。
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(...)
.
请注意,如果要调用特定于Bus,Truck或Car的方法(即,在其中一个类中定义而不是在超类中定义),则在不转换列表结果的情况下无法执行此操作。得到(...)。
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.
您可以调用在类Vehicle中声明的任何方法(或在类Object中声明超类 - toString())而不进行强制转换,并且将调用适合特定对象(Bus,Truck或Car)的方法 - 这就是多态性的全部内容关于。
#2
3
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.
是的,只要差异是私密的,那么只要不进行类型转换就行。这将改变您将方法添加到未出现在界面中的方法的那一刻。