Java interface and implementation

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.