參考鏈接: Java中的加法和串聯
c++多態與java多態性
?
??
??
?
?Polymorphism is one of the core concepts of OOPS paradigm. The meaning of polymorphism is the condition of occurring in several different forms.
? 多態是OOPS范式的核心概念之一。 多態性的含義是以幾種不同形式出現的條件。??
? 1.什么是多態性? (1. What is Polymorphism?)?
?Polymorphism is the ability to perform different things in different scenarios. Sometimes, the polymorphism is based on the input parameters to the function. The polymorphism can be present in case of inheritance also. The functions behave differently based on the actual implementation.
? 多態是在不同場景下執行不同任務的能力。 有時,多態性基于函數的輸入參數。 在繼承的情況下也可以存在多態性。 函數的功能根據實際實現而有所不同。??
?
??
? 2.多態現實生活中的例子 (2. Polymorphism Real Life Example)?
?One of the functions of a Person is to write. If the person is presented with a pencil and paper, then he will write some words on it. If the person is presented with oil paint and a coloring book, he will make a painting. Here the function is behaving differently based on the input arguments. 一個人的功能之一就是寫作。 如果給該人鉛筆和紙,那么他會在上面寫一些字。 如果向該人展示油畫顏料和圖畫書,他將作畫。 在此,函數根據輸入參數的行為有所不同。 A delivery person deliver items to the given address. If it’s a Postman, he will deliver letters and parcels to the home. If it’s a Pizza delivery person, he will deliver Pizza to you. Here the polymorphism in the delivery function is achieved through the different implementation. 送貨人員將物品運送到給定的地址。 如果是郵遞員,他將把信件和包裹寄到家里。 如果是披薩送貨員,他會把披薩送貨給您。 在這里,交付功能的多態性是通過不同的實現方式實現的。?
? 3. Java中的多態 (3. Polymorphism in Java)?
?Java supports polymorphism and it can be divided into two types.
? Java支持多態,它可以分為兩種類型。??
?Compile Time Polymorphism 編譯時多態 Runtime Polymorphism 運行時多態?
? 4.編譯時多態 (4. Compile Time Polymorphism)?
?When the compiler is able to determine the actual function, it’s called compile-time polymorphism. There are two types of compile-time polymorphism.
? 當編譯器能夠確定實際功能時,稱為編譯時多態。 有兩種類型的編譯時多態性。??
?Method Overloading 方法重載 Operator Overloading 運算符重載?
? 4.1)方法重載 (4.1) Method Overloading)?
?When different functions in a class have the same name but different signature, it’s called method overloading. A method signature contains the name and method arguments. So, overloaded methods have different arguments. The arguments might differ in the numbers or the type of arguments. The method return type is not part of the signature.
? 當類中的不同函數具有相同的名稱但簽名不同時,則稱為方法重載 。 方法簽名包含名稱和方法參數。 因此,重載方法具有不同的參數。 參數的數量或類型可能有所不同。 方法的返回類型不是簽名的一部分。??
?Here is a simple example of method overloading in Java.
? 這是Java中方法重載的簡單示例。??
?
??
??
?
?import java.io.File;
?
class Printer{
? ??
? ? void print(String message) {
? ? ? ? // printing string message
? ? }
? ??
? ? void print(String message, int count) {
? ? ? ? // printing message count times
? ? }
? ??
? ? boolean print(Object obj, File f) {
? ? ? ? //printing object to file
? ? ? ? return true;
? ? }
}
?If you are looking for method overloading in JDK classes, you will find a lot of them in the String and primitive wrapper classes. For example, String valueOf() methods, Integer parseInt() methods, etc.
? 如果要在JDK類中查找方法重載,則可以在String和原始包裝器類中找到很多方法重載。 例如,String valueOf()方法,Integer parseInt()方法等。??
?
??
? Method Overloading in Java String Class
? ?Java字符串類中的方法重載??
??
? ?
? ?
??
?
? 4.2)操作員重載 (4.2) Operator Overloading)?
?Java doesn’t allow us to override operators. However, the addition (+) operator is overloaded for the String objects. When the addition operator is used with string objects, it concatenates them and returns a new string object.
? Java不允許我們覆蓋運算符。 但是,對于String對象,加法(+)運算符已重載。 當加法運算符與字符串對象一起使用時,它將它們串聯起來并返回一個新的字符串對象。??
?jshell> 10 + 5
$1 ==> 15
?
jshell> 10.5 + 20.1
$2 ==> 30.6
?
jshell> "ABC" + "abc"
$3 ==> "ABCabc"
?
jshell> new Object() + new Object()
|? Error:
|? bad operand types for binary operator '+'
|? ? first type:? java.lang.Object
|? ? second type: java.lang.Object
|? new Object() + new Object()
|? ^-------------------------^
?
jshell>
?
? String Concatenation in Java
? Java中的字符串連接?
?
? 5.運行時多態 (5. Runtime Polymorphism)?
?The runtime polymorphism is achieved by method overriding. When the superclass method is overridden in the subclass, it’s called method overriding.
? 通過方法重寫實現運行時多態。 當超類方法在子類中被覆蓋時,稱為方法覆蓋。??
?In this case, the compiler is not able to determine whether the superclass or subclass method will get called. The method resolution happens at runtime based on the actual type of the object. That’s why it’s called runtime polymorphism. It’s also called Dynamic Method Dispatch.
? 在這種情況下,編譯器無法確定將調用超類還是子類方法。 方法解析會在運行時根據對象的實際類型發生。 這就是為什么將其稱為運行時多態性。 也稱為動態方法調度 。??
?Here is an example of runtime polymorphism in Java.
? 這是Java中運行時多態的示例。??
?package com.journaldev.examples;
?
import java.util.Random;
?
public class DeliveryBoy {
?
? ? void deliver() {
? ? ? ? System.out.println("Delivering Item");
? ? }
?
? ? public static void main(String[] args) {
? ? ? ? DeliveryBoy db = getDeliveryBoy();
? ? ? ? db.deliver();
? ? }
?
? ? private static DeliveryBoy getDeliveryBoy() {
? ? ? ? Random rand = new Random();
? ? ? ? int num = rand.nextInt(10);
? ? ? ? return num % 2 == 0 ? new PizzaDeliveryBoy() : new Postman();
? ? }
}
?
class PizzaDeliveryBoy extends DeliveryBoy {
?
? ? @Override
? ? void deliver() {
? ? ? ? System.out.println("Delivering Pizza");
? ? }
}
?
class Postman extends DeliveryBoy {
? ? @Override
? ? void deliver() {
? ? ? ? System.out.println("Delivering Letters");
? ? }
}
?The deliver() method is overridden in the PizzaDeliveryBoy and Postman classes. The compiler can’t determine whether the getDeliveryBoy() will return a PizzaDeliveryBoy or Postman. So, the method resolution happens at runtime.
? 在PizzaDeliveryBoy和Postman類中,delivery deliver()方法被覆蓋。 編譯器無法確定getDeliveryBoy()將返回PizzaDeliveryBoy還是Postman。 因此,方法解析在運行時發生。??
?If you run the program, the output will vary between “Delivering Letters” and “Delivering Pizza”.
? 如果運行該程序,輸出將在“ Delivering Letters”和“ Delivering Pizza”之間變化。??
? 六,結論 (6. Conclusion)?
?The philosophy of Java has always been to take the best features of object-oriented programming. That’s why operator overloading feature was dropped in Java because it creates more confusion.
? Java的哲學一直是采取面向對象編程的最佳功能。 這就是Java中刪除運算符重載功能的原因,因為它會造成更多混亂。??
? 7.參考 (7. References)?
?Oracle Docs Oracle文檔 JavaWorld Article JavaWorld文章?
?
? 翻譯自: https://www.journaldev.com/33246/polymorphism-in-java
?
?c++多態與java多態性