Review of C# Basic Concepts

1. Abstract class, abstract method, method overriding

       Abstract method: In object-oriented programming languages, an abstract method refers to a method that has only a method declaration without a specific method body. Abstract methods generally exist in abstract classes or interfaces.

       Method Overriding: A subclass can inherit a method from a parent class without rewriting the same method. But sometimes the subclass does not want to inherit the method of the parent class as it is, but wants to make certain modifications, which requires method rewriting, also known as method overriding.

//In abstract classes and abstract methods, use the abstract modifier

//declare an abstract method, cash abstract class

abstract class CashSuper

{

    //Abstract methods can only be defined in abstract classes

    public abstract double acceptCash(double money);
}


// Inherit the abstract class in the concrete class, rewrite the abstract method, and use the override modifier

//Normal charge subclass inherits abstract class

Class CashNormal:CashSuper

{

    //Override abstract methods in abstract classes

    public overridedouble acceptCash(double money)

    {

    return money;

    }

}

Second, abstract methods and virtual methods

       A virtual method can have an implementation body. If the declaration of an instance method contains the virtual modifier, the method is called a virtual method. After using the virtual modifier, no more static, abstract or override modifiers are allowed.

//Virtual method code in decoration mode:

class Person

{

    //Virtual method in Person class

    public virtual void Show()

    {

        Console.WriteLine("dressed {0},name);

    }

}

//Override the virtual method in the derived apparel class

classFinery:Person

{

    public override void Show()

    {

        If(component !=null)

        {

            componet.Show();

        }

    }

}

The difference between the two:

       An abstract method has only the method name but no method body (that is, there is no specific implementation method), and the subclass must override the method in the parent class; the virtual method has a method body, but the subclass can override it or not.

       Abstract methods generally exist in abstract classes or interfaces, and virtual methods can exist in general classes.

Third, the constructor, overloading

       Constructor, is a special method. It is mainly used to initialize the object when it is created, that is, to assign the initial value to the object member variable, and it is always used together with the new operator in the statement to create the object. Its characteristics: 1. The name of the constructor must be exactly the same as the type; 2. It has no return value and cannot be modified with void; 3. The constructor cannot be called directly, it must be passed through the new operator when the object is worn. Called automatically, while the general method is called when the execution program reaches it.

       Method overloading refers to defining multiple methods with the same name in a class, but requiring each method to have a different parameter type or number of parameters. Method overloading is often used to create methods that accomplish a similar set of tasks but differ in the type or number of parameters. When calling methods, the specific method to use is determined by the number and types of parameters passed to them.

// overloading the constructor

public Person(string name, char gender, int age)

{

       this._name = name;//Indicative pronoun

       this._gender = gender;

       this._age = age;

}

public Person(string name)

{

      this._name = name;

}

public Person()

{

}

// client call

staticvoid Main(string[] args)

{

      Person per=new Person("Zhang San",'male',18);

       Person p = new Person("Zhang San");

}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325962216&siteId=291194637