The following is a java4android video tutorial from marschen.
The methods used by interfaces actually reflect the essence of objects.
Interfaces are definitely the most important.
Main content.
1. What is an interface.
2. Basic interface syntax.
Computer ==>> USB interface. can be connected to MP3, mobile phone, refrigerator.
USB
Interface syntax. (It is a pure abstract class .)
1. Use interface definition.
2. All methods in the interface are abstract methods.
3. All methods in the interface have the public permission.
//************************************** *********************************//
====>>>> Defining interfaces is the standard for defining call objects. It is very important. <<<<<==
//************************************** **********************************//
1. Implement the interface using the implements keyword.
2. A class can implement multiple interfaces. (Unlike a class that can only inherit one parent class)
3. One interface can inherit multiple interfaces.
Implementation is a special inheritance
Program example
USB Interface
interface USB{public void read();public void write();}
Implement USB Interface
class USBPhoneimplements USB{public void read(){System.out.println("USBPhone read");}public void write(){System.out.println("USBPhone write");}}
Test 1
class Test{public static void main(String args[]){USBPhone usbPhone = new USBPhone();USB usb = usbPhone;usb.read();usb.write(); }}
Define WiFi Interface
interface WiFi{public void open();public void close();}
Use phone to implement USB and WiFi Interfaces
class Phoneimplements USB,WiFi{public void read(){System.out.println("Phone read");}public void write(){System.out.println("Phone write");}public void open(){System.out.println("WiFi open ");}public void close(){System.out.println("WiFi Close");}}
Test 2
Class test {public static void main (string ARGs []) {// USB phone = new USB phone (); // USB = USB phone; // USB. read (); // USB. write (); phone = new phone (); USB = phone; // The interface can also be upgraded. USB. Read (); USB. Write (); WiFi = phone; wifi. open (); wifi. Close ();}}
3. One interface can inherit multiple interfaces.
The Code is as follows:
Interface
interface A{ public void funA();}
Interface B
interface B{ public void funB();}
Interface C inherited interface A and interface B
interface C extends A,B{public void funC();}