Here I take the customer to buy a camera for example to illustrate the application of Java reflection mechanism. The classes and interfaces involved in the example are:
Camera interface: Defines the Takephoto () method.
Camera01 class: A type of camera that implements the camera interface.
CAMERA02 class: Another type of camera that implements the camera interface.
Seller class: Selling cameras.
Customer class: Buy a camera, there is a main method.
All classes are in a COM package.
The procedure is as follows:
public interface Camera {
//声明照相机必须可以拍照
public void takePhoto();
}
public class Camera01 implements Camera {
private final int prefixs =300;//300万象素
private final double optionZoom=3.5; //3.5倍变焦
public void takePhoto() {
System.out.println("Camera01 has taken a photo");
}
}
Similar to having
public class Camera02 implements Camera {
private final int prefixs =400;
private final double optionZoom=5;
public void takePhoto() {
System.out.println("Camera02 has taken a photo");
}
}