This article shares two classic usages of java interface for your reference. The specific content is as follows
1. Dynamic loading example of Java polymorphic interface
Write a general program to calculate the time it takes for non-transport vehicles to run 1,000 kilometers. It is known that the parameters of each transport are 3 expressions of integers A, B, and C. There are two existing tools: Car and Plane, where the speed calculation formula of Car is: A+B+C. There are three classes that need to be written: ComputeTime.java, Palne.java, Car.java and interface Common.java. It is required that if the third transportation vehicle is added in the future, there is no need to modify any previous procedures, but only new transportation vehicle procedures are required. The operation process is as follows:
Enter the four parameters of ComputeTime from the command line. The first is the type of the vehicle, and the second, third and fourth parameters are the integers A, B, and C. As an example:
Calculate the time of Plane: "Plane 20 30 40"
Calculate the time of Car: "Car 23 34 45"
If the vehicle in the third place is Ship, you need to write Ship.java, and the runtime input: "Ship 22 33 44"
Tip: Make full use of the concept of interfaces, and interface objects act as parameters.
Another way to instantiate an object: Class.forName(str).newInstance(); for example, it is necessary to instantiate
For a Plane object, just call Class.forName("Plane").newInstance().
Code:
1. ComputTime.java Please make sure the input is correct, where the NumberFromatException is not caught
import CalTime.vehicle.all.Common;import java .lang.*;public class ComputeTime {public static void main(String args[]) {System.out.println("Travel: "+args[0]);System.out.println("Parameter A: "+args[1]);System.out.println("Parameter B: "+args[2]);System.out.println("Parameter C: "+args[3]);double A=Double.parseDouble(args[1]);double B=Double.parseDouble(args[2]);double C=Double.parseDouble(args[3]);double v,t;try {Common d=(Common) Class.forName("CalTime.vehicle."+args[0]).newInstance();v=d.runTimer(A,B,C);t=1000/v;System.out.println("Average speed: "+v+" km/h");System.out.println("Runtime: "+t+" hours");} catch(Exception e) {System.out.println("class not found”);}}} 2.Plane.java
package CalTime.vehicle;import CalTime.vehicle.all.Common;public class Plane implements Common {public double runTimer(double a, double b, double c) {return (a+ b + c);}} 3. Car.java
package CalTime.vehicle;import CalTime.vehicle.all.Common;public class Car implements Common {public double runTimer(double a, double b, double c) {return ( a*b/c );}} 4.Common.java
package CalTime.vehicle.all;public interface Common {double runTimer(double a, double b, double c);} One run result:
C:/ java> java ComputeTime Car 100 45 67
Transportation: Car
Parameter A: 100
Parameter B: 45
Parameter C: 67
Average speed: 67.16417910447761 km/h
Running time: 14.88888888888889 hours
C:/ java > java ComputeTime Plane 130 45 67
Transportation: Plane
Parameter A: 130
Parameter B: 45
Parameter C: 67
Average speed: 242.0 km/h
Running time: 4.132231404958677 hours
This example demonstrates the classic usage of interfaces, and Thinking in java has also made a profound analysis on this, so you can check it out.
2. The interface is passed as a parameter of the method.
Example:
interface Extendbroadable{public void inPut();}class KeyBroad implements Extendbroadable{public void inPut(){System.out.println("/n hi,keybroad has be input into then mainbroad!/n");}}class NetCardBroad implements Extendbroadable{public void inPut(){System.out.println("/n hi,netCardBroad has be input into then mainbroad!/n");}}class CheckBroad{public void getMainMessage(Extendbroadable ext){ext.inPut();}}public class InterfaceTest01{public static void main(String []args){KeyBroad kb=new KeyBroad();NetCardBroad ncb=new NetCardBroad();CheckBroad cb=new CheckBroad();cb.getMainMessage(kb);cb.getMainMessage(ncb); }} Parameters of interface type can be used as method parameters. In actual use, the class that implements the interface can be passed to the method, and the method may be executed according to the principle of rewriting. The actual call is the method code body in the implementation class. This allows different functions to be implemented according to the parameters transmitted in.
What's important is that when I need another object in the future and have my own method body, we don't have to rewrite the original class, we just need a new class to implement the interface.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.