Abstract Class Designer in C #

In C # we cannot create an obeject of an abstact class or an interface, this means that the abstract class has no constructor, is this true? or if he has what it is?

+3
source share
5 answers
Good question. This is why abstract classes need constructors, even if they cannot be created.

In any object-oriented language, such as C #, building an object is a hierarchical process. Take a look at the code below. When you instantiate any object of type DerivedClass, it must first create a base object before creating an object of type DerivedClass. Here, the base class may or may not be an abstract class. But even when creating an object of a specific type obtained from an abstract class, you still need to call the constructor of the base class before creating an object of type DerivedClass, so you always need a constructor for the abstract class. If you have not added any constructor, the C # compiler will automatically add an open constructor without parameters to the class in the generated MSIL.

public class BaseClass
{
    public BaseClass() 
    {
        Console.WriteLine("BaseClass constructor called..");
    }
}

public class DerivedClass : BaseClass
{
    public DerivedClass()
    {
        Console.WriteLine("DerivedClass constructor called..");
    }
}

DerivedClass obj = new DerivedClass();

//Output
//BaseClass constructor called..
//DerivedClass constructor called..

PS: , , . , , , , .

+6

, ( , , ), . ... , , , , , Name . , ... , , . , .

:

public abstract class DemoBase
{
    private readonly string name;    
    public string Name { get { return name; } }

    protected DemoBase(string name)
    {
        this.name = name;
    }

    // Abstract members here, probably
}

public class FixedNameDemo : DemoBase
{
    public FixedNameDemo()
        : base ("Always the same name")
    {
    }

    // Other stuff here
}

public class VariableNameDemo : DemoBase
{
    public VariableNameDemo(string name)
        : base(name)
    {
    }

    // Other stuff here
}

BoltClock, asbtract , . , , , . -:

public abstract class ArithmeticOperator
{
    public static readonly ArithmeticOperator Plus = new PlusOperator();
    public static readonly ArithmeticOperator Minus = new MinusOperator();

    public abstract int Apply(int x, int y);

    private ArithmeticOperator() {}

    private class PlusOperator : ArithmeticOperator
    {
        public override int Apply(int x, int y)
        {
            return x + y;
        }
    }

    private class MinusOperator : ArithmeticOperator
    {
        public override int Apply(int x, int y)
        {
            return x - y;
        }
    }
}

/ - , . . protected , .

+12

. , new .

/ .

.

, , . , . .

new

:

?

, , . - , [className()]. - , , . , , . , , .

+4

, , .

, , private - , abstract . , .

: . . .

+2

Abstract classes have constructors. When you instantiate a derived class, its parent class constructors are called. This also applies to classes derived from abstract classes.

+1
source

Source: https://habr.com/ru/post/1769476/


All Articles