In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces how to use the static and default of the interface in java8. It is very detailed and has a certain reference value. Friends who are interested must read it!
Static method
A new feature has been added to the interface in java8: define one or more static methods. The usage is the same as the ordinary static method.
The static method can be defined in the interface, through the interface name. Method name () call, implementing class cannot inherit static method
Public interface InterfaceA {/ * static methods cannot be overridden by the implementation class * / static void hello () {System.out.println ("Hello Java8");}}
How to use it:
Public class Test {public static void main (String [] args) {InterfaceA.hello ();}}
Default method
In the interface, the default method is added to add new functionality to the classes of thousands of existing Java class libraries, and there is no need to redesign these classes. For example, you only need to add default Stream stream () to the Collection interface, and the corresponding Set and List interfaces and their subclasses contain this method, and you don't have to re-copy this method for each subclass.
The default method can be defined in the interface. The default-modified method has a method body, which represents the default implementation of this method. The subclass can call it directly, and can choose to rewrite or not to rewrite it.
Public interface Human {/ * must be rewritten * / void oldMethod (); / * the implementation class may or may not rewrite * / default void hello () {System.out.println ("hello Human!");}}
However, if the implementation class implements both the interface Human and the interface Food interface, and the default method with the same name is defined in the Food interface, then two methods must be overridden in the implementation class.
Public interface Food {default void hello () {System.out.println ("hello Food!");}} public class Person implements Human,Food {@ Override public void oldMethod () {} / * when there are default methods with the same method signature in multiple interfaces, the implementation class must override the method * / @ Override public void hello () {System.out.println ("Human eats Food!");}}
The above is all the contents of the article "how to use static and default of interfaces in java8". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.