C # - Abstract class (reproduced)

Defining an abstract class and keyword abstract abstract methods in C #.

Not initialize the class is called abstract classes that provide only a partial implementation, but another class can inherit it, and they can create
an instance.

"Containing one or more pure virtual functions is called an abstract class, an abstract class that can not be instantiated, further
an abstract class only through the interface and the other as the base class." - C ++ Programming Language by Stroustrup Chapter13.2


an abstract class can be used for classes, methods, properties, events, and indexers, the tendency to use the abstract class as the other represents a base class in a class declaration
members are marked as abstract, or contained in an abstract class, must It is to achieve its derived classes.


Copy the code
abstract class ShapesClass
     {
        abstract public int Area();
    }
    class Square : ShapesClass
    {
        int x, y;
        // Not providing an Area method results
        // in a compile-time error.
        public override int Area()
        {
            return x * y;
        }
    }
Copy the code


For example, a non-abstract class contains abstract methods:

Copy the code

    abstract class MyAbs
     {
        public void NonAbMethod()
        {
            Console.WriteLine("Non-Abstract Method");
        }
    }
    class MyClass : MyAbs
    {
    }
    class MyClient
    {
        public static void Main()
        {
            //MyAbs mb = new MyAbs();//not possible to create an instance
            MyClass mc = new MyClass();
            mc.NonAbMethod(); // Displays 'Non-Abstract Method'
        }
    }
Copy the code



An abstract class may contain abstract and non-abstract method, when a class inherits from the abstract class, then the derived class must implement all of
the abstract base class methods.

An abstract method is not a method of a method body.

Copy the code
 abstract class MyAbs
     {
        public void NonAbMethod()
        {
            Console.WriteLine("Non-Abstract Method");
        }
        public abstract void AbMethod(); // An abstract method
    }
    class MyClass : MyAbs//must implement base class abstract methods
    {
        public override void AbMethod()
        {
            Console.WriteLine("Abstarct method");
        }
    }
    class MyClient
    {
        public static void Main()
        {
            MyClass mc = new MyClass();
            mc.NonAbMethod();
            mc.AbMethod();
        }
    }
Copy the code



But by declaring the derived class is also abstract, we can avoid the realization of all or specific virtual method,
which is part of the abstract class.


Copy the code
abstract class MyAbs
     {
        public abstract void AbMethod1();
        public abstract void AbMethod2();
    }
    //not necessary to implement all abstract methods
    //partial implementation is possible
    abstract class MyClass1 : MyAbs
    {
        public override void AbMethod1()
        {
            Console.WriteLine("Abstarct method #1");
        }
    }
    class MyClass : MyClass1
    {
        public override void AbMethod2()
        {
            Console.WriteLine("Abstarct method #2");
        }
    }
    class MyClient
    {
        public static void Main()
        {
            MyClass mc = new MyClass();
            mc.AbMethod1();
            mc.AbMethod2();
        }
    }
Copy the code


In C #, a non-abstract class inherits from another abstract class can be, in addition, the method inherits the base class, adding new
abstract and non-abstract methods are possible.


Copy the code
 class MyClass1 // Non-Abstract class
     {
        public void Method1()
        {
            Console.WriteLine("Method of a non-abstract class");
        }
    }
    abstract class MyAbs : MyClass1 // Inherits from an non-abstract class
    {
        public abstract void AbMethod1();
    }
    class MyClass : MyAbs//must implement base class abstract methods
    {
        public override void AbMethod1()
        {
            Console.WriteLine("Abstarct method #1 of MyClass");
        }
    }
    class MyClient
    {
        public static void Main()
        {
            MyClass mc = new MyClass();
            mc.Method1();
            mc.AbMethod1();
        }
    }
Copy the code


An abstract class is also an interface to achieve from this situation, we must provide a method body for all methods, which are from the interface

Copy the code
    interface IInterface
     {
        void Method1();
    }
    abstract class MyAbs : IInterface
    {
        public void Method1()
        {
            Console.WriteLine("Method implemented from the IInterface");
        }
    }
    class MyClass : MyAbs//must implement base class abstract method
    {
    }
    class MyClient
    {
        public static void Main()
        {
        MyClass mc = new MyClass();
        mc.Method1();
        }
    } 
Copy the code

 We can not use keywords abstract and sealed together in C #, because a sealed class can not be abstract .

Copy the code
 abstract class MyAbs
     {
        public abstract void AbMethod1();
        public abstract void AbMethod2();
    }
    class MyClass1 : MyAbs
    {
        public override void AbMethod1()
        {
            Console.WriteLine("Abstarct method #1 of MyClass1");
        }
        public override void AbMethod2()
        {
            Console.WriteLine("Abstarct method #2 of MyClass1");
        }
    }
    class MyClient
    {
        public static void Main()
        {
            MyAbs ma1 = new MyClass1();// Polymorphism
            ma1.AbMethod1();
            ma1.AbMethod2();
        }
    }
Copy the code

 

Abstract method has the following characteristics:

1 can be seen as an abstract method a virtual function.

2. declare abstract method only in an abstract class.

3. Because an abstract method declaration provides only way to achieve a free, no method body

4. The method of realization thereof is provided a method override, override method is a non-member of the abstract class.

The abstract behavior and abstract properties similar method, except that different declaration forms.

6. Use a mistake in the abstract is a static property.

        * An abstract properties can be achieved by using override a derived class.

For abstract classes:

an abstract class must provide an implementation for all interface members
that implements the abstract class interface may arrange the interface methods onto abstract methods. E.g

Copy the code
interface I 
{
    void M();
}
abstract class C: I 
{
    public abstract void M();
}
Copy the code


Abstract class has the following characteristics:

1. An abstract class can not be instantiated.

2. An abstract class may contain abstract methods and accessors

3. An abstract class can not seal (sealed) to modify, it means that the class can not be inherited, in violation of the principles of abstract class is inherited.

4. A derived from an abstract class of the abstract methods to achieve non-abstract class and must include all inherited accessor

5. abstract keyword means comprising in their implementation methods and attributes.

Reproduced in: https://www.cnblogs.com/flyinthesky/archive/2008/06/18/1224774.html

Guess you like

Origin www.cnblogs.com/mexihq/p/12572128.html