Java超类从子类获取参数值
问题是: 1) 如何将子类参数值获取到超类 2) 我想使用collection.sort的名称(来自子类),如下面的.model.compareTo(other.model)。但是,我不知道如何从子类中获取“Name”值并执行collection.sort **写公共int比较(汽车其他、出租车其他)正确吗** 代码如下:Java超类从子类获取参数值,java,subclass,superclass,Java,Subclass,Superclass,问题是: 1) 如何将子类参数值获取到超类 2) 我想使用collection.sort的名称(来自子类),如下面的.model.compareTo(other.model)。但是,我不知道如何从子类中获取“Name”值并执行collection.sort **写公共int比较(汽车其他、出租车其他)正确吗** 代码如下: public class Car implement comparable <Car>() { private string model; privat
public class Car implement comparable <Car>()
{
private string model;
private int price;
public car(String model , int price)
{
this.model=model;
this.price=price;
}
............some getmethod here..........
public int compareTo (Car other)
{
** Want to sort by name , like this.model.compareTo(other.model)**
}
}
您可以通过在超类中创建子类的对象来使用子类值。
它也称为委派。如果您的超类不使用“名称”,那么为什么需要在那里定义它?也许我只想在超类中使用名称。。。。但是我不知道如何调用要在超类中使用的名称值,因为我需要使用“this.name.compareTo(other.name)”进行集合排序。抱歉,键入错误。他只是忘记了扩展名。请告诉我们更多关于您的收藏分类。为什么它需要在车里而不是在出租车里?你的意思是出租车=新出租车()?但是它不应该在里面添加参数吗?我可以把新的instancein超类放在哪里。做一个汽车类的物体。
public class taxi extends Car ()
{
private string name;
public taxi (String model , int price, String name)
{
super(model, price);
this.name = name;
}
......some getmethod here..........
}