"Java Fundamentals" "Sixth Lesson" interface interface

Source: Internet
Author: User

The interface syntax provided by Java is designed to separate the interface from the class to form an independent body.

First, join us to define this cup interface:

Interface cup{    void addwater (int  w);     void Drinkwater (int  w);}

Interface, note the highlights:

1. You do not need to define the body of the method

2. Visibility not required for description (public by default)

Implement an interface in a class, such as the following Musiccup

class Implements cup{    publicvoid addwater (int  W)    {        = water + w;    }          Public void Drinkwater (int  W)    {        = water- w;    }     Private     int water = 0;}

It is important to note that once a interface is implemented in a class, all methods of Interface (Addwater () and Drinkwater ()) must be defined in the class. The methods in the class need to match the method prototypes in the interface. Otherwise, Java will error.

The meaning of the interface interface exists:

We used the interface, but the interface didn't reduce the amount of work we had when we defined the class. We still have to write the class exactly as before. We even have to be more careful,

Must not violate the interface's rules. So why should we use interface?

In fact, interface is like an industry standard. A factory (class) can adopt the industry standard (implement Interface), or it can not adopt the industry standard.

However, a product that incorporates industry standards will have the following benefits:

• Higher quality: Cups that do not have a water-adding function do not meet the standard.

• Easier to promote: As with the USB interface on a computer, downstream products can be more easily bridged.

If we already have a Java program that handles objects that match the CPU interface, such as picking a child to drink. So, as long as we are sure, we give the child's Cup (object) to implement a cup interface, you can ensure that children can perform water this action.

As for how the Cup (object) defines the action of drinking water, we can leave it to the appropriate class to decide (for example, to drink water with a straw, or to open a small mouth to drink water).

Multiple interfaces:

A class can implement more than one interface interface.

For example, we also have a interface:

Interface musicplayer{    void  play ();

So the real Musiccup also needs to implement this interface, so it looks like this:

classMusiccupImplementsCup, musicplayer{ Public voidAddwater (intW) {water= water +W; }         Public voidDrinkwater (intW) {water= Water-W; }         Public voidPlay () {System.out.println ("Dun...dun...dun ..."); }            Private     intWater = 0;}

That's all, all right, let's see it with a source:

1 InterfaceCup2 {3     voidAddwater (intW);4     voidDrinkwater (intW);5 }6 7 InterfaceMusicplayer8 {9     voidplay ();Ten } One  A  - /*This class if implements Cup, then the method defined in Cup, in Musiccup - must have Addwater and Drinkwater, or will be error, this is not the same as C + +*/ the classMusiccupImplementsCup, Musicplayer - { -      Public voidAddwater (intW) -     { +Water = water +W; -System.out.println ("Water is" +water); +     } A      at      Public voidDrinkwater (intW) -     { -Water = water-W; -System.out.println ("Water is" +water); -     } -      in      Public voidPlay () -     { to          for(inti = 0; I <water; i++) +         { -System.out.println ("Dun...dun...dun ..."); the         } *     } $     Panax Notoginseng      Public intwatercontent () -     { the         returnwater; +     }     A      the     Private     intWater = 0; + } -  $  Public classTest $ { -      Public Static voidMain (string[] args) -     { theMusiccup Mycupcup =NewMusiccup (); -Mycupcup.addwater (5);Wuyi Mycupcup.play (); theMycupcup.drinkwater (3); - Mycupcup.play (); WuSystem.out.println ("Water content is" +mycupcup.watercontent ());  -     } About}
View Code

Output Result:

Water is 5
Dun...dun...dun ...
Dun...dun...dun ...
Dun...dun...dun ...
Dun...dun...dun ...
Dun...dun...dun ...
Water is 2
Dun...dun...dun ...
Dun...dun...dun ...
Water content is 2

"Java Fundamentals" "Sixth Lesson" interface interface

Related Article

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.