This article for everyone to share the Java interface Two classic usage, for your reference, the specific contents are as follows
Dynamic loading instance of 1.Java Polymorphic interface
Write a generic program that calculates the time required for no vehicle to run 1000 kilometers, and each vehicle is known to have an expression of 3 integers a, B, and C. There are two tools available: Car and plane, where the car's speed operation formula is: A+b+c. Three classes need to be written: Computetime.java,palne.java,car.java and Interface Common.java. In the future, if you increase the 3rd medium of transportation, do not need to modify any previous procedures, only to write a new means of transport procedures. The process is as follows:
Enter the Computetime four parameters from the command line, the first is the type of transport, the 第二、三、四个 parameters are integers a, B, and C respectively. Examples are as follows:
Calculate Plane Time: "Plane 20 30 40"
Calculate car time: "Car 23 34 45"
If the 3rd medium of transportation is ship, you need to write Ship.java, run-time input: "Ship 22 33 44"
tip: make full use of the concept of interface, the interface object acts as a parameter.
Another way to instantiate an object: Class.forName (str). newinstance (); For example, you need to instantiate
A Plane object, just call Class.forName ("Plane"). Newinstance () is available.
Code:
1. Computtime. Java Please make sure the input is correct, which does not capture numberfromatexception
Import CalTime.vehicle.all.Common;
import java. lang.*;
public class Computetime {public
static void Main (String args[]) {
System.out.println ("means of transport: +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 ("Run Time:" +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);
}
Run Results once:
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 the interface, and thinking in Java also makes a deep analysis of it, which can be viewed below.
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 is 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);
}
The parameters of an interface type can be used as a method parameter, and the class that implements the interface can be passed to the method in practical use, followed by the method or executed in accordance with the overriding principle, actually calling the method code body in the implementation class. This allows different functions to be implemented according to the different parameters that are passed in.
The important thing is that when I need another object later 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 the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.