Initialization method of C# base class
Today, the editor will share with you the relevant knowledge points about the initialization method of the C# base class. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article, let's take a look at it.
Initialization of base class
The derived class inherits the member variables and member methods of the base class. Therefore, the parent object should be created before the subclass object is created. You can initialize the parent class in the member initialization list.
The following program demonstrates this:
Using System
Namespace RectangleApplication
{
Class Rectangle
{
/ / member variable
Protected double length
Protected double width
Public Rectangle (double l, double w)
{
Length = l
Width = w
}
Public double GetArea ()
{
Return length * width
}
Public void Display ()
{
Console.WriteLine ("length: {0}", length)
Console.WriteLine ("width: {0}", width)
Console.WriteLine ("area: {0}", GetArea ())
}
} / / end class Rectangle
Class Tabletop: Rectangle
{
Private double cost
Public Tabletop (double l, double w): base (l, w)
{}
Public double GetCost ()
{
Double cost
Cost = GetArea () * 70
Return cost
}
Public void Display ()
{
Base.Display ()
Console.WriteLine ("cost: {0}", GetCost ())
}
}
Class ExecuteRectangle
{
Static void Main (string [] args)
{
Tabletop t = new Tabletop (4.5,7.5)
T.Display ()
Console.ReadLine ()
}
}
}
When the above code is compiled and executed, it produces the following results:
Length: 4.5breadth: 7.5area: 33.75cost: more than 2362.5 is all the content of this article "initialization method of C# Base Class". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.