Teach you how to define a method in the Java interface

Source: Internet
Author: User

Basically all Java tutorials will tell us that the Java interface method is public, abstract type, no method body.

But in the JDK8, you can break through the boundaries of the OH.

Suppose we now have an interface: Timeclient, whose code structure is as follows:

Import java.time.*;  Public interface Timeclient {    void settime (int hour, int minute, int second);    void setDate (int day, int month, int year);    void Setdateandtime (int day, int month, int year,                               int hour, int minute, int second);    LocalDateTime getlocaldatetime ();}

Next, the Simpletimeclient class implements the Timeclient interface, with the following code:

Package Defaultmethods;import java.time.*;import java.lang.*;import java.util.*;p ublic class Simpletimeclient        Implements Timeclient {private LocalDateTime dateandtime;    Public simpletimeclient () {dateandtime = Localdatetime.now (); } public void SetTime (int hour, int minute, int second) {Localdate currentdate = Localdate.from (dateandtime        );        LocalTime Timetoset = Localtime.of (hour, minute, second);    DateAndTime = Localdatetime.of (currentdate, Timetoset);        } public void SetDate (int day, int month, int) {Localdate Datetoset = Localdate.of (day, month, year);        LocalTime currenttime = Localtime.from (DateAndTime);    DateAndTime = Localdatetime.of (Datetoset, currenttime); public void Setdateandtime (int day, int month, int year, int hour, int minute, int        Second) {Localdate Datetoset = Localdate.of (day, month, year); LocalTime Timetoset = Localtime.of (Hour, minute, second);    DateAndTime = Localdatetime.of (Datetoset, Timetoset);    } public LocalDateTime Getlocaldatetime () {return dateandtime;    } public String toString () {return dateandtime.tostring ();        public static void Main (String ... args) {timeclient mytimeclient = new Simpletimeclient ();    System.out.println (Mytimeclient.tostring ()); }}

Now suppose you want to add a new feature to the interface Timeclient, which allows us to specify the time zone in which we are located.

 Public Interfacetimeclient {voidSetTime (intHourintMinuteintsecond); voidSetDate (intDayintMonthintYear ); voidSetdateandtime (intDayintMonthintYear ,intHourintMinuteintsecond);                           LocalDateTime Getlocaldatetime (); zoneddatetime getzoneddatetime (String zonestring);}

As timeclient changes, you have to modify your simpletimeclient class to implement the Getzoneddatetime method. In general, setting the time zone is a common feature in each Timeclient implementation class. At this point, you will typically choose to define a Abstracttimeclient class to implement the Getzoneddatetime method. In JDK8, you can choose to implement the method directly in the interface (interface has reached the site of the abstract class).

 PackageDefaultmethods;ImportJava.time.*; Public Interfacetimeclient {voidSetTime (intHourintMinuteintsecond); voidSetDate (intDayintMonthintYear ); voidSetdateandtime (intDayintMonthintYear ,intHourintMinuteintsecond);        LocalDateTime Getlocaldatetime (); StaticZoneID Getzoneid (String zonestring) {Try {            returnZoneid.of (zonestring); } Catch(datetimeexception e) {System.err.println ("Invalid time zone:" + zonestring + "; Using the default time zone instead.); returnZoneid.systemdefault (); }    }            defaultzoneddatetime getzoneddatetime (String zonestring) {returnZoneddatetime.of (Getlocaldatetime (), Getzoneid (zonestring)); }}

From the above example, we can see that through the static and default modifiers we can implement the method body directly in the interface, and do not forget that any method declaration in the interface is of the public type Oh.

OK, now we need a new interface: Anothertimeclient, which has to inherit the Timeclient interface. So, for the Getzoneddatetime method defined in the Timeclient interface, you can do the following three kinds of processing:

    1. Re-declare the Getzoneddatetime method so that it becomes an abstract type.
    2. Redefine the Getzoneddatetime method.
    3. Direct inheritance.

The static method is consistent with the static method concept we define in the class.

Teach you how to define a method in the Java 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.