Classification and usage scenarios of interfaces in Java
1. Classification of interfaces
In Java, an interface is a standardized definition used to define classes The method that should be implemented. Interfaces can be divided into the following types:
public interface Shape { double calculateArea(); // 计算面积的方法 double calculatePerimeter(); // 计算周长的方法 }
The above Shape interface defines a method to calculate area and perimeter.
public interface OnButtonClickListener { void onButtonClick(); // 按钮点击时调用的方法 }
The above-mentioned OnButtonClickListener interface is used to handle button click events.
@FunctionalInterface public interface Calculator { int calculate(int a, int b); // 计算两个数的方法 }
The above-mentioned Calculator interface defines a method for calculating two numbers.
2. Usage scenarios of interfaces
Interfaces have a wide range of application scenarios in Java. The following are several common usage scenarios:
public class Circle implements Shape { private double radius; public Circle(double radius) { this.radius = radius; } public double calculateArea() { return Math.PI * radius * radius; } public double calculatePerimeter() { return 2 * Math.PI * radius; } } public class Rectangle implements Shape { private double height; private double width; public Rectangle(double height, double width) { this.height = height; this.width = width; } public double calculateArea() { return height * width; } public double calculatePerimeter() { return 2 * (height + width); } } public class Main { public static void main(String[] args) { Shape circle = new Circle(5); Shape rectangle = new Rectangle(3, 4); System.out.println("Circle area: " + circle.calculateArea()); System.out.println("Circle perimeter: " + circle.calculatePerimeter()); System.out.println("Rectangle area: " + rectangle.calculateArea()); System.out.println("Rectangle perimeter: " + rectangle.calculatePerimeter()); } }
In the above example, polymorphism is achieved through the interface Shape. You can use the reference of the Shape interface to operate the Circle and Rectangle objects respectively.
public class Button { private OnButtonClickListener listener; public void setOnButtonClickListener(OnButtonClickListener listener) { this.listener = listener; } public void click() { if (listener != null) { listener.onButtonClick(); } } } public class ViewController implements OnButtonClickListener { private Button button; public ViewController() { button = new Button(); button.setOnButtonClickListener(this); } public void onButtonClick() { // 处理按钮点击事件 System.out.println("Button clicked"); } public static void main(String[] args) { ViewController viewController = new ViewController(); viewController.button.click(); } }
In the above example, the Button class implements the processing of button click events through the callback interface OnButtonClickListener. The ViewController class implements the OnButtonClickListener interface and passes its instance to the Button object. When the button is clicked, the onButtonClick method of the ViewController class is called.
import java.util.Arrays; import java.util.List; public class CalculatorDemo { public static void main(String[] args) { List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5); Calculator add = (a, b) -> a + b; Calculator multiply = (a, b) -> a * b; int sum = numbers.stream().reduce(0, add::calculate); int product = numbers.stream().reduce(1, multiply::calculate); System.out.println("Sum: " + sum); System.out.println("Product: " + product); } }
In the above example, the functional interface Calculator is used to define a method for calculating two numbers. Calculator objects for addition and multiplication are implemented through Lambda expressions, and collection classes are operated through the Stream API.
Summary:
Interfaces have many classifications in Java, including regular interfaces, callback interfaces, and functional interfaces. Interfaces have a wide range of usage scenarios and can be used to implement polymorphism, event handling, functional programming, etc. Through the reference of the interface, unified operations on different implementation classes can be achieved. With the help of interfaces, we can write more flexible and extensible code.
The above is the detailed content of The purpose of interfaces in Java and the classification of application scenarios. For more information, please follow other related articles on the PHP Chinese website!