Abstract classes and interfaces

Reference article: http: //www.cftea.com/c/2012/03/5522.asp; http: //blog.sina.com.cn/s/blog_4aaeba510100c4y6.html
an abstract method: Benefits: Flexible implementation code reuse, a subclass overrides (override); abstract purpose that I can not achieve it by inheriting my class to achieve
1, a, abstract class method must be abstract methods, or error;
     b, the properties do not have to abstract property.
2, a, a class can inherit an abstract class;
     B, abstract and private, virtual, static can not be used simultaneously;
     C, can not be instantiated abstract class.
3, an abstract method of the abstract class, an abstract property to be rewritten generally subclasses (to be consistent accessibility override), and abstract properties of the read and write cases to be consistent.
4, an abstract method of the abstract class does not have to override the abstract properties abstract subclasses, but must be rewritten to implement, is not implemented in the rewriting must be overridden in a subclass.
5, sub-class inherits the abstract class can override inherited abstract class inherits the method properties again.
6, not abstract field (because the field can only be assigned by an equal sign, no other code (different from the property), nothing abstract sense).
example:

public  abstract  class PhoneAbsstract // abstract class method must be abstract methods, or error; property does not have to abstract properties. 
    {
         Public  abstract  void call ();
         public  abstract  void camera ();
         public  abstract  void Internet ();

        private int _size = 0;
        public int size
        {
            get { return _size; }
            set { _size = size; }
        }

        public abstract int weight { get;}
    }

    public  class Phone1: PhoneAbsstract // abstract method abstract class must override the abstract properties (rewrite accessibility to be consistent) in its general subclasses, and read the abstract properties, write to the same situation. 
    {
         Public  the override  void call ()
        {
            Console.Write ( " I can call. " );
        }
        public  the override  void camera ()
        {
            Console.Write ( " I can play photo. " );
        }
        public  the override  void Internet ()
        {
            Console.Write ( " I can access. " );
        }

        private int _weight = 0;
        public override int weight {
            get { return _weight; }
        }
    }

    public  abstract  class IPhoneAbstract: PhoneAbsstract // abstract method of the abstract class, an abstract property is not always to be rewritten in the abstract subclasses, but must be rewritten to implement 
    {
         public  the override  void call ()
        {
            Console.Write ( " the IPhone mobile phone can call. " );
        }
        public  the override  void Internet () // subclass inherits the abstract class can override inherited abstract class inherits the method properties again. 
        {
            Console.Write ( " the IPhone mobile phone access to the Internet. " );
        }
        private int _weight = 0;
        public override int weight
        {
            get { return _weight; }
        }    
    }

    public  class iphone1: IPhoneAbstract // not implemented in a rewritable must be overridden in a subclass 
    {
         public  the override  void Internet ()
        {
            Console.Write ( " the IPhone mobile phone access to the Internet. " );
        }
        public  the override  void camera ()
        {
            Console.Write ( " the IPhone camera phone can. " );
        }
    }
View Code

Second, the interface: multi-state; the purpose is to provide a standard interface for people to comply with, we do not know how to achieve; Simple factory pattern applications (using multi-state);
1, the interface can only be public, that implements the interface must also be public;
2, can implement multiple interfaces, but all methods inherited interfaces must be achieved;
example: a simple factory pattern

class Program
    {
        static void Main(string[] args)
        {
            phone2 p2=new phone2();
            camera c = new camera();
            . SimplenessFactory camera (P2);
            . SimplenessFactory camera (C);
            Console.Read();
        }
    }
    public  class SimplenessFactory // simple engineering model application interface 
    {
         public  static  void camera (photographing device interface Photo)
        {
            . Photo camera ();
        }
    }

public  interface call interface // interfaces only public 
    {
         void call ();
    }

    public  interface photographic device interface
    {
        void photographic ();
         int size { GET ;}
    }

    public  interface Internet Device Interface
    {
        void Internet ();
    }

    public  class phone1: call interface // interface implementation class must be public 
    {
         public  void call () // must be public 
        {
            Console.WriteLine ( " phone 1 phone calls. " );
        }
    }

    public  class phone2: call device interface, camera interface, Internet interface // can implement multiple interfaces, but all methods inherited interfaces must be realized 
    {
         public  void call ()
        {
            Console.WriteLine ( " Phone 2 phone calls. " );
        }

        public  void camera ()
        {
            Console.WriteLine ( " Phone 2 photo shoot. " );
        }

        public  void Internet ()
        {
            Console.WriteLine ( " phone access to the Internet 2. " );
        }

        public int size {
            get { return 100; }
        } // attribute must also realize, it must be public 
    }

    public  class Camera: camera Device Interface
    {
        public  void camera ()
        {
            Console.WriteLine ( " camera photo shoot. " );
        }
        public int size
        {
            get { return 200; }
        } // attribute must also realize, it must be public 
    }
View Code

 

Third, the difference between abstract classes and interfaces:
1, a class may be a plurality of interfaces, but only implement an abstract class.
2, the specific methods of the abstract class implements virtual default, but the interface of the interface class method implementation has a default non-virtual, of course, you can declare virtual
3, (Interface) with similar non-abstract class, an abstract class must also provide its own implementation for all members of the interfaces listed in the base class list of this class. However, the abstract class interface method allowing  
      mapped to the abstract methods.
4, good interface definition should be a specific functional, rather than a multi-functional, interfaces or cause pollution.
Fourth, the use of abstract classes and interfaces:

1. If you expect to create multiple versions of a component, create an abstract class. Abstract class provides a simple way to control the component version.

2. If the function was created to be used in a wide range between disparate objects, use the interface. If you want to design small and concise function block, use the interface.

3. If the functional unit to be designed large, an abstract class is used. To provide common among all components to achieve functionality has been achieved, using an abstract class.

4. main abstract class for objects close; and the interface is adapted to provide generic functions unrelated to the class.

Here is what I see on the Internet a few metaphor, really good, huh:

1. The aircraft can fly, birds fly, they have inherited the same interface "fly"; but F22 aircraft belonging to an abstract class, abstract class pigeons belong to the birds.

2. Like iron gate doors are doors (abstract class), you want a door I can not give (can not be instantiated), but I can give you a specific gates or doors (multi-state); and can only be door, you can not say that it is a window (single inheritance); a door can be locked (interfaces) can also doorbell (more than realized). Door (abstract class) defines what you are, an interface (lock) sets out what you can do (the best one interface can only do one thing, you can not ask locks can sound bar (Interface pollution).

Guess you like

Origin www.cnblogs.com/Hyman-Zheng/p/11076413.html