参考链接: 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多态性

[转载] c++多态与java多态性_Java中的多态性相关推荐

  1. java中的多态性_[转载] c++多态与java多态性_Java中的多态性

    参考链接: Java中的加法和串联 c++多态与java多态性 Polymorphism is one of the core concepts of OOPS paradigm. The meani ...

  2. c++多态与java多态性_Java中的多态性

    c++多态与java多态性 Polymorphism is one of the core concepts of OOPS paradigm. The meaning of polymorphism ...

  3. java的多态性_java中多态性什么意思?

    展开全部 在面向对象的程序JAVA中,多态性的定义是: 同一操作作用于不同的32313133353236313431303231363533e78988e69d8331333366306461类的实例 ...

  4. java 事务_Java中事务总结详解(精华)

    1.什么是JAVA事务? 通常的观念认为,事务仅与数据库相关. 事务必须服从ISO/IEC所制定的ACID原则.ACID是原子性(atomicity).一致性(consistency).隔离性 (is ...

  5. java 且_JAVA中逻辑运算符“|”和“”与“||”和“”的用法

    1.使用规则: (1)& 可以用作逻辑与的运算符,表示逻辑与(and) a.当运算符两边的表达式的结果都为true时,整个运算结果才为true,否则,只要有一方为false,则结果为false ...

  6. java 包装类_Java中的包装类

    Java中哪些需要包装类 它们将原始数据类型转换为对象.如果我们希望修改传递给方法的参数,则需要对象(因为基元类型是按值传递的). java.util包中的类只处理对象,因此在这种情况下包装类也有帮助 ...

  7. java 难度_java中难度大一点的面试题

    1.请大概描述一下Vector和ArrayList的区别,Hashtable和HashMap的区别.(5) (1)Vector和ArrayList的异同 实现原理,功能相同,可以互用 主要区别: Ve ...

  8. math java 计算_Java中的数学计算函数汇总

    Math类:  java.lang.Math类中包含基本的数字操作,如指数.对数.平方根和三角函数. java.math是一个包,提供用于执行任意精度整数(BigInteger)算法和任意精度小数(B ...

  9. java引_JAVA中的引用

    JDK1.2之后,Java扩充了引用的概念,将引用分为强引用.软引用.弱引用和虚引用四种. 强引用 类似于"Object a = new Object()"这类的引用,只要垃圾强引 ...

最新文章

  1. 一位老工程师前辈的忠告
  2. 查看ngnix使用的php.ini位置_修改Nginx php.ini文件的经典教程
  3. DBus glib 各数据类型接收与发送详解—C语言(3)
  4. git 创建本地仓库、远程仓库,上传项目
  5. SAP Cloud for Customer动态控制任意UI元素的显示或隐藏
  6. Git本地仓库文件的创建、修改和删除
  7. CentOS64位下python2.6升级到2.7的详细教程
  8. 恭喜您被选为CSDN插件内测用户:点此领取福利
  9. 剑指offer--面试题10
  10. Maven详解及相关操作
  11. 计算机专业大学排名_2020全国计算机专业大学排名
  12. 中美线径对照表_AWG 与国际线径对照表
  13. word删除空白页的方法和技巧详细介绍,提高工作效率!!!!!!!!!!!!
  14. 山海经电子书古文/翻译白话文版发布,免费电子书。
  15. Hold住通话有三种方式
  16. Shell入门教程[2]
  17. Hopscotch(POJ-3050)
  18. Zabbix 邮件报警、钉钉报警、微信报警
  19. SpringCloud与微服务Ⅴ --- Eureka服务注册与发现
  20. 数据结构(C语言版 第2版)课后习题答案 严蔚敏 等 编著

热门文章

  1. 12 [虚拟化] 进程抽象;fork,execve,exit
  2. Yapi Mock 远程代码执行漏洞
  3. C语言—sort函数比较大小的快捷使用--algorithm头文件下
  4. linux中权限765啥意思,Linux中的文件权限
  5. 手机apk签名工具安卓版_小飞鱼APK签名工具使用方法
  6. 计算机2级u盘作弊,一种带LED指示灯的计算机考试防U盘作弊装置制造方法及图纸...
  7. java char i=2+#039;2#039;;_P039 二维数组的字符按列存放到字符串中 ★★
  8. java 签名 ecdsa_Java实现ECDSA签名算法
  9. 【SpringBoot 2】(一)基础知识了解学习
  10. Angular实现图片点击缩放组件