[英]Java generics and polymorphism
What is polymorphism in java? java中的多态是什么? I am trying to understand this with generics.我试图用泛型来理解这一点。 Let us consider the following class Pair.让我们考虑以下类 Pair。
public class Pair<X, Y>
{
public final X x;
public final Y y;
public Pair(X x, Y y) {
this.x = x;
this.y = y;
}
}
If we want to instantiate the class then we are doing to do something like this:如果我们想实例化这个类,那么我们正在做这样的事情:
Pair <String, int> pair = new Pair<>("one", 1);
Now because i am using String and int instead of X and Y can i say this is polymorphism?现在因为我使用 String 和 int 而不是 X 和 Y 我可以说这是多态吗? There is also this concept of super classes and polymorphism.还有超类和多态的概念。 What about that?那个怎么样?
What you see here are generics .你在这里看到的是泛型。 It has nothing to do with polymorphism.它与多态性无关。
Generics are something like an argument is for a method:泛型就像是一个方法的参数:
public static void foo(int bar) { ... }
The method foo
wants a user to give it some int
value when it is called.方法foo
希望用户在调用时为其提供一些int
值。 The method itself refers to that value by the local variable bar
.该方法本身通过局部变量bar
引用该值。 A call might look like一个电话可能看起来像
foo(5);
foo(8);
If you have a generic class like如果你有一个通用类,比如
public class Pair<A, B> { ... }
the class Pair
wants the user to declare two types when using it.类Pair
希望用户在使用时声明两种类型。 The class refers to them by the placeholders A
and B
.该类通过占位符A
和B
引用它们。 So one might use it like:所以人们可能会像这样使用它:
Pair<String, Integer> stringAndInteger;
Pair<Dog, Car> dogAndCar;
The nice thing about it is that the compiler can use this information to ensure that dogAndCar
can really only be used with Dog
and Car
for A
and B
.它的dogAndCar
是编译器可以使用这些信息来确保dogAndCar
真的只能与Dog
和Car
用于A
和B
。 So if the class has a method like所以如果这个类有一个像
public void setFirst(A first) { ... }
you can not call the method with the wrong type, like dogAndCar.setFirst(10);
你不能用错误的类型调用方法,比如dogAndCar.setFirst(10);
. . The compiler knows A
is Dog
for dogAndCar
and will not allow that you use it with anything that is not Dog
.编译器知道A
是dogAndCar
Dog
并且不允许您将它与任何不是Dog
东西一起使用。
For more on the topic, read the official tutorial for generics.有关该主题的更多信息,请阅读泛型的官方教程。
Polymorphism refers to the concept of one class implementing features of a parent class but overriding some other or adding new functionality.多态是指一个类实现父类的特性但覆盖其他一些或添加新功能的概念。
Let's consider the following parent class让我们考虑以下父类
public class Animal {
public void makeNoise() {
System.out.println("Hello");
}
}
Now we extend that class and override the method.现在我们扩展该类并覆盖该方法。 Additionally we add a second method:此外,我们添加了第二种方法:
public class Dog extends Animal {
@Override
public void makeNoise() {
System.out.println("wuff wuff");
}
public String getName() {
return "John";
}
}
If we use the makeNoise
method on a Dog
, we will see "wuff wuff"
and not "Hello"
.如果我们在Dog
上使用makeNoise
方法,我们将看到"wuff wuff"
而不是"Hello"
。
For more on this take a look at the official tutorial on polymorphism.有关更多信息,请查看有关多态性的官方教程。
Note that you can further distinguish this into inheritance and polymorphism .请注意,您可以进一步将其区分为继承和多态。 Read What is the main difference between Inheritance and Polymorphism?阅读继承和多态的主要区别是什么? for more.更多。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.