将子类或超类对象传递给方法(期望超类对象) [英] Passing subclass or superclass object to method (expecting a superclass object)

查看:142
本文介绍了将子类或超类对象传递给方法(期望超类对象)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一些需要将超类或子类对象发送到方法的代码。

I'm working with some code that needs to send either a superclass or subclass object to a method.

方法 public void repair (车辆车辆)将仅访问超类对象中的方法。

The method public void repair(Vehicle vehicle) will ONLY access methods in the super class object.

public class Vehicle {
    //class stuff
}

public class Car extends Vehicle {
    //class stuff
}

public static void main(String[] args) 
{
    // do stuff to determine whether working with a vehicle or car
    if (someCondition)
    {
        Car = new Car();
        // do some stuff...
        repair(Car);
    }
    else
    {
        Vehicle = new Vehicle();
        // do some stuff...
        repair(Vehicle);
    }
}   

我想我有三个选择:


  1. 保持代码不变,它似乎正常工作。 - 我不喜欢这个选项,感觉我正在做出假设,我怀疑汽车只有方法可能被意外调用这样做,导致意外行为。

  2. 在car中创建一个getVehicle()方法,以返回一个Vehicle。然后使用修复(Car.getVehicle()); - 感觉好一点

  3. 更改 Car = new Car(); to Vehicle = new Car(); 我相信它会创建一个只能执行类型方法的对象(车辆)车辆。 - 这感觉最安全,因为我现在限制可以做的事情,以防止意外行为。

  1. Leave the code as it is, it seems to be working. - I don't like this option, it feels like I'm making assumptions and I suspect car only methods could be accidentally called doing this, leading to unexpected behaviour.
  2. Create a getVehicle() method in car, to return a Vehicle. Then use repair(Car.getVehicle()); - this feels a little better
  3. Change Car = new Car(); to Vehicle = new Car(); which I believe would create an object (vehicle) that can only perform methods of type vehicle. - This feels the safest, as I'm now restricting what can be done, to prevent unexpected behaviour.

是3,最好方法,因为修理方法只是期待车辆吗?

Is 3, the best approach, given that the repair method is only ever expecting vehicles?

此外,我能做什么/应该做什么: public void repair (车辆车辆)方法声明?

Also, is there anything I could/should to to the: public void repair(Vehicle vehicle) method declaration?

编辑:我似乎应该使用:

It seems I should be using:


保留代码原样

Leave the code as it is

因为 repair() 方法无论如何都将子类对象强制转换为超类对象。

since the repair() method casts the subclass object to a superclass object anyway.

推荐答案

没有修复的定义,但我想你想要这样的东西

There is no definition of repair but I think you want something like this

public abstract class Vehicle {
    //class stuff
}

public class Car extends Vehicle {
   //class stuff
}


public class Garage {
   public void repair(Vehicle vehicle){
   ....
   }
}

然后你可以将任何子类的Vehicle传递给修复方法。在这种情况下,它只是汽车,但你可以延伸到自行车,摩托车等。

Then you can pass any subclass of Vehicle to the repair method. In this case it is only Car but you could extend to have bike, motorcycle etc.

现在你不需要检查if语句。你可以将你的对象(或Car或其他任何东西)传递给 repair 方法。

Now you will not need to check with an if statement. You can just pass your object (or Car or anything else) into the repair method.

你刚刚成为

public static void main(String[] args)  {
    Garage g = new Garage();
    Vehicle c = new Car();
    Vehicle b = new Bike(); //assuming Bike is a subclass of Vehicle.
    g.repair(c);
    g.repair(b);
}  

如果在访问变量b和c时需要Car and Bike特定方法那么你可以将其声明更改为

If when accessing variable b and c you need Car and Bike specific method then you can change their declarations to be

Car c = new Car();
Bike b = new Bike();

这篇关于将子类或超类对象传递给方法(期望超类对象)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