Object-oriented--the practical application of interface--setting standards

Source: Internet
Author: User

Object-oriented--the practical application of interface--setting standards Blog Category:
    • Java SE
Flash working Java code
  1. Interface USB
  2. {
  3. public void Start ();
  4. public void Stop ();
  5. }
  6. Class computer
  7. {
  8. 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.
  9. Usb.start ();
  10. System.out.println ("**************working********");
  11. Usb.stop ();
  12. }
  13. }
  14. Class Flash implements USB
  15. {
  16. public void Start () {
  17. System.out.println ("======u disk starts work ===========");
  18. }
  19. public void Stop () {
  20. System.out.println ("======u disc stop Working ===========");
  21. }
  22. }
  23. Class Print implements USB
  24. {
  25. public void Start () {
  26. System.out.println ("====== printer starts working ===========");
  27. }
  28. public void Stop () {
  29. System.out.println ("====== printer stops working ===========");
  30. }
  31. }
  32. Public class Demo5
  33. {
  34. public static void Main (string[] args)
  35. {
  36. Computer.pugin (new Flash ()); This method of invocation, or the first time to see
  37. Computer.pugin (new Print ());
  38. }
  39. }


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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.