[转载] 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多态性

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值