C Flexible Use of Class Methods

  • 2021-12-05 07:01:14
  • OfStack

Constructor

Summary: The constructor is a special method in the class, which mainly completes the initialization of the object and completes the specified work when creating the object. Moreover, the constructor method name is the same as the class name, and there is no return value type.

Parametric constructor

By default, a class is assigned a parameterless constructor with no method body. However, we can also customize a parameterless constructor to automatically give a default value to the property when creating the object.


class Demo
 {
 public string DemoName { get; set; }
 public Demo() // Create a parameterless constructor 
 {
 this.DemoName = " Parametric constructor "; // Write the properties that need to be initialized when creating the object in the method body 
 }
 }

Tip: The shortcut to create a parameterless constructor in Visual Studio is' ctor '+ two tab keys; The shortcut to create a property is' prop '+ two tab keys.

Parametric constructor

Sometimes we need to specify 1 value for the properties of the object when we create the object, and these values are 1 fixed, so we need a constructor with arguments.


class Demo
 {
 public string DemoName { get; set; }
 public Demo(string DemoName) // Create a constructor with parameters 
 {
 this.DemoName = DemoName;
 }
 }
 class Test
 {
 Demo demo = new Demo(" Parametric constructor "); // Specify values in parentheses when creating objects 
 }

There are several other matters to pay attention to:

1. The arguments given in parentheses when creating an object must be the same as the argument list of the argument constructor.

2. When you have a parameterized constructor in your class, you must give the corresponding value in parentheses when creating an object. Because the 1-denier class has a constructor in C #, the constructor is no longer automatically assigned. (It is recommended to create a parameterless constructor after creating a parameterized constructor. )

Additional:

There is another quick way to initialize properties, that is, object initializers.

Usage: Demo demo = new Demo () {Name = "object initializer"};

Or: Demo demo = new Demo {Name = "Object Initializer"};

Method overload

Summary: In the same thing, different operations are performed according to different parameters, that is, method overloading.

Features: Each method has the same name, different parameter list and is in the same class.

Note: Different parameter list means that each method has different parameter types, different parameter order or different parameter number.

Only different return values do not constitute a method overload.

Example:


 class Demo
 {
 public string DemoName { get; set; }
 /********* Constructor overload ********/
 public Demo()
 {
 this.DemoName = " Parametric constructor ";
 }
 public Demo(string demoName)
 {
 this.DemoName = demoName;
 }
 /********** Method overload **********/
 public void SayHello(string name)
 {
 Console.WriteLine(" Hello, I'm {0} , Glad to meet you. ", name);
 }
 public void SayHello(string name, int age)
 {
 Console.WriteLine(" Hello, I'm {0} , this year {1} Nice to meet you. ", name, age);
 }
 }
 class Test
 {
 /********* Constructor overload ********/
 Demo demo = new Demo();
 Demo demo1 = new Demo(" Parametric constructor ");
 /********** Method overload **********/
 public void Test()
 {
 demo.SayHello(" Xiao Ming ");
 demo.SayHello(" Xiao Ming ", 18);
 }
 }

Related articles: