简体   繁体   English

对超类 Object 的子类引用

[英]Subclass reference to Superclass Object

What is use of creating Subclass reference to Superclass Object创建对超类 Object 的子类引用有什么用

class A { /* ... */ }

class B extends A { /* ... */ }

public class Sample {
    public static void main(String a[]){
        A a = new B();
    }
}

One reason is so you can call methods that appear in the subclass that do not appear in/override the methods of the superclass.一个原因是你可以调用出现在子类中的方法,这些方法没有出现在/覆盖超类的方法。

eg例如

Class A (Vehicle) ~ superclass Class A (Vehicle) ~超类

Class B (Car) ~ subclass Class B (Car) ~子类

Vehicle has a method called getTopSpeed() , which can be called on any Vehicle. Vehicle 有一个名为getTopSpeed()的方法,可以在任何 Vehicle 上调用。 Car has a method called getTaxDiscExpiry() , which only makes sense for Cars but not for other vehicles such as Boats. Car 有一个名为getTaxDiscExpiry()的方法,该方法仅对 Cars 有意义,而对其他车辆(例如 Boats)无效。

Do you mean creating a "superclass reference to a subclass object?"您的意思是创建“对子类 object 的超类引用”? This question would seem to make more sense (and is what the code is doing) since you actually do (or potentially could) lose some functionality because of methods that aren't available in the superclass.这个问题似乎更有意义(并且是代码正在做什么),因为由于超类中不可用的方法,您实际上(或可能)失去了一些功能。

If so, it's because of abstraction - if you have lots of cars and you want them to drive() , you just need to treat them as cars.如果是这样,那是因为抽象 - 如果您有很多汽车并且您希望它们能够drive() ,您只需将它们视为汽车。 You don't need to find out and downcast to what type of car they are so you have additional methods available (might be massage() on some cars for instance.) Sure, downcasting in this case would get you more methods to play with but that's just unnecessary complexity that you're not going to use.您不需要找出并向下转换它们是什么类型的汽车,因此您可以使用其他方法(例如,在某些汽车上可能是massage() 。)当然,在这种情况下向下转换会为您提供更多的方法来玩但这只是您不会使用的不必要的复杂性。

It's also a common supertype - if you were iterating over a list of saabs, vauxhalls and fords you can put them all in a List<Car> and iterate over them as Cars .它也是一个常见的超类型 - 如果您正在迭代 saab、vauxhall 和福特的列表,您可以将它们全部放在List<Car>中并将它们作为Cars进行迭代。

At a top level you could just have a Drivable interface and reference all the cars with that.在顶层,您可以只拥有一个Drivable接口并使用它来引用所有汽车。 That way you could have anything that drives in the list example above - car or not - and you'd never need to know any more details than you had to, as well as broadening the scope to everything that drives, not just cars.)这样,您就可以在上面的列表示例中拥有任何可以驾驶的东西 - 是否有汽车 - 而且您永远不需要知道比您必须知道的更多细节,以及将 scope 扩展到所有可以驾驶的东西,而不仅仅是汽车。)

Its of no use in the code you have.它在您拥有的代码中没有用。 but there will be Situations where in但是会有一些情况在

many classes are extending single class许多类正在扩展单个 class

and a method some where in other class is called by both of these classes at that time以及当时这两个类都调用其他 class 的方法

its arguement type should be of its Super class type for Object Oriented programming to come handy...它的争论类型应该是它的超级 class 类型,用于 Object 面向编程派上用场......

in one word its all for PolyMorphism .. assume condition like below总之,它的全部用于PolyMorphism .. 假设条件如下

Class A
{
void f1(Super s){

}
}

class B extends Super
{

void f2(){
new A().f1(new B())
}
}

class A extends Super
{

void f2(){
new A().f1(new A())
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM