luni, 25 august 2014

Abstract C#

Abstract  indicates that a class is intended only to be a base class of other classes.
Abstract classes features:
  • cannot be instantiated.
  • may contain abstract methods and accessors.
  • it is not possible to modify an abstract class with the sealed modifier
  • class derived from an abstract class must include actual implementations of all inherited abstract methods and accessors.
Example:

    public abstract class Shape
    {
        public abstract int Area();
    }
    public class Rectangle : Shape 
    {
        int x = 0;
        int y = 0;
        public Rectangle(int _x, int _y)
        {
            x = _x;
            y = _y;
        }
        public override int Area()
        {
            return x * y;
        }
    }

    Rectangle rct = new Rectangle(10, 20);
    int area = rct.Area();

Niciun comentariu:

Trimiteți un comentariu