欢迎来到新的Java Challengers博客!本博客致力于挑战Java编程中的概念。掌握它们,你将成为一名技术娴熟的Java程序员。
术语:方法重载
什么是方法重载?
清单1.三种类型的方法重载
Number of parameters:
public class Calculator {
void calculate(int number1, int number2) { }
void calculate(int number1, int number2, int number3) { }}
Type of parameters:
public class Calculator {
void calculate(int number1, int number2) { }
void calculate(double number1, double number2) { }
}
Order of parameters:
public class Calculator {
void calculate(double number1, int number2) { }
void calculate(int number1, double number2) { }
}复制代码
方法重载和原始类型
表1. Java中的原始类型
Type | Range | Default | Size | Example literals |
boolean | true or false | false | 1 bit | true, false |
byte | -128 .. 127 | 0 | 8 bits | 1, -90, 128 |
char | Unicode character or 0 to 65,536 | \u0000 | 16 bits | 'a', '\u0031', '\201', '\n', 4 |
short | -32,768 .. 32,767 | 0 | 16 bits | 1, 3, 720, 22,000 |
int | -2,147,483,648 .. 2,147,483,647 | 0 | 32 bits | -2, -1, 0, 1, 9 |
long | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 | 0 | 64 bits | -4000L, -900L, 10L, 700L |
float | 3.40282347 x 1038, 1.40239846 x 10-45 | 0.0 | 32 bits | 1.67e200f, -1.57e-207f, .9f, 10.4F |
double | 1.7976931348623157 x 10308, 4.9406564584124654 x 10-324 | 0.0 | 64 bits | 1.e700d, -123457e, 37e1d |
为什么我们要使用方法重载?
什么不是重载?
public class Calculator {
void calculate(int firstNumber, int secondNumber){}
void calculate(int secondNumber, int thirdNumber){}
} 复制代码
你也不能通过更改方法签名中的返回类型来重载方法。以下代码不会编译:
public class Calculator {
double calculate(int number1, int number2){return 0.0;}
long calculate(int number1, int number2){return 0;}
}复制代码
构造函数重载
public class Calculator {
private int number1;
private int number2;
public Calculator(int number1) {this.number1 = number1;}
public Calculator(int number1, int number2) {
this.number1 = number1;
this.number2 = number2;}
}复制代码
挑战方法重载!
清单2.高级方法重载挑战
public class AdvancedOverloadingChallenge3 {
static String x = "";
public static void main(String... doYourBest) {
executeAction(1);
executeAction(1.0);
executeAction(Double.valueOf("5"));
executeAction(1L);
System.out.println(x);
}
static void executeAction(int ... var) {x += "a"; }
static void executeAction(Integer var) {x += "b"; }
static void executeAction(Object var) {x += "c"; }
static void executeAction(short var) {x += "d"; }
static void executeAction(float var) {x += "e"; }
static void executeAction(double var) {x += "f"; }}复制代码
1.befe
2.bfce
3.efce
4.aecf
刚刚发生了什么?JVM如何编译重载方法
1.宽化类型转换
2.box(自动装箱和拆箱)
3.可变参数
int primitiveIntNumber = 5;
double primitiveDoubleNumber = primitiveIntNumber ;复制代码
这是宽化时基本类型的顺序:
int primitiveIntNumber = 7;
Integer wrapperIntegerNumber = primitiveIntNumber;复制代码
Integer wrapperIntegerNumber = Integer.valueOf(primitiveIntNumber);复制代码
这是一个拆箱的例子:
Integer wrapperIntegerNumber = 7;
int primitiveIntNumber= wrapperIntegerNumber;复制代码
int primitiveIntNumber = wrapperIntegerNumber.intValue();复制代码
这是一个可变参数的例子; 请注意,可变参数它始终是最后执行的:
execute(int… numbers){}复制代码
什么是可变参数?
execute(1,3,4,6,7,8,8,6,4,6,88...); // We could continue…复制代码
可变参数非常方便,因为值可以直接传递给方法。如果我们使用数组,我们必须使用值实例化数组。
宽化:一个实际的例子
重载常见错误
使用包装器进行自动装箱
Java是一种强类型编程语言,当我们使用包装器进行自动装箱时,我们必须记住一些事情。首先,以下代码将无法编译:
int primitiveIntNumber = 7;Double wrapperNumber = primitiveIntNumber;复制代码
自动装箱只适用于该double类型,因为编译此代码时发生的情况与以下内容相同:
Double number = Double.valueOf(primitiveIntNumber);复制代码
上面的代码将编译。第一种int类型将被扩展为double,然后它将被包装成Double。但是当自动装箱时,没有类型宽化,函数Double.valueOf将收到一个double,而不是一个int。在这种情况下,自动装箱仅在我们强制转换时才有效,如下所示:
Double wrapperNumber = (double) primitiveIntNumber;复制代码
请记住,Integer不能转换为Long和Float不能转换为Double。没有继承关系。这些每个类型--Integer,Long,Float,和Double-- 是一个Number和Object。
JVM中的硬编码数字类型
class Calculator {
public static void main(String… args) {
// This method invocation will not compile
// Yes, 1 could be char, short, byte but the JVM creates it as an int
calculate(1);
}
void calculate(short number) {}
}复制代码
class Calculator {
public static void main(String… args) {
// This method invocation will not compile
// Yes, 1 could be float but the JVM creates it as double
calculate(1.0);
}
void calculate(float number) {}
}复制代码
char anyChar = 127; // Yes, this is strange but it compiles复制代码
关于重载要记住什么
首先是宽化
第二是装箱
第三是可变参数
答案
有关Java中方法重载的更多信息
· Java 101: Classes and objects in Java: A true beginner’s introduction to classes and objects, including short sections on methods and method overloading.
· Java 101: Elementary Java language features: Learn more about why it matters that Java is a strongly typed language and get a full introduction to primitive types in Java.
· Too many parameters in Java methods, Part 4: Explore the limitations and disadvantages of method overloading, and how they may be remedied by integrating custom types and parameter objects.
(未经同意,请勿转载)