Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use JAVA interface to implement multiple implementation classes

Shulou Source: shulou.com Published: 2022-06-02 02:28:10 09月22日 Update

This article mainly introduces how to use the JAVA interface to achieve multi-implementation classes, which has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, let the editor take you to understand it.

Class NoteBook {/ / Notebook enables the running function public void run () {System.out.println ("Notebook running") } / / Notebook uses usb device. When the notebook object calls this function, it must pass a USB device public void useUSB (USB usb) that conforms to USB rules {/ / to determine whether there is a USB device if (usb! = null) {usb.open (); usb.close () }} public void shutDown () {System.out.println ("Notebook closed");}} public class Test {public static void main (String [] args) {/ / create notebook entity object NoteBook nb = new NoteBook (); / / Notebook opens nb.run (); / / create mouse entity object Mouse m = new Mouse () / / Notebook uses mouse nb.useUSB (m); / / creates keyboard entity object KeyBoard kb = new KeyBoard (); / / Notebook uses keyboard nb.useUSB (kb); / / Notebook closes nb.shutDown ();}} class KeyBoard implements USB {public void open () {System.out.println ("Keyboard on") } public void close () {System.out.println ("keyboard off");}} class Mouse implements USB {public void open () {System.out.println ("mouse on");} public void close () {System.out.println ("mouse off");}} interface USB {void open (); / / enable void close () / / close function} Thank you for reading this article carefully. I hope the article "how to use JAVA interface to implement multiple implementation classes" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Tags: Notes notebooks functions objects articles keyboards mice entities devices interfaces operations values interests at the same time more friends knowledge ribbons industries rules Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Shulou Technology Xiaomi Linux MariaDB