C#_构造函数 new this 析构函数

internal class Program
{
    static void Main(string[] args)
    {
        //构造函数
        //作用:给对象的每个属性依次赋值
        //注意:构造函数没有返回值,连void都不能写,构造函数名称必须跟类名一样

        //new的作用
        //1.在内存中帮我们开辟了一空间
        //2.在开辟空间中创建对象
        //3.调用对象的构造函数进行初始化对象

        //this的作用
        //代表当前类的对象,在类中显示的调用本类的构造函数

        //析构函数
        //当程序结束的时候才被调用
        //作用:析构函数是用来释放资源的
        Person b = new Person("张三", 22, '男', 89, 100, 87);
        Person p = new Person("张三",100,89,67);
        p.MyGrade();
        Console.ReadKey();

    }
    
}
 public class Person
 {
     //字段
     public string _name;
     private int _age;
     private char _gender;
     public int _language;
     public int _mathematics;
     public int _english;

     //属性
     public string Name { get; set; }
     public int Age
     {
         get { return _age; }
         set
         {
             if (value > 100 && value < 0)
             {
                 value = 0;
             }
             _age = value;
         }
     }
     public char Gender
     {
         get
         {
             if (_gender != '男' && _gender != '女')
             {
                 return _gender = '男';
             }
             return _gender;
         }
         set { }
     }
     public int Language { get; set; }
     public int Mathematics { get; set; }
     public int English { get; set; }

     //方法
     public void MyGrade()
     {
         Console.WriteLine("我的名字叫{0},我的年龄是{1}岁,我的性别是{2}生,我的语文成绩是{3},我的数学成绩是{4},我的英语成绩是{5},我的平均成绩是{6}",
             this.Name,
             this.Age,
             this.Gender,
             this.Language,
             this.Mathematics,
             this.English,
             (this.Language + this.Mathematics + this.English) / 3);
     }

     //构造函数
     public Person(string name, int age, char gender, int language, int mathematics, int english)
     {
         this.Name = name;
         this.Age = age;
         this.Gender = gender;
         this.Language = language;
         this.Mathematics = mathematics;
         this.English = english;
     }
     //重载
     public Person(string name, int language, int mathematics, int english) : this(name, 0, 'c', language, mathematics, english)
     {

     }

     //析构函数
     ~Person()
     {
         Console.WriteLine("我是析构函数");
     }
 }

相关推荐

  1. C++ 类构造函数 & 函数

    2024-06-16 05:44:02       40 阅读
  2. C++ 类构造函数 & 函数

    2024-06-16 05:44:02       18 阅读
  3. C++入门【36-C++ 类构造函数 & 函数

    2024-06-16 05:44:02       31 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-06-16 05:44:02       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-06-16 05:44:02       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-06-16 05:44:02       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-06-16 05:44:02       18 阅读

热门阅读

  1. C++:特殊类

    2024-06-16 05:44:02       5 阅读
  2. c++_0基础_讲解3 输入 输出

    2024-06-16 05:44:02       6 阅读
  3. vuex是什么?如何使用?使用他的功能场景?

    2024-06-16 05:44:02       8 阅读
  4. perl语言入门学习

    2024-06-16 05:44:02       6 阅读
  5. 新增套餐——后端

    2024-06-16 05:44:02       6 阅读
  6. 向mysql发送一个请求的时候,mysql到底做了什么

    2024-06-16 05:44:02       8 阅读
  7. 什么是 Linux ?(Linux)

    2024-06-16 05:44:02       7 阅读