[轉載] c++多態與java多態性_Java中的多態性

參考鏈接: 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多態性

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/540245.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/540245.shtml
英文地址,請注明出處:http://en.pswp.cn/news/540245.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

USB peripherals can turn against their users

Turning USB peripherals into BadUSB USB devices are connected to – and in many cases even built into – virtually all computers. The interface standard conquered the world over the past two decades thanks to its versatility: Almost any computer peripheral…

[轉載] JAVA條件表達式的陷阱

參考鏈接&#xff1a; Java條件表達式中的數字類型提升 Map<String, Integer> map new HashMap<String, Integer>(); map.put("count", null); Integer it map null ? 0 : map.get("count"); 注意&#xff1a;在第三行&#xff0c;會…

Linux系統管理初步(七)系統服務管理、chkconfig與systemd 編輯中

Linux系統本身包含了很多服務&#xff0c;CentOS6之前系統的服務用SysV控制&#xff0c;CentOS7改為systemd控制 一、chkconfig服務管理機制 簡而言之&#xff0c;chkconfig就是CentOS6以前用來控制系統服務的工具&#xff0c; 常用方法舉例 chkconfig --list #列出所有的系統服…

[轉載] 菜鳥舉例理解字節流和字符流區別

參考鏈接&#xff1a; Java中的字符流與字節流 Character Stream對比Byte Stream 菜鳥舉例理解字節流和字符流區別 按照uft8編碼方式存儲文檔 文檔存儲路徑在D盤下 /** * 按照utf8格式存儲文檔 */ public static void storeDataByUTF8(){ String path "D:" …

[轉載] Java9發布回顧Java 8的十大新特性

參考鏈接&#xff1a; Java中的DoubleStream mapToObj() java9已經在北京時間9月22日正式發布&#xff0c;開發者可以在oracle jdk官網上下載到最新的jdk9。 今天&#xff0c;我們先來一起復習一下2014年發布的Java 8的十大新特性。先來喝杯java~~~ 按照java升級的傳統&…

窗體間傳遞數據

前言 做項目的時候&#xff0c;winfrom因為沒有B/S的緩存機制&#xff0c;窗體間傳遞數據沒有B/S頁面傳遞數據那么方便&#xff0c;今天我們就說下winfrom中窗體傳值的幾種方式。 共有字段傳遞 共有字段傳遞實現起來很方便&#xff0c;就是在窗體類中加個共有字段屬性&#xff…

[轉載] c語言中檢查命令行參數_C中的命令行參數

參考鏈接&#xff1a; Java中的命令行參數 c語言中檢查命令行參數 Command line argument is a parameter supplied to the program when it is invoked. Command line argument is an important concept in C programming. It is mostly used when you need to control your …

MySQL關閉Enterprise Server源碼

今天從MySQL官方網站上獲知&#xff0c;MySQL宣布關閉Enterprise Server的源碼&#xff0c;對于廣大開源愛好者來說&#xff0c;這是一個沉重的打擊。雖然免費的用戶群體一般僅僅使用MySQL Community Server&#xff08;開源免費社區版&#xff09;&#xff0c;但關閉MySQL Ent…

[轉載] Java中Scanner用法總結

參考鏈接&#xff1a; Java之Scanner類 最近在做OJ類問題的時候&#xff0c;經常由于Scanner的使用造成一些細節問題導致程序不通過&#xff08;最慘的就是網易筆試&#xff0c;由于sc死循環了也沒發現&#xff0c;導致AC代碼也不能通過。。。&#xff09;&#xff0c;因此對S…

os和shutil模塊

import os //os模塊基本實現了linux系統中所有的命令 os.system(終端命令)&#xff1a;在終端執行命令 os.getcwd():獲取當前的工作路徑 os.chdir():修改工作路徑 os.chmod():修改權限 os.chown():修改屬主屬組 os.mkdir():創建單個目錄&#xff0c;當目錄存在時報異常&…

[轉載] JAVA語言程序設計(基礎篇)第十版課后題答案(第一章)

