[转载] 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.mzph.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…