最近在准备复习java,拿牛客网 Java 题目来练习,发现自己基础真的很薄弱。所以想通过share来巩固自己的基础知识。
https://www.nowcoder.com/profile/135782706/test/21149629/1092
关于重载的说法正确的是
https://docs.oracle.com/javase/tutorial/java/javaOO/methods.html
The Java programming language supports overloading methods, and Java can distinguish between methods with different method signatures. This means that methods within a class can have the same name if they have different parameter lists (there are some qualifications to this that will be discussed in the lesson titled "Interfaces and Inheritance").
方法可以拥有相同的名字,如果它们之间拥有不同的参数(形参:parameters)列表。
public class DataArtist { ... public void draw(String s) { ... } public void draw(int i) { ... } public void draw(double f) { ... } public void draw(int i, double f) { ... } }
You cannot declare more than one method with the same name and the same number and type of arguments, because the compiler cannot tell them apart.
The compiler does not consider return type when differentiating methods, so you cannot declare two methods with the same signature even if they have a different return type.
你不能声明两个相同名字和相同数目和类型的参数,因为编辑器无法区分它们。
编辑器并不考虑区分两个拥有相同方法签名并拥有不同返回值的方法。
重载函数并不要求不同的返回类型(1.),和形参个数(2.),但要求函数名要相同(4.)。
什么是override(覆盖)
https://www.geeksforgeeks.org/overriding-in-java/
When a method in a subclass has the same name, same parameters or signature and same return type(or sub-type) as a method in its super-class, then the method in the subclass is said to override the method in the super-class.
当一个子类有名字相同,相同的参数/方法签名,相同的返回值/sub-type在它的super-class中作为一个方法,然后可以说在这个super-calss中的方法重载了super-class的方法。
什么是方法签名
https://blog.csdn.net/qq_27093465/article/details/54907833
方法签名由方法名称和一个参数列表(方法的参数的顺序和类型)组成。
https://www.thoughtco.com/method-signature-2034235
It's the combination of the method name and the parameter list.
public double calculateAnswer(double wingSpan, int numberOfEngines, double length, double grossTons) { //method code }
you can see that the method signature here is calculateAnswer(double, int, double, double).
这里的方法签名就是 calculateAnswer(double, int, double, double)。
什么是形参,什么是实参
https://stackoverflow.com/questions/12709026/difference-between-arguments-and-parameters-in-java
package com.stackoverflow.works; public class ArithmeticOperations { public static int add(int x, int y) { //x, y are parameters(形参) here return x + y; } public static void main(String[] args) { int x = 10; int y = 20; int sum = add(x, y); //x, y are arguments(实参) here System.out.println("SUM IS: " +sum); } }
http://www.cs.toronto.edu/~reid/web/javaparams.html
形参: format parameters
实参:actual arguments
// Method definition public int mult(int x, int y) { return x * y; } // Where the method mult is used int length = 10; int width = 5; int area = mult(length, width);
x and y are the formal parameters.
length and width are actual parameters.
public void method(int i,double j) { } public void method(double j,int i) { }这种也是重载,属于参数列表不同的一种