详细说明:方法重载是静态/编译时绑定,但不是多态性.将静态绑定与多态性相关联是否正确?

Elaboration: Method overloading is a static/compile-time binding but not polymorphism. Is it correct to correlate static binding with polymorphism?(详细说明:方法重载是静态/编译时绑定,但不是多态性.将静态绑定与多态性相关联是否正确?)

本文介绍了详细说明:方法重载是静态/编译时绑定,但不是多态性.将静态绑定与多态性相关联是否正确?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我提出问题之前,让我解释一下我的理解和看法.

  • 除非有向上转换,否则仅通过覆盖我们无法实现多态性.由于它只能在运行时看到,人们可能将其命名为运行时多态.(我不反对将多态性称为运行时多态性)
  • 我反对方法重载称为编译时多态多态性.

我同意方法重载是静态绑定(编译时绑定),但我看不到多态性.

根据javadoc,只有多态.没有编译时或运行时多态性.

根据 javadoc 在名为 定义方法的部分,它解释了方法的重载.但是编译时多态并没有什么.

据我说:

如果将多态性归入运行时多态性的范畴,那么当你改变你的JDK版本时,你只能看到编译时多态性":

如果您从较高的 JDK 版本切换到较低的版本,您将开始看到编译错误.相同的代码在编译时表现不同,例如:lambda 表达式、菱形运算符、switch case 中的字符串、泛型等.

让我阐述一下我的观点,我对运行时多态性和编译时多态性如何出现在博客/教程中的预测:

对话1

<块引用>

开发人员 1:嘿,我今天读到了多态性.如果对接口进行编码,则可以实现多态性.编写代码不与类紧密耦合,而是通过松耦合将其写入接口,调用超类方法或接口方法实际上调用子类的方法,具体取决于传递的实例.

开发者 2:对不起,我没听明白.

开发者 1:在运行时很简单,您将传递哪个对象实例,然后执行该实例方法.

开发者 2: 哦!运行时间.我明白了.

开发人员 1: 是相同的代码,但在运行时传递的实例不同.

开发者 2:**运行时间!好的,我知道了.

对话2

<块引用>

开发者 2:嘿,昨天我遇到了开发者 1,他正在讲述一些运行时多态性.通过覆盖子类中的方法,我们可以实现它.

开发者 3: 通过重写方法实现运行时多态性?那么什么是超载呢?编译时多态?

开发者 2:您如何将重载称为编译时多态性?

开发者 3: 因为它只在编译时决定.

开发者 2: 静默!

多态接口编码最好的例子是java.sql:

java.sql.Connection conn = DriverManager.getConnection(DB_URL,USER,PASS);java.sql.Statement stmt = conn.createStatement();java.sql.ResultSet rs = stmt.executeQuery(sql);

根据注册的驱动程序,同一段代码的行为会有所不同.这意味着,如果我们注册 Mysql 驱动程序,由于多态性,这段代码会执行 mysql 实例的方法.IE.它执行被覆盖的方法.如果您注册了Oracle驱动程序,它适用于Oracle,等等.

在上面我们发现相同的代码表现不同.

现在任何人都可以向我展示在编译时表现不同的相同代码.或者换句话说,告诉我在编译期间绑定到不同方法(其他签名方法)的 add(3,4) 方法?

根据javadoc,

<块引用>

Java 编程语言支持重载方法,Java 可以区分具有不同方法签名的方法.

该方法将根据匹配的签名执行.方法同名不代表多态,因为调用方法的签名不同:

问题1:如果您不更改调用方法签名,它会调用与签名匹配的方法不同的方法吗?在任何情况下它的行为都会有所不同吗?

让我们看看方法重载:

public void add(int a, int b){System.out.println(a+b);}公共无效添加(int a,int b,int c){System.out.println(a+b+c);}公共静态无效主要(字符串 [] 参数){添加(3,4);添加(3,4,5);}

问题 1: 如果方法重载是多态,那么在上面的代码块中哪一段代码的行为不同?这里的多态性在哪里?

问题2:方法调用add(3,4);在什么场景下显示多态,除非修改为add(3,4,5)?

<小时>编辑
@FutureVisitor 因为这个线程没有找到支持将方法重载作为一种多态性的答案(即使在提出一个月的问题之后),没有任何理由接受支持方法重载的答案是不是多态性,如果我的方法重载论点中的任何答案点问题不是多态性,将被接受并支持他们的观点.

解决方案

在Java世界中,多态是指类之间的多态.IE.可能与他们的共同父类一起引用多个子类.在 Java 中,方法之间没有多态性.

void add(int a, int b)void add(int a, int b, int c) 在 Java 语法中是完全不同的方法.不应该是这样 - 例如,在 C++ 中,您可以将它们cast 相互转换 - 但在 Java 中却是这样.

