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:
- Re-declare the Getzoneddatetime method so that it becomes an abstract type.
- Redefine the Getzoneddatetime method.
- 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