In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article focuses on "how to define the Java interface", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to define the Java interface.
Class keyword is used to define classes and interface keyword is used to define interfaces.
An interface is a collection of constants and abstract methods. The data members in the interface, with or without modifiers "public", "static", or "final", are public static final and must be initialized. The methods in the interface, whether or not decorated with the modifiers "public" or "abstract", are public abstract and must not have a method body.
Multiple classes or interfaces can be defined in the source program, but there can be at most one class or interface of public. If there is one, the source file must have the same name as the class and interface of public.
Interfaces can be inherited, and subinterfaces can inherit constants and abstract methods in the parent interface and add new abstract methods, and so on.
The interface has no constructor and cannot be instantiated.
The methods in the interface must be implemented by the class, defining a class that implements the interface in the format:
Modifier class class name extends parent class name implements interface name 1, interface name 2,... , interface name n {
Realization method
}
If a class cannot implement all the methods in all the interfaces listed after the implements keyword, the class must be decorated with abstract to become an abstract class.
A class can inherit only one parent class, but it can implement multiple interfaces.
Although it is not allowed to create an instance of an interface, it allows variables of the interface type to be defined to refer to an instance of the class that implements the interface.
Example:
Import static java.lang.System.*
Public class Demo {
Public static void main (String [] args) {
SataHdd sh2=new SeagateHdd (); / / initialize Seagate hard disk
SataHdd sh3=new SamsungHdd (); / / initialize Samsung hard disk
}
}
/ / Serial hard disk interface
Interface SataHdd {
The number of public static final int CONNECT_LINE=4;// connections, which can also be written as int CONNECT_LINE=4
Public abstract void writeData (String data); / / write data, or write as void writeData (String data)
Public abstract String readData (); / / read the data, or write as String readData ()
}
/ / repair the hard disk interface
Interface fixHdd {
String address = "Haidian District, Beijing"; / / maintenance address
Boolean doFix (); / / start maintenance
}
/ / Seagate hard drive
Class SeagateHdd implements SataHdd, fixHdd {
Public String readData () {return "data";} / / read data from Seagate hard disk
Public void writeData (String data) {out.println ("write successful");} / / write data to Seagate hard disk
Public boolean doFix () {return true;} / / repair Seagate hard disk
}
/ / Samsung hard disk
Class SamsungHdd implements SataHdd {
Public String readData () {return "data";} / / Samsung hard disk reads data
Public void writeData (String data) {out.println ("write successful");} / / Samsung hard disk writes data
}
/ / A shoddy hard disk cannot write data
Abstract class XXHdd implements SataHdd {
Public String readData () {return "data";} / / read data from hard disk
}
One interface does not implement another interface, but can inherit multiple other interfaces. The multi-inheritance feature of the interface makes up for the single inheritance of the class:
Public interface SataHdd extends A,B {
Public static final int CONNECT_LINE = 4
Public void writeData (String data)
Public String readData ()
}
Interface A {
Public void a ()
}
Interface B {
Public void b ()
}
At this point, I believe you have a deeper understanding of "how to define the Java interface". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.