參考鏈接&#xff1a; Java中的Scanner和nextChar() JAVA語言程序設計&#xff08;基礎篇&#xff09;第十版課后題答案 第一章 第二題 /** Created by ysy on 2018/7/6. */ public class text2 { public static void main(String[] args){ for(int i 0; i < 5; i) Syste…

java.util.Date和java.sql.Date 一點區別

最近無意中發現&#xff0c;在oracle中同一樣的一個Date類型字段&#xff0c;存儲的日期格式有兩種不同的情況&#xff0c;第一種是2011-1-1 12:00:00&#xff0c;第二種是2011-1-1&#xff0c;仔細查找發現在向數據庫中寫數據的時候定義的變量的問題。 第一種是&#xff1a;ja…

[轉載] java中關于用\t格式輸出

參考鏈接&#xff1a; 用Java格式化輸出 看了好多人關于\t的用法&#xff0c;感覺找不到自己想要的答案&#xff0c;所以索性就自己輸出來看看&#xff0c;如圖&#xff1a;這樣可以一目了然的看出來&#xff0c;\t&#xff08;制表符&#xff09;的作用就是看前面輸出滿不滿8…

微信搶房軟件開發

2019獨角獸企業重金招聘Python工程師標準>>> 這兩年樓市真可謂是一個"火“字難以形容 經歷了長沙兩次開盤&#xff0c;都沒有搶到&#xff0c;目前還沒有買到房子&#xff0c;說說我的悲劇吧&#xff0c;讓大伙都開心開心 第一次搶房是今年4月份長沙萬科金域國…

[轉載] Java——數組習題

參考鏈接&#xff1a; Java從控制臺讀取輸入的方法 package chap02; import java.util.Scanner; /** * * author admin * date 2020-4-8 * description: * 題目內容&#xff1a; 編寫程序&#xff0c; 從控制臺讀取下面的信息&#xff0c; 每月按22天工作日計算&#xff0c;…

超全Linux備份工具集合,滿足你的所有需要!

經常備份計算機上的數據是個好的做法&#xff0c;它可以手動完成&#xff0c;也可以設置成自動執行。許多備份工具擁有不同的功能特性&#xff0c;讓用戶可以配置備份類型、備份時間、備份對象、將備份活動記入日志及執行更多操作。 1.Rsync這是一款在Linux用戶當中頗受歡迎的命…

[轉載] Java內存管理-你真的理解Java中的數據類型嗎(十)

參考鏈接&#xff1a; Java中的字符串類String 1 做一個積極的人 編碼、改bug、提升自己 我有一個樂園&#xff0c;面向編程&#xff0c;春暖花開&#xff01; 推薦閱讀 第一季 0、Java的線程安全、單例模式、JVM內存結構等知識梳理 1、Java內存管理-程序運行過程&#x…

Linux系統安全加固腳本

閑來無事&#xff0c;整理一個系統安全加固腳本&#xff0c;每個公司的要求不一樣&#xff0c;所以僅供參考&#xff1a; #!/bin/sh echo "00 */1 * * * /usr/sbin/ntpdate 192.168.1.1 >>/var/log/ntpdate.log" > mycrontab crontab mycrontab rm -rf my…

[轉載] 整理下java中stringBuilder和stringBuffer兩個類的區別

參考鏈接&#xff1a; Java中的StringBuffer類 StringBuilder和StringBuffer這兩個類在動態拼接字符串時常用&#xff0c;肯定比String的效率和開銷小&#xff0c;這是因為String的對象不會回收哦。 其實我一直用StringBuilder這個類&#xff0c;因為可以簡寫為sb的變量在程序…

11.13 模10計數器設計

.新建一個工程 Family&#xff1a;FLEX10K Available device&#xff1a;EPF10K20TC144-3 2.設置lpm_counter宏單元參數并連接引腳 連接引腳的時候要注意的是&#xff0c;向量線的連接。 3.時序仿真 檢查無誤后進行下一步 4.載入7448并進行引腳連接 5.分配管腳 再次編譯&#x…