C# declares the Employee class to implement its constructor (C# after-school exercises - constructor)

Preface

The title of this article is the basic exercises of C#, the constructor part. Before doing these exercises, you need to make sure you have learned about constructors.
This article can be used to deepen your impression after learning the constructor, and can also be used for after-school exercises in college.

topic

Exercise 1

Suppose you are developing a simple employee management system in which there is a Employee class representing employees. Please write a Employee class and implement the following in it:

  1. Attributes (this class has the following attributes):
    Name: Employee name, type string.
    Age: Employee age, type int.

  2. Constructor (the constructor of this class is as follows):

    • Two-parameter constructor: accepts two parameters name and age, which are used to set the employee's name and age respectively. Print out in the constructor: "The two-parameter constructor was called."
    • Single-parameter constructor: accepts a parameter name, which is used to set the employee's name, and calls another constructor Employee(string name, int age). The default age is 0. In the constructor, it prints: "The single-parameter constructor was called. function".
    • No-parameter constructor: Create an employee object with an empty name and an age of 0, call another constructor Employee(string name), and print out in the constructor: "The no-parameter constructor was called."

Please implement the constructor of the Employee class according to the above requirements.

Run the example

The call is shown below and the output is shown in the figure.

Employee emp1=new Employee();
Employee emp2=new Employee("李明");
Employee emp3=new Employee("王五",19);
Console.ReadLine();

Insert image description here

Exercise 2

Suppose you are developing a simple animal management system with an abstract class Animal representing animals. Please write the Animal class and its subclasses according to the following requirements:

  1. AnimalThe class contains the following members:

    • Attribute Name: The name of the animal, type string.
    • Constructor: accepts one parameter name, used to set the name of the animal.
  2. DogClass is a subclass of class Animal and contains the following members:

    • AttributeAge: The age of the dog, type isint.
    • Constructor: Accepts two parameters name and age, representing the dog’s name and age respectively. Set the age in the constructor and print: "The constructor of the Dog class was called", and call the constructor of the parent class to set the name.
  3. PenguinClass is a subclass of class Animal and contains the following members:

    • AttributeGender: The gender of the penguin, type isstring.
    • AttributeAge: The age of the penguin, type: int.
    • Three-parameter constructor: accepts three parameters name, age and gender, which are used to set the penguin's name, age and gender respectively. Set the age and gender in the constructor, and call the parent class constructor to set the name.
    • Two-parameter constructor: receives name and age, calls the three-parameter constructor, and uses an empty string for gender. And prints out: "The constructor of the Penguin class was called."

Please implement the constructors of Animal, Dog and Penguin classes according to the above requirements.

Run the example

The call is shown below and the output is shown in the figure.

Dog dog=new Dog("大黄",1);
Penguin penguin=new Penguin("大黑",2,"雄性");
System.Console.WriteLine($"小狗叫{
      
      dog.Name}{
      
      dog.Age}岁了!");
System.Console.WriteLine($"企鹅叫{
      
      penguin.Name}{
      
      penguin.Age}岁了,是{
      
      penguin.Gender}");
Console.ReadLine();

Insert image description here

Reference answer

Exercise 1

public class Employee
{
    
    
    public int Age {
    
     get; set; }
    public string Name {
    
     get; set; }
    public Employee() : this("")
    {
    
    
        System.Console.WriteLine("调用了无参构造函数");
    }
    public Employee(string name) : this(name, 0)
    {
    
    
        Name = name;
        System.Console.WriteLine("调用了单参构造函数");
    }
    public Employee(string name, int age)
    {
    
    
        Name = name;
        Age = age;
        System.Console.WriteLine("调用了双参构造函数");
    }
}

Exercise 2

public abstract class Animal
{
    
    
    public string Name {
    
     get; set; }
    public Animal(string name)
    {
    
    
        Name = name;
    }
}
public class Dog : Animal
{
    
    
    public int Age {
    
     get; set; }
    public Dog(string name,int age):base(name)
    {
    
    
        Age=age;
        System.Console.WriteLine("调用了 Dog 类的构造函数");
    }

}
public class Penguin:Animal
{
    
    
    public int Age{
    
    get;set;}
    public string Gender{
    
    get;set;}

    public Penguin(string name, int age) : this(name, age, "")
    {
    
    
        Console.WriteLine("调用了 Penguin 类的构造函数");
    }

    public Penguin(string name, int age, string gender) : base(name)
    {
    
    
        Age = age;
        Gender = gender;
    }
}

Other articles

Unity implements mobile game control joystick
Godot implements flashing effect

Guess you like

Origin blog.csdn.net/weixin_44499065/article/details/133690514