In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Today, I will talk to you about how to deeply understand the interface in Java. Many people may not know much about it. In order to make you understand better, the editor has summarized the following for you. I hope you can get something according to this article.
I. Preface
Earlier we talked about the overview of abstract classes, we also have an understanding and understanding of abstract classes, now we learn a very important content interface, the word interface is more abstract, but it is also very easy to understand.
II. Interface
To put it simply, an interface is a regulated standard, which can be universal as long as it conforms to this standard. The performance of an interface lies in the abstraction of behavior. Just like the socket, we can use the socket produced by every manufacturer, this is because they have the same standard, and here the socket is the interface.
Format of the interface
Format 1:
Public interface interface name
Format 2:
Interface interface name
III. Characteristics of the interface
Instantiation of ① Polymorphism using the implement keyword
Public interface usb {} / / define an interface public class computer implements usb {} / / create a class to implement the interface
The ② interface cannot be instantiated directly, but can be instantiated in the form of polymorphism.
Usb u = new computer ()
The ③ interface is full of abstract methods and constants, there can be no non-abstract methods, constants must be assigned.
Constants are defaulted by public static final
The method is modified by public static abstractl by default
Public final int num1=10; √ num1=20; × int num2=10; √ num2=20; × the above two correct spellings are equivalent to public abstract void method1 (); √ void method2 (); √
Directly int num=10, the a created is constant
The premise of ④ polymorphism:
Have an inheritance or implementation relationship
There is a method of rewriting
There is a parent class or parent interface reference to a subclass or implementation class object
Implementation class of ⑤ interface
All methods in the interface must be overridden, or they can be abstract classes (plus abstract) if you don't want to.
But the subclass inherits the methods that the abstract class must override in the interface.
⑥ interface can only be modified by public
The use of the interface
Under the service package
Package service; public interface EatService {void eat ();}
Under the impl package under the service package
Package service.impl; import service.EatService; public class CatImpl implements EatService {@ Override public void eat () {System.out.println ("eat");}}
Under the controller package
Package controller; import service.EatService;import service.impl.CatImpl; public class CatController {public static void main (String [] args) {EatService e=new CatImpl (); e.eat ();}} IV. The relationship between class and interface
There is an inheritance relationship between classes and classes.
There is an inheritance relationship between interfaces and interfaces.
There is an implementation relationship between the class and the interface (the class implements the interface)
Classes only support single inheritance (that is, a subclass has only one parent class), and can implement multiple classes
Public class An extends B implements C,B,D.... {}
There can be multiple inheritance between interfaces.
Benefits of multiple inheritance of public interface an implements bforce c. {} interface
Many interfaces are defined, and it will be a lot of overhead if a device wants to implement so many interfaces at the same time.
People of the same kind can inherit it with an interface, so that this interface requires all the functions of those interfaces, and we
You only need to implement one class.
Interface 1
Package service; public interface EatService {void eat ();}
Interface 2
Package service; public interface PlayService {void play ();}
Interface 3
Package service; public interface SleepService {void sleep ();}
Master interface: used to inherit the above three interfaces
Package service; public interface SumService extends SleepService,EatService,PlayService {}
At this point, the original class implementation only needs to implement a general interface.
Old:
Public class CatImpl implements EatService,PlayService,SleepService {/ / method rewriting}
New:
Public class CatImpl implements SumService {/ / method rewriting}
Calling in controller is also a lot easier.
Package controller;import service.SumService;import service.impl.CatImpl;public class CatController {public static void main (String [] args) {SumService ss=new CatImpl (); ss.eat (); ss.play (); ss.sleep ();}}
Practice
Define an interface to compare two objects.
Package cn.com.Classwork190124;public interface CompareObject {public int compareto (object o); / / if the returned value is 0, it means equality; if it is positive, it means the current object is large; and the negative number represents the current object is small}
Define a Circle class.
Package cn.com.Classwork190124;public class Circle {private double radius; private final double PI=3.14; public double findArea () {return PI*radius*radius;} public double getRadius () {return radius;} public void setRadius (double radius) {this.radius = radius;}}
Define a ComparableCircle class that inherits the Circle class and implements the CompareObject interface. The implementation of the method compareTo in the interface is given in the ComparableCircle class, which is used to compare the radius of two circles.
Package cn.com.Classwork190124;public class ComparableCircle extends Circle implements CompareObject {@ Override public int compareto (Circle o) {if (this.getRadius () = = o.getRadius ()) {return 0;} else if (this.getRadius ())
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.