这里要理解的关键概念是方法签名.方法签名在一种语言中定义了标识单个方法的内容.(例如,除了 void add(int a, int b);,您根本不能声明 int add(int a, int b); 方法 -返回值不是 Java 中方法签名的一部分,因此编译器会将其解释为方法重新定义.)

Before I ask my question, let me to explain my understanding and opinion.

  • By only Overriding we can't achieve polymorphism unless there is upcasting. And as it can only seen at runtime people might have named it as Run time ploymorphism. (I have no objection to call polymorphism as Run Time Polymorphism)
  • I have objection to call method overloading as compile time polymorphism or polymorphism.

I agree that method overloading is static binding (compile time binding), but I can't see polymorphism in that.

According to the javadoc, there is only polymorphism. There is no compile time or run time polymorphism.

According to javadoc in a section called Defining Methods, it explains Overloading of Methods. But there is nothing about compile time polymorphism.

According to me:

If you put polymorphism in to category of Run time polymorphism, you can only see "compile time polymorphism" when you change your JDK version:

If you switch from a higher JDK version higher to a lower one, you will start seeing compilation errors. The same code behaving differently during compilation time, eg: lambda expression, diamond operator, string in switch case, generics etc.

Let me elaborate my opinion, my prediction about how run time polymorphism and compile time polymorphism came to blogs/tutorials:

Conversation1

Developer 1: Hey I read about polymorphism today. If you do coding to interface, you can achieve polymorphism. writing codes not tightly coupling to a class but instead writing it to interface by making loose coupling, calling a superclass method or interface method actually invokes method of subclass depending on instance passed.

Developer 2: Sorry, I didn't get you.

Developer 1: It's simple at run time which object instance you will pass, that instance method will be executed.

Developer 2: Ohh! Run Time. I got it.

Developer 1: Yes same piece of code, but at Run Time passed instances are different.

Developer 2: **Run Time! Okay, I got it.

Conversation2

Developer 2: Hey yesterday I've met Developer1, he was telling about some run-time polymorphism. By overriding methods in a sub-class we can achieve it.

Developer 3: Run time polymorphism by overriding methods? Then what is oveloading? Compile time polymorphism?

Developer 2: How do you say overloading as compile time polymorphism?

Developer 3: Because it is decided at compile time only.

Developer 2: Silent!

Polymorphism and coding to interface best example is java.sql:

java.sql.Connection conn = DriverManager.getConnection(DB_URL,USER,PASS);
java.sql.Statement stmt = conn.createStatement();
java.sql.ResultSet rs = stmt.executeQuery(sql);

The same piece of code behaves differently based on the Driver registered. This means, if we register the Mysql driver, due to polymorphism this code executes methods of mysql instances. I.e. it executes overridden methods. If you register the Oracle driver, it works for Oracle, and so on.

Above we found same code behaving differently.

Now can anyone show me same code behaving differently in compile time. Or in other words show me add(3,4) method bounded to different methods (other signatured methods) during compile time?

According to javadoc,

The Java programming language supports overloading methods, and Java can distinguish between methods with different method signatures.

The method will be executed depending on signature matched. Having same name for methods does not mean there is a polymorphism, because calling methods signature is different:

Queestion1: If you won't change calling method signature will it call different method other than method for which signature has matched? In any condition will it behave differently?

Let's look at Method overloading:

public void add(int a, int b)
{
    System.out.println(a+b);
}

public void add(int a, int b, int c)
{
    System.out.println(a+b+c);
}

public static void main(String[] args)
{
    add(3,4);
    add(3,4,5);
}

Question 1: If method overloading is polymorphism, Which piece of code behaving differently in above block of code? Where is polymorphism here?

Question 2: Method invocation add(3,4); on what scenario it shows ploymorphism, unless it is modified to add(3,4,5)?


EDIT
@FutureVisitor since this thread found no answers in favor of method overloading as a type of polymorphism(Even after a month of question being asked), without any justification accepting answer in favor of Method overloading is not a polymorphism, if any answer points problem in my argument of method overloading is not polymorphism will be accepted and supported their views.

解决方案

In the Java world, polymorphism means polymorphism between classes. I.e. refering possibly multiple child classes with their common parent. In Java, there is no polymorphism between methods.

void add(int a, int b) and void add(int a, int b, int c) are entirely different methods in the Java syntax. It shouldn't be so - for example, in C++, you can cast them to each other -, but it is so in Java.

The key concept to understand here is the method signature. The method signature defines in a language, what identifies the induvidual methods. (For example, beside a void add(int a, int b);, you simply can't declare an int add(int a, int b); method - the return value is not a part of the method signature in Java, thus the compiler would interpret it as a method re-definition.)

这篇关于详细说明:方法重载是静态/编译时绑定,但不是多态性.将静态绑定与多态性相关联是否正确?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:详细说明:方法重载是静态/编译时绑定,但不是多态性.将静态绑定与多态性相关联是否正确?

基础教程推荐