Case: Make full use of interface and reflection to load classes without changing the original code
- There are two kinds of tools available: Car and Plane. Car's speed formula is A*B/C and Plane's speed formula is A+B+C. There are three kinds of tools to be written: ComputeTime.java,Plane.java,Car.java and Common.java.
- In the future, if the third type of vehicle is added, it is not necessary to modify any previous program, but only to write a new one. Its operation process is as follows: input four parameters of ComputeTime from the command line, the first is the type of vehicle, and the second, third and fourth parameters are integers A, B and C, respectively.
- Examples are as follows:
Calculating Plane Time: "java ComputeTime Plane 20 30 40"
Calculating the time of Car007: "java ComputeTime Car 23 3445"
The third vehicle is Ship, which only needs to write Ship.java and input at runtime: "java ComputeTime Ship 22 33 44"
First, design class:
- First of all, each vehicle needs a separate class, which includes four variables: vehicle type, A, B, C, and the getSpeed method for calculating speed.
- Then you need a main class that receives keyboard data, initializes the vehicle class, creates its object, and calls the getSpeed method to output the corresponding data.
- We need interfaces to abstract getSpeed methods of different subclasses of vehicles, but because interfaces can not be instantiated, we can not directly call all kinds of getSpeed methods using interfaces.
- Finally, a Speed class is created, and a vehicleMethod method is created. With the interface object as the parameter, each getSpeed method is invoked in the method, so that the object invocation method can be implemented in the main class.
Main class:
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.Scanner;
/**
* @Description: Using reflection to load classes
* @author: Joker
* @date: 2017 January 29, 2001, 8:30:26 p.m.
*/
public class Java_4
{
public static void main(String[] args)
{
for (int i = 0; i < 10000; i++)
{
//Keyboard input data in corresponding format
Scanner sc = new Scanner(System.in);
System.out.println("Please enter data in format");
String string = sc.nextLine();
// Regular expression that splits the string by spaces
String[] str2 = string.split(" ");
// Get the object name and the values of A,B,C
String vehicleName = str2[2];
int A = Integer.valueOf(str2[3]);
int B = Integer.valueOf(str2[4]);
int C = Integer.valueOf(str2[5]);
try
{
//Gets the bytecode file object of the input class
Class vehicleClass = Class.forName("com.cskaoyan.Day19_HomeWork."
+ vehicleName);
//Get the construction method and initialize the data received by the keyboard
Constructor constructor = vehicleClass.getConstructor(String.class, int.class, int.class, int.class);
//Create interface objects. References to parent classes point to instances of subclasses to achieve polymorphism
TansportMeans t = (TansportMeans) constructor.newInstance(vehicleName,new Integer(A), new Integer(B), new Integer(C));
//Create speed object to call vehicleMethod method
Speed vehicle = new Speed();
vehicle.vehicleMethod(t);
}
catch (ClassNotFoundException e)
{
e.printStackTrace();
}
catch (NoSuchMethodException e)
{
e.printStackTrace();
}
catch (SecurityException e)
{
e.printStackTrace();
}
catch (InstantiationException e)
{
e.printStackTrace();
}
catch (IllegalAccessException e)
{
e.printStackTrace();
}
catch (IllegalArgumentException e)
{
e.printStackTrace();
}
catch (InvocationTargetException e)
{
e.printStackTrace();
}
}
}
}
Interfaces and classes that implement polymorphism:
/**
* @Description:Create interfaces to define specific methods in transportation
*/
interface TansportMeans
{
public abstract void getSpeed();
}
/**
* Create the speed class and use the vehicleMethod method to call getSpeed() method in various vehicle classes
*/
class Speed
{
public void vehicleMethod(TansportMeans vehicle)
{
vehicle.getSpeed();
}
}
Transportation:
/**
* Car The member variables of the class are vehicleName,A,B,C,
* In order to receive keyboard input data, there must be a construction method.
* To implement getSpeed method in interface, ABC must be processed and output accordingly.
*/
class Car implements TansportMeans
{
String vehicleName;
int A, B, C;
/**
* @Description:car Class construction method for initializing classes
*/
public Car(String vehicleName, int a, int b, int c)
{
this.vehicleName = vehicleName;
A = a;
B = b;
C = c;
}
/**
* @Description:car Method of Class Computing Speed
*/
@Override
public void getSpeed()
{
System.out.println(vehicleName + "The speed is:" + (A * B / C));
}
}
/**
* plane The member variables of the class are vehicleName,A,B,C,
* In order to receive keyboard input data, there must be a construction method.
* To implement getSpeed method in interface, ABC must be processed and output accordingly.
*/
class Plane implements TansportMeans
{
String vehicleName;
int A, B, C;
//Construction method
public Plane(String vehicleName, int a, int b, int c)
{
this.vehicleName = vehicleName;
A = a;
B = b;
C = c;
}
@Override
public void getSpeed()
{
System.out.println(vehicleName + "The speed is:" + (A + B + C));
}
}
Finally, add a ship class and write the ship.java class directly, which can realize the calculation of ship speed.
class Ship implements TansportMeans
{
String vehicleName;
int A, B, C;
//Construction method
public Ship(String vehicleName, int a, int b, int c)
{
this.vehicleName = vehicleName;
A = a;
B = b;
C = c;
}
@Override
public void getSpeed()
{
System.out.println(vehicleName + "The speed is:" + ((A + B) * C));
}
}
Console output results:
Please enter data in format java ComputeTime Car 23 34 45 Car's speed is: 17 Please enter data in format java ComputeTime Plane 20 30 40 Plane's speed is 90 Please enter data in format java ComputeTime Ship 22 33 44 Ship's speed is 2420 Please enter data in format