简体   繁体   English

在 Java 中,您可以将子类的对象存储为超类类型,为什么要这样做?

[英]In java you're able to store objects of a subclass as a super-class type, why would you do this?

Why would you declare the "thing" object as the super-class when you can use the subclass, which would give you access to all of the same methods and fields and wouldn't require type casting for methods in the B class.当您可以使用子类时,为什么要将“事物”对象声明为超类,这将使您可以访问所有相同的方法和字段,并且不需要对 B 类中的方法进行类型转换。

public class A{}
public class B extends A{}

public class main()
{
  A thing = new B();
}

This is called Polymorphism.这称为多态性。 If you had another class called C extends A you could create a List<A> and put both B and C there.如果您有另一个名为C extends A类,您可以创建一个List<A>并将BC放在那里。 Then you could iterate over them and call the common method etc.然后你可以迭代它们并调用通用方法等。

Maybe because you want to feed() several Animal s at same time, without caring about the real type of Animal :也许是因为你想同时feed()几个Animal ,而不关心真正的Animal类型:

interface Animal { void feed();}
class Dog implements Animal { public void feed() { /* feed a dog (give it a cat) */ }}
class Cat implements Animal { public void feed() { /* feed a cat (give it a bird) */ }}
class Cow implements Animal { public void feed() { /* feed a cow (give it some grass) */ }}

// Now I have some animals mixed somewhere (note that I am allowed to have the array declaring a supertype (Animal), and it can contain many kind of different animals)
Animal[] manyAnimals = new Animal[]{ new Dog(), new Cat(), new Cow() };

// I can feed them all without knowing really what kind of Animal they are. I just know they are all Animal, and they will all provide a feed() method.
for(Animal some : manyAnimals) { some.feed(); }

It is polymorphism.它是多态性。

This example might help you understand it.这个例子可能会帮助你理解它。

In a company there are both Permanent and Contract employees are there.在公司中,既有固定员工也有合同员工。 The salary calculations happen differently for different type of employee.不同类型员工的工资计算方式不同。 But PF calculation is common for both type of employees.但是 PF 计算对于这两种类型的员工都很常见。 So in this case you can write common code in super class(Employee) and only custom code in sub class(PermanentEmployee and ContractEmployee).因此,在这种情况下,您可以在超类(Employee)中编写通用代码,而在子类(PermanentEmployee 和 ContractEmployee)中仅编写自定义代码。 This way you can make code reusable instead of writing again and again and also you can achieve dynamic polymorphism.通过这种方式,您可以使代码可重用,而不是一次又一次地编写,并且您还可以实现动态多态性。 Most of the time the type of employee is decided at run time.大多数情况下,员工的类型是在运行时决定的。

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

相关问题 如果超类没有一个默认构造函数,为什么此Java代码中的子类具有默认构造函数? - Why does the subclass in this Java code have a default constructor if the super-class does not have one? 当引用类型为超类时,子类无法访问超类的受保护方法 - Sub-class not able to access the protected method of super-class when the reference is of type super-class java抽象类,构造器未在超类中调用,为什么? - java abstract class, Constructor not called in super-class, Why? Java-您可以从父类访问在子类中声明的枚举吗? - Java - Can you access an Enum declared in Subclass from Super class? 如何创建子类,以便参数属于Java中的子类类型 - How do you create a subclass so that the parameters are of the subclass type in Java 如何在子类上下文中执行从超类继承的方法? - How to execute methods inherited from super-class in the subclass context? Java:为什么需要构造函数来使用父类中的对象? - Java: Why do you need constructors to use objects in a parent class? 如何在 HttpSession 中存储 Java 对象? - How do you store Java objects in HttpSession? 为什么不能在 Java 中将类声明为静态类? - Why are you not able to declare a class as static in Java? 为什么Java中的子类是超类的实例但不能分配给它? - Why is a subclass in Java an instance of the super class but not assignable to it?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM