Object-oriented--the practical application of interface--setting standards
Blog Category:
Flash working Java code
- Interface USB
- {
- public void Start ();
- public void Stop ();
- }
- Class computer
- {
- public static void Pugin (USB USB) {//Here is just a reference variable for an interface object, equivalent to a formal parameter, an interface cannot create an instance object with love new, but can declare an object reference variable.
- Usb.start ();
- System.out.println ("**************working********");
- Usb.stop ();
- }
- }
- Class Flash implements USB
- {
- public void Start () {
- System.out.println ("======u disk starts work ===========");
- }
- public void Stop () {
- System.out.println ("======u disc stop Working ===========");
- }
- }
- Class Print implements USB
- {
- public void Start () {
- System.out.println ("====== printer starts working ===========");
- }
- public void Stop () {
- System.out.println ("====== printer stops working ===========");
- }
- }
- Public class Demo5
- {
- public static void Main (string[] args)
- {
- Computer.pugin (new Flash ()); This method of invocation, or the first time to see
- Computer.pugin (new Print ());
- }
- }
This two-day learning interface, abstract class, object polymorphism, feeling very laborious, mainly polymorphic place, feel not much necessary, learned here to understand the importance of polymorphism, he is used to instantiate the interface and abstract class.
In this example, USB is a standard that only meets this standard in order to connect to the computer via USB, and the computer only recognize USB. As long as you meet this interface, you can use it regardless of what device you are.
So you can think of that, interface interface he developed a standard for connecting parts of the interface (standard).
And, compared to abstract classes, interfaces can inherit more.
Object-oriented--the practical application of interface--setting standards