Java 简明教程
Java - Static Binding
绑定是一种在方法调用和方法实际实现之间创建链接的机制。根据 polymorphism concept in Java , object 可以有许多不同的形式。对象形式可以在编译时和运行时进行解析。
Binding is a mechanism creating link between method call and method actual implementation. As per the polymorphism concept in Java, object can have many different forms. Object forms can be resolved at compile time and run time.
Java Static Binding
Static binding 指在编译时在方法调用和方法实现之间解析链接的过程。Static binding 也称为 compile-time binding 或 early binding。
Static binding refers to the process in which linking between method call and method implementation is resolved at compile time. Static binding is also known as compile-time binding or early binding.
Characteristics of Java Static Binding
-
Linking − Linking between method call and method implementation is resolved at compile time.
-
Resolve mechanism − Static binding uses type of the class and fields to resolve binding.
-
Example − Method overloading is the example of Static binding.
-
Type of Methods − private, final and static methods and variables uses static binding.
Example of Java Static Binding
在此示例中,我们创建了一个 Calculator 类,其中有两个以相同名称命名的静态方法,但参数不同,分别用于分别添加两个和三个 int 值。在 main() 方法中,我们调用这些方法并打印结果。基于传递的参数数量,编译器使用静态绑定来确定要调用的方法,并根据此信息相应地打印结果。
In this example, we’ve created a Calculator class having two static methods with same name but different arguments to add two and three int values respectively. In main() method, we’re calling these methods and printing the result. Based on the number of arguments passed, compiler decides the method using static binding which method is to be called and result is printed accordingly.
package com.tutorialspoint;
class Calculator{
public static int add(int a, int b){
return a + b;
}
public static int add(int a, int b, int c){
return a + b + c;
}
}
public class Tester {
public static void main(String args[]){
System.out.println(Calculator.add(20, 40));
System.out.println(Calculator.add(40, 50, 60));
}
}
60
150
Java Static Binding: More Examples
Example 1
在此示例中,我们创建了一个 Calculator 类,其中有两个以相同名称命名的非静态方法,但参数不同,分别用于分别添加两个和三个 int 值。在 main() 方法中,我们使用 Calculator 类的对象调用这些方法并打印结果。基于传递的参数数量,编译器使用静态绑定来确定要调用的方法,并根据此信息相应地打印结果。
In this example, we’ve created a Calculator class having two non-static methods with same name but different arguments to add two and three int values respectively. In main() method, we’re calling these methods using object of Calculator class and printing the result. Based on the number of arguments passed, compiler decides the method using static binding which method is to be called and result is printed accordingly.
package com.tutorialspoint;
class Calculator{
public int add(int a, int b){
return a + b;
}
public int add(int a, int b, int c){
return a + b + c;
}
}
public class Tester {
public static void main(String args[]){
Calculator calculator = new Calculator();
System.out.println(calculator.add(20, 40));
System.out.println(calculator.add(40, 50, 60));
}
}
60
150
Example 2
在此示例中,我们创建了一个 Calculator 类,其中有两个以相同名称命名的非静态方法,但参数类型不同,分别用于分别添加两个 int 值和两个 double 值。在 main() 方法中,我们使用 Calculator 类的对象调用这些方法并打印结果。基于传递的参数类型,编译器使用静态绑定来确定要调用的方法,并根据此信息相应地打印结果。
In this example, we’ve created a Calculator class having two non-static methods with same name but different types of arguments to add two int values and two double values respectively. In main() method, we’re calling these methods using object of Calculator class and printing the result. Based on the type of arguments passed, compiler decides the method using static binding which method is to be called and result is printed accordingly.
package com.tutorialspoint;
class Calculator{
public int add(int a, int b){
return a + b;
}
public double add(double a, double b){
return a + b;
}
}
public class Tester {
public static void main(String args[]){
Calculator calculator = new Calculator();
System.out.println(calculator.add(20, 40));
System.out.println(calculator.add(20.0, 40.0));
}
}
60
60.0