[英]Using a method of parent class with variable of child class without overriding
我目前正在用Java设计两个类,并且在理解我当前的继承问题时遇到了一些麻烦,因此我创建了一个简单的示例代码,希望有人可以帮助我理解正在发生的事情。
因此,在此示例中,父类(出于通用目的,我们可以将其称为Car类)和子类(RedCar类)。 父类是抽象的,包含一个属性距离,并包含一个computeVelocity方法,该方法采用输入参数时间(双精度),并返回距离的双精度值除以时间(从而得出速度)。
子类具有构造函数RedCar(double inputDistance)。 它也应该可以访问computeVelocity方法,而不要覆盖它。
这就是问题所在。 从逻辑上讲,我知道computeVelocity不应从RedCar中获取inputDistance变量,因为它调用了另一个类的方法。 但是,我想知道的是如何将该方法引入RedCar类,以便可以在传递给构造函数的参数的情况下在该类中使用它。
这是代码示例
//Car Class
public abstract class Car{
//abstract variable
private double distance;
//compute velocity method
public double computeVelocity(double time){
return distance/time;
}
}
//RedCar Class
public class RedCar extends Car{
public double distance;
public RedCar (double inputDistance){
this.distance = inputDistance;
}
//This is currently my method but I believe this already constitutes as overridding
public double computeVelocity(double hours){
Car.super.computeVelocity(hours);
}
}
正如对您的回答的一些评论中所述,您实际上并不需要两次间隔(一次在父类中,一次在子类中)。 使用父类的distance参数,没有理由覆盖您的方法。 您可以使用super
关键字调用父类方法。
希望这可以帮助。
由于私有实例变量是在子类中继承的,但不能在子类中访问(您可以使用反射来访问它)。 通过提供公共setter或getter方法或提供受保护的或公共的构造函数并从子类调用该构造函数,您可以通过两种方式在Car实例变量私有双倍距离中提供值。
参见链接子类是否继承私有字段? 继承私有字段
超类中的构造函数会在私有距离变量中设置值,因为私有成员只能通过其自身的成员通过自己的方法和构造函数来访问。
protected Car(double distance){
this.distance = distance;
}
同样,您也不需要在子类中再次定义距离变量,因为您没有在(子类的)方法computeVelocity中使用它。 由于子类方法调用,因此超类方法和超类方法使用自己的私有双倍距离。 因此,您的距离变量在超类方法未使用的子类中定义。
在超类中提供受保护的构造函数后,您可以使用super关键字从子类构造函数中调用它。super关键字调用超类的构造函数。 由于我们在超类中提供了构造函数,因此java不会在超类中添加任何默认的构造函数。 并使用super从子类调用该构造函数,将值分配给私有double distance变量
public RedCar(double inputDistance) {
super(inputDistance);
}
调用super类方法的正确方法是,不需要Car.super
public double computeVelocity(double hours) {
return super.computeVelocity(hours);
}
您的RedCar
类继承了Car
类。 因此, Car
不仅是另一类。 如果创建RedCar
实例,则Car
所有部分也都可用。 RedCar
是 Car
。
在您的解决方案中,您已经在父类和子类中创建了一个场distance
。 实际上,每个RedCar
实例中都有两个名为distance
字段。
这里考虑了可见性。 尽管私有变量distance
是RedCar
一部分,但仅在Car
类中可见。 如果仅Car
定义的方法需要访问distance
,则无需更改可见性。 但是您需要一种设置distance
。 如果该值不随时间变化,则应在Car
类中包含一个构造函数。
Car
定义的方法computeVelocity()
的可见性是公共的,因此无需在子类中重复它。 因为RedCar
是Car
您可以在每个RedCar
实例上调用computeVelocity()
。
public abstract class Car{
private double distance;
// Constructor with visibility protected.
// So it is visible in each subclass and must be
// called by each subclass constructor.
// Btw.: It is common practice to use the same name
// for input parameters as for fields. The field
// variable can be accessed with the "this" keyword.
protected Car(double distance) {
this.distance = distance;
}
//compute velocity method
public double computeVelocity(double time){
return distance/time;
}
}
public class RedCar extends Car{
public double distance;
public RedCar (double distance){
// Call the Car constructor
super(distance)
}
// No need to repeat the definition of
// computeVelocity unless you want to redefine
// the behaviour..
}
然后,当您创建RedCar
实例时:
RedCar redCar = new RedCar(100.0);
首先调用RedCar
构造函数,然后调用Car
构造函数,将distance
字段设置为100.0
。
您可以调用该方法:
double velocity = redCar.computeVelocity(60.0);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.