Article directory
1. Anonymous inner class
An anonymous inner class can be regarded as a special local inner class, and its essence is an anonymous subclass of an inherited class (or implemented interface).
Anonymous inner class is to dynamically specify the logic of the method body when using it, without having to create a subclass of an inherited class (or implement an interface) every time. The compiler will automatically generate an anonymous subclass during compilation.
1. Grammatical format
The grammatical format of an anonymous inner class: new interface name | class name () {override method}
Among them, if the rewriting method is not necessary, in principle, there is no rewriting method part.
2. How to use
① Traditional way
Parent-child class:
public class ParentClass {
public void aa(String str) {
}
public void bb(String str) {
}
}
public class ChildClass extends ParentClass {
@Override
public void aa(String str) {
System.out.println("自定义方法体");
}
@Override
public void bb(String str) {
System.out.println("自定义方法体");
}
}
public class Test {
public static void main(String[] args) {
ParentClass ohj = new ChildClass();
}
}
Interface implementation class:
public interface ParentInterface {
void aa(String str);
void bb(String str);
}
public class ChildClass implements ParentInterface {
@Override
public void aa(String str) {
System.out.println("自定义方法体");
}
@Override
public void bb(String str) {
System.out.println("自定义方法体");
}
}
public class Test {
public static void main(String[] args) {
ParentInterface ohj = new ChildClass();
}
}
② Anonymous inner class method
Parent-child class:
public class ParentClass {
public void aa(String str) {
}
public void bb(String str) {
}
}
public class Test {
public static void main(String[] args) {
ParentClass obj = new ParentClass() {
@Override
public void aa() {
System.out.println("自定义方法体");
}
@Override
public void bb() {
System.out.println("自定义方法体");
}
};
}
}
Interface implementation class:
public interface ParentInterface {
void aa(String str);
void bb(String str);
}
public class Test {
public static void main(String[] args) {
ParentInterface obj = new ParentInterface() {
@Override
public void aa() {
System.out.println("自定义方法体");
}
@Override
public void bb() {
System.out.println("自定义方法体");
}
};
}
}
Two, Lambda expression
Since Java8, Lambda expressions have been introduced.
For an interface with only one abstract method (functional interface) , you can directly use Lambda expression to create its anonymous implementation class instance.
- Inner classes defined using anonymous inner classes: After compilation, a separate class bytecode file (external class name $number.class) will be generated;
- Internal classes defined using Lambda expressions: After compilation, a separate class bytecode file will not be generated, and the corresponding bytecode will be dynamically generated at runtime.
1. Grammatical format
The syntax format of Lambda expression: (parameter list) -> {method body}
Omit writing:
- The parameter type of the parameter in parentheses can be omitted
- When there are no parameters in the parentheses, the parentheses cannot be omitted
- When there is only one parameter in the parentheses, the parentheses can be omitted
- When there are multiple parameters in the parentheses, the parentheses cannot be omitted
- When there is one and only one line of statements inside curly braces, it can be omitted
2. How to use
public interface ParentInterface {
void aa(String str);
}
① Anonymous inner class method
public class Test {
public static void main(String[] args) {
ParentInterface obj = new ParentInterface() {
@Override
public void aa(String str) {
System.out.println("自定义方法体");
}
};
}
}
② Lambda expression method
public class Test {
public static void main(String[] args) {
ParentInterface obj = str -> System.out.println("自定义方法体");
}
}
3. Method reference
Method reference is a further simplification of Lambda expression, borrowing the method body of other classes, eliminating the need to write the method body.
For a Lambda expression with only one line of code in the method body (this line of code is when calling a method of a class or an instance), it can be simplified as a simpler method reference.
1. Grammatical format
Syntax for method references:
grammatical type | grammatical format | example | Parameter Correspondence |
---|---|---|---|
type static method reference | class::static method | String::valueOf | The parameters of the abstract method correspond to the order of the parameters of the static method |
type constructor reference | class::new | String::new | The parameters of the abstract method correspond to the order of the parameters of the constructor |
type instance method reference | Class::InstanceMethod | String::replaceAll | The first parameter of the abstract method is the instance object that calls the reference method The remaining parameters of the abstract method correspond to the order of the parameters of the instance method |
object instance method reference | Object::InstanceMethod | s::replaceAll | The instance object s is an external parameter. The parameters of the abstract method correspond to the order of the parameters of the instance method. |
requires attention:
- When the abstract method of the interface has no return value, the method referenced by the method can have no return value;
- When the abstract method of the interface has a return value, the method referenced by the method must also have a return value, and the return value type instanceof the abstract method return value type of the interface
2. How to use
① Static method reference of type
Static method reference of type: ClassName::StaticMethod
Corresponding Lambda expression: (parameter 1, parameter 2,...) -> class name. static method (parameter 1, parameter 2,...)
@FunctionalInterface
public interface ParentInterface {
String aa(String s);
}
public class Test {
public static void main(String[] args) {
// 类型的静态方法引用
ParentInterface parentInterface1 = String::valueOf;
// 对应Lambda表达式方式
ParentInterface parentInterface2 = s -> String.valueOf(s);
}
}
② Type constructor reference
Type's constructor reference: class::new
Corresponding Lambda expression: (parameter 1, parameter 2,...) -> new class name (parameter 1, parameter 2,...)
@FunctionalInterface
public interface ParentInterface {
String aa(String s);
}
public class Test {
public static void main(String[] args) {
// 类型的构造方法引用
ParentInterface parentInterface1 = String::new;
// 对应Lambda表达式方式
ParentInterface parentInterface2 = s -> new String(s);
}
}
③ Type instance method reference
Instance method reference of type: Class::InstanceMethod
Corresponding Lambda expression: (parameter 1, parameter 2,...) -> parameter 1. Static method (parameter 2,...)
@FunctionalInterface
public interface ParentInterface {
String aa(String s1, String s2, String s3);
}
public class Test {
public static void main(String[] args) {
// 类型的实例方法引用
ParentInterface parentInterface1 = String::replaceAll;
// 对应Lambda表达式方式
ParentInterface parentInterface2 = (s1,s2,s3) -> s1.replaceAll(s2,s3);
}
}
④ Object instance method reference
Object's instance method reference: Object::InstanceMethod
Corresponding Lambda expression: (parameter 1, parameter 2,...) -> object. Static method (parameter 1, parameter 2,...)
@FunctionalInterface
public interface ParentInterface {
String aa(String s1, String s2);
}
public class Test {
public static void main(String[] args) {
String s = "";
// 对象的实例方法引用
ParentInterface parentInterface1 = s::replaceAll;
// 对应Lambda表达式方式
ParentInterface parentInterface2 = (s1,s2) -> s.replaceAll(s1,s2);
}
}
4. Summary of Comparative Analysis of Anonymous Inner Classes, Lambda Expressions, and Method References
- Lambda expressions are shorthand for anonymous inner classes (an anonymous inner class is the implementation of a functional interface)
- Method reference is shorthand for Lambda expression (the method body of Lambda expression has only one line of code and the line of code method call)