2016-07-24 42 views
-1

我目前正在设计两个班在Java中,和我遇到一些麻烦了解我目前的继承问题,所以我在希望创建一个简单的示例代码有人可以帮助我理解正在发生的事情。使用与子类的变量父类的方法,而不覆盖

因此,在这个例子中,一个父类(通用,我们可以把它叫做Car类)和子类(类雷德卡)。父类是抽象的,包含一个属性距离,并且包含一个computeVelocity方法,该方法使用输入参数时间(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); 
    } 
} 
+0

这不是真的我清楚你想在这里做什么,或者为什么该方法需要在所有覆盖。由于覆盖不会添加任何内容,为什么它没有?为什么不只是使用基类“computeVelocity”方法呢? – David

+1

你不应该在子类中有第二个距离字段。顺便说一下,基类的距离字段始终为0.只需在基类中有一个受保护的构造函数,并将距离作为参数,并从子类构造函数中调用它(使用'super(distance)')。 –

回答

0

至于你回答一些评论所说,你并不真正需要的,应该不会有距离两次(一次在父类中的一个例子,一次在儿童班)。使用父类的距离参数,没有理由重写你的方法。您可以使用super关键字调用您的父类方法。

希望这会有所帮助。

0

由于私有实例变量的子类继承,但在子类中不可访问(可以使用反射来访问它)。通过提供公共setter或getter方法或提供protected或public构造函数并从子类调用该构造函数,您有两种方法可以在Car实例变量private double distance中提供值。

见链接Do subclasses inherit private fields?继承,私人领域

构造函数,这在民营距离变量设置的值超类,因为只有有自己的成员访问私有成员通过他们自己的方法和构造手段。

protected Car(double distance){ 
    this.distance = distance; 
    } 

,你也没有必要,因为你没有在(子类)你的方法computeVelocity用它来在子类中再次定义的距离变量。由于子类方法调用超类方法而超类方法使用自己的私有双距离。所以你的距离变量在超类方法中没有使用的子类中定义。

提供超一流的受保护的构造后,你可以使用超级keyword.The super关键字调用父类的构造函数从子类构造函数调用它。由于我们在超类中提供了构造函数,因此java不会在超类中添加任何默认构造函数。并使用超私下双距离变量

 public RedCar(double inputDistance) { 
    super(inputDistance); 

     } 

和正确的方式分配值来调用父类的方法是,没有必要租车调用该构造从子类。超级

public double computeVelocity(double hours) { 
     return super.computeVelocity(hours); 
    } 
0

你的类继承RedCarCar。所以Car不只是另一类。如果创建RedCar实例,则还可以使用Car的所有部分。 A RedCarCar

在您的解决方案中,您已在超类和子类中创建了一个字段distance。实际上,您在每个RedCar实例中都有两个名为distance的字段。

这里是可视性的考虑。虽然私有变量distanceRedCar的一部分,但只能在类Car中看到。如果只有Car中定义的方法需要访问distance,则不需要更改可见性。但是你需要一种方法来设置distance。如果该值不随时间变化,则应在类Car中包含构造函数。

Car中定义的方法computeVelocity()的可见性是公开的,所以不需要在子类中重复它。由于RedCarCar,因此您可以在每个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其设置distance100.0

可以调用方法:

double velocity = redCar.computeVelocity(60.0);