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

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

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

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

  2. java中多态到底是什么_java中的多态是指什么

    java中的多态是指什么 发布时间:2020-06-28 09:59:19 来源:亿速云 阅读:119 作者:Leah java中的多态是指什么?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希 ...

  3. java对象工厂池_[转载]Java对象池技术的原理及其实现

    作者:汪永好 出处:计算机与信息技术 责任编辑: 方舟 [ 2006-11-25 07:00 ] 摘 要 :本文在分析对象池技术基本原理的基础上,给出了对象池技术的两种实现方式.还指出了使用对象池技术 ...

  4. java io流操作_十个Demo进行讲解Java中IO流的常用操作~

    好久不见的IO流 对IO流的学习,我记得还是初学Java基础的时候,后来找工作过程中经常看到有些招聘信息中写到熟悉IO流,现在想想IO流,真的是一脸懵逼,不说这么多废话了,IO流这次好好整理一下. 说 ...

  5. java hashmap is遍历_关于内存:在Java(或Scala)中遍历HashMap的HashMap

    我创建了一个类Foo,该类具有返回Array的方法toArray(). 现在,我有一个将字符串映射到HashMaps的HashMap,后者将对象映射到Foo.那是: HashMap> 我想创建一 ...

  6. java底层原理书籍_阿里面试题:Java中this和super关键字的底层实现原理

    知道的越多,不知道的就越多,业余的像一棵小草! 编辑:业余草 来源:https://www.xttblog.com/?p=5028 B 站:业余草 最近一个粉丝加我说,接到了阿里的面试,问问我阿里会面 ...

  7. jsp文件里java代码的作用_如何使用JSP 2避免JSP文件中的Java代码?

    小编典典 自从2001年标签库(例如JSTL)和EL(表达语言,那些东西)的诞生以来,在JSP中确实不建议使用scriptlet(那些东西).${} scriptlet的主要缺点是: 可重用性:您无法 ...

  8. java import自定义类_自定义类加载器-从.class和.jar中读取

    一. 类加载器 JVM中的类加载器:在jvm中,存在两种类加载器, a) Boostrap ClassLoader:这个是由c++实现的,所以在方法区并没有Class对象的实例存在.用于加载JAVA_ ...

  9. 如何在matlab里输入复杂公式_[转载]如何在Matlab绘制的图形中显示复杂公式

    Matlab文本的Interpreter属性使我们能在图形中显示一个较为复杂的公式,例如在公式中除了有希腊字母外,还有分号.根号等数学符号. 当键入:>> set(text,'Interp ...

最新文章

  1. 计算机的硬盘和光盘数,硬盘和光盘属于什么媒体
  2. 委外订单_听听晚报-英特尔扩大芯片委外订单、苹果秋季或举行两场发布会
  3. mysql数据库删除一条数据后还想让新增数据从空缺id处开始
  4. NumPy Beginner's Guide 2e 带注释源码 九、使用 Matplotlib 绘图
  5. 优先队列详解priority_queue .RP
  6. Android——适配器其他组件(AutoCompleteTextView:自动完成文本编辑框;Spinner:下拉列表)...
  7. 持久化存储技术之本地存储
  8. Two Bases CodeForces - 602A (BigInteger c++long long也可以)
  9. C++学习(二六七)find_package() find_library()
  10. sentry-前端应用监控工具
  11. 计算两个时间之间的进度百分比
  12. 电动门窗防夹发展新趋势-基于电机纹波的防夹
  13. win10 sublime text3配置c++
  14. 第12节 html创建热点区域 链接
  15. Unity 3D游戏九:粒子光环
  16. hyper扩展linux硬盘,Hyper-V 虚拟机扩展磁盘
  17. NLD4J - Arbiter
  18. 新型4-(3H)-喹唑啉酮类作为VEGFR-2抑制剂对肝癌细胞具有潜在活性
  19. 二级计算机哪种最简单,计算机二级考什么好 哪个最实用
  20. python+opencv多进程实现识别魔方颜色,通过kociemba算法得出算法字符串并画图(附毕设完整视频)

热门文章

  1. iOS 自定义UISlider
  2. 用LVM在VMware中的Linux硬盘扩容
  3. Python语言解析xml文件
  4. c语言 recv_sin,C++_C语言中经socket接收数据的相关函数详解,recv()函数: 头文件:#incl - phpStudy...
  5. ios跨线程通知_iOS多线程编程指南(三)Run Loop
  6. python函数的四个特点_Python面向对象三大特征之封
  7. androidx86 9.0下载_Surface pro 安装 android x86/chrome OS
  8. linux下搭建博客Day8
  9. python方法测试怀孕,Python unittest模拟:是否可以在测试时模拟方法的默认参数的值?...
  10. java 获取下拉框的值_java中怎么获取下拉框的值