Use of the purpose and interface of the interface

Source: Internet
Author: User

1. Why interfaces are required:

Software development process, a project can not be done purely by one person, then it must be accurately divided into several parts of the software, will be divided into a number of parts to different people to do, and the interface is equivalent to the connection between the slot, first by the designer to define all the interface name, interface parameters, return data type, and explain the role of the interface, the implementation of the interface is responsible for the function of the interface, and the front desk responsible for the design of the UI as long as the user is responsible for the collected data, call interface implementation of the data processing, conversion, storage on the line. For example, when we call the String.Length method, we do not care about how the code is done, only about its return value, which gives some of the common functions to the predecessors for us to do it, we just need to invoke their methods (interface).

2. How to define an interface:

Interface IControl {void Paint ();}

3. How to implement the interface:

An interface can be implemented by multiple classes, manifesting polymorphism.

public class control1:icontrol{public          void Paint () {            implementation code;        }     } public  class control2:icontrol{ Public          void Paint () {            implementation code;        }     

4. Defining the class factory

Interfaces cannot be instantiated, so a class factory is required to get the instantiated object corresponding to the interface

public static factory1{public     static IControl GetProgID (String ProgID)     {          if (progid== "1")              return new Control1 ();          else if (progid== "2")              return new Control2 ();          else              return null;    }         }    

  5. How to invoke an interface

IControl Ic=factroy1.getprogid ("1");//Use the class factory to instantiate the interface IC. Paint ();//Use interface implementation method

  

Use of the purpose and interface of the interface

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.