Computable.java
Public Interface computable { int MAX = ; int f (int x);}
China.java
Public class Implements computable { //Chinaclass implements computable interface int number ; Public int f (int// do not forget the Public keyword int sum=0; for (int i=1;i<=x;i++) { sum=sum+i; } return sum; }}
Japan.java
Public class Implements // Japan class implements computable interface int Number ; Public int f (int x) { return max+x; }}
Example6_1.java
public class Example6_1 { public static void main (String args[]) { China Zhang; Japan Henlu; Zhang =new China (); Henlu =new Japan (); Zhang.number =32+COMPUTABLE.MAX; Henlu.number =14+COMPUTABLE.MAX; System.out.println ( "Zhang's study number" +zhang.number+ ", Zhang Sum result" +zhang.f (100 Henlu "+henlu.number+", Henlu sum result "+henlu.f (100
Understanding of the interface
If cars, trucks, tractors, motorcycles and buses are all sub-categories of motor vehicles, the motor vehicle is an abstract class. Motor vehicles such as "brakes", "steering" and other methods are reasonable, that is, the requirements of cars, trucks, tractors, motorcycles and buses must be specific to achieve "brake", "steering" and other functions. But the motor vehicle class contains two abstract class method "Charge fee", "Adjust the temperature", then all subclasses must rewrite these two methods, namely gives the method body, produces the individual charge to obtain the control temperature behavior. This obviously does not accord with people's thinking logic, because the tractor may not need to "charge", "Adjust the temperature" function, while some other classes, such as aircraft, ships and so may also need to specifically realize the "charge", "adjust the temperature."
The idea of an interface is that it requires that some classes have the same method, but that the specific content of the method (the content of the method body) can be different, that is, requiring these classes to implement interfaces to ensure that these classes must have the methods declared in the interface (that is, the so-called method bindings).
Example6_2.java
Abstract classMotorvehicles {Abstract voidbrake ();}InterfaceMoneyfare {voidcharge ();}InterfaceControltemperature {voidcontrolairtemperature ();}classBusextendsMotorvehiclesImplementsMoneyfare {voidBrake () {System.out.println ("The use of hub-type braking technology for buses"); } Public voidcharge () {System.out.println ("Bus: one yuan/sheet, not counting kilometers"); }} classTaxiextendsMotorvehiclesImplementsMoneyfare,controltemperature {voidBrake () {System.out.println ("Taxi Use disc brake technology"); } Public voidcharge () {System.out.println ("Taxi: 2 yuan/km, starting at 3 km"); } Public voidcontrolairtemperature () {System.out.println ("The taxi has hair air conditioning."); }}classCinemaImplementsMoneyfare,controltemperature { Public voidcharge () {System.out.println ("Cinema: Tickets, 10 yuan/piece"); } Public voidcontrolairtemperature () {System.out.println ("The cinema is equipped with central air conditioning"); }} Public classExample6_2 { Public Static voidMain (String args[]) {Bus bus101=NewBus (); Taxi Buletaxi=NewTaxi (); Cinema Redstarcinema=NewCinema (); Bus101.brake (); Bus101.charge (); Buletaxi.brake (); Buletaxi.charge (); Buletaxi.controlairtemperature (); Redstarcinema.charge (); Redstarcinema.controlairtemperature (); }}
Personal understanding: Some public class methods are used by everyone to inherit, some special to define an interface to achieve.
Class Taxi extends Motorvehicles implements Moneyfare,controltemperatur
Java interface and implementation