问题描述
在我问我的问题之前,让我先解释一下我的理解和看法.
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.
根据Javadoc,只有多态 .没有编译时或运行时多态性.
According to the javadoc, there is only polymorphism. There is no compile time or run time polymorphism.
根据javadoc中名为定义方法 ,它说明了方法的重载.但是,关于编译时多态性并没有任何意义.
According to javadoc in a section called Defining Methods, it explains Overloading of Methods. But there is nothing about compile time polymorphism.
据我所说:
如果将多态性放入运行时多态性"类别中,则在更改JDK版本时只能看到编译时多态性":
If you put polymorphism in to category of Run time polymorphism, you can only see "compile time polymorphism" when you change your JDK version:
如果从较高的JDK版本切换到较低的版本,您将开始看到编译错误.相同的代码在编译期间的行为会有所不同,例如:lambda表达式,菱形运算符,开关大小写的字符串,泛型等.
让我阐述一下我的看法,关于运行时多态性和编译时多态性的预测是如何出现在博客/教程中的:
Let me elaborate my opinion, my prediction about how run time polymorphism and compile time polymorphism came to blogs/tutorials:
开发人员2:抱歉,我没有得到你.
Developer 2: Sorry, I didn't get you.
开发人员1:在运行时,您将传递的对象实例很简单,该实例方法将被执行.
Developer 1: It's simple at run time which object instance you will pass, that instance method will be executed.
开发人员2:哦! 运行时间.我知道了.
开发人员1:是相同的代码,但是在运行时传递的实例不同.
Developer 1: Yes same piece of code, but at Run Time passed instances are different.
开发人员2:**运行时!好的,我明白了.
对话2
开发人员3:通过覆盖方法运行时多态吗?那么什么是oveloading?编译时多态性?
Developer 3: Run time polymorphism by overriding methods? Then what is oveloading? Compile time polymorphism?
开发人员2:您怎么说重载是编译时的多态性?
Developer 2: How do you say overloading as compile time polymorphism?
开发人员3:,因为它仅在编译时决定.
Developer 3: Because it is decided at compile time only.
开发者2:安静!
多态和编码到接口的最佳示例是java.sql:
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);
根据注册的驱动程序,同一段代码的行为有所不同.这意味着,如果我们注册Mysql驱动程序,由于多态性,该代码将执行mysql实例的方法. IE.它执行重写的方法.如果注册了Oracle驱动程序,则该驱动程序适用于Oracle,依此类推.
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.
现在任何人都可以向我展示相同的代码,它们在编译时的行为有所不同.或者换句话说,告诉我add(3,4)
方法在编译期间是否绑定到不同的方法(其他签名的方法)?
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?
将根据匹配的签名执行该方法.方法具有相同的名称并不意味着存在多态性,因为调用方法的签名是不同的:
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:
问题1:如果您不更改调用方法签名,它将调用除签名已匹配的方法以外的其他方法吗?在任何情况下,它的行为都会有所不同吗?
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);
}
问题1:如果方法重载是多态的,那么上面的代码块中哪段代码表现不同?多态性在哪里?
Question 1: If method overloading is polymorphism, Which piece of code behaving differently in above block of code? Where is polymorphism here?
问题2:在什么情况下方法调用add(3,4);
会显示多态性,除非将其修改为add(3,4,5)
?
Question 2: Method invocation add(3,4);
on what scenario it shows ploymorphism, unless it is modified to add(3,4,5)
?
编辑
@FutureVisitor ,因为此线程未找到支持方法重载作为多态性类型的答案(即使在问了一个月的问题之后),也没有任何理由接受支持方法重载的答案不是多态性,如果在我关于方法重载的论点中有任何回答要点问题不是多态性,将被接受并支持他们的观点.
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.
推荐答案
在Java世界中,多态性意味着类之间的多态性. IE.用其共同的父类引用多个子类.在Java中,方法之间没有多态性.
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)
和void add(int a, int b, int c)
在Java语法中是完全不同的方法.事实并非如此-例如,在C ++中,您可以相互投射到彼此-但是在Java中则是如此.
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.
此处要理解的关键概念是方法签名.方法签名用一种语言定义了标识单个方法的内容. (例如,在void add(int a, int b);
旁边,您根本无法声明int add(int a, int b);
方法-返回值不是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.)
这篇关于阐述:方法重载是静态/编译时绑定,但不是多态.将静态绑定与多态相关联是否正确?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!