zoukankan      html  css  js  c++  java
  • C# 面向对象5 this关键字和析构函数

    this关键字

     1.代表当前类的对象

     2.在类当中显示的调用本类的构造函数(避免代码的冗余)

    语法: ":this"

    以下一个参数的构造函数调用了参数最全的构造函数!并赋值了那些不需要的属性!

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace _04面向对象练习
    {
        public class Student
        {
            //字段,属性,方法,构造函数
    
            
    
            //构造函数
            public Student(string name,int age,char gender,int chinese,int math,int english)
            {
                //
                this.Name = name;
                this.Age = age;
                this.Gender = gender;
                this.Chinese = chinese;
                this.Math = math;
                this.English = english;
            }
    
            //******
            //this 关键字的用法
            public Student(string name):this(name,0,'a',0,0,0)
            {
                //this.Name = name;
            }
    
            public Student()
            {
                Console.WriteLine("Hello!");
            }
    
            private string _name;
            public string Name
            {
                get { return _name; }
                set { _name = value; }
            }
    
            private int _age;
            public int Age
            {
                get { return _age; }
                set {
                    if (value < 0 || value > 100)
                        value = 0;
    
                    _age = value; }
            }
    
            private char _gender;
            public char Gender
            {
                get {
                    if(_gender !=''&&_gender!='')
                       return  _gender='';
    
                    return _gender; }
                set { _gender = value; }
            }
    
            private int _chinese;
            public int Chinese
            {
                get { return _chinese; }
                set { _chinese = value; }
            }
    
            private int _math;
            public int Math
            {
                get { return _math; }
                set { _math = value; }
            }
    
            private int _english;
            public int English
            {
                get { return _english; }
                set { _english = value; }
            }
    
            public void SayHello()
            {
                Console.WriteLine("我叫{0},我今年{1}岁了,我是{2}生",this.Name,this.Age,this.Gender);
            }
    
            public void ShowScore()
            {
                Console.WriteLine("我叫{0},我的总成绩是{1},平均成绩是{2}", this.Name, this.Chinese + this.Math + this.English, (this.Chinese + this.Math + this.English)/3);
            }
    
        }
    }

    析构函数

     当程序结束的时候,才会执行析构函数.

    作用,不必等GC来回收垃圾,可以调用析构函数马上回收垃圾!!

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace _04面向对象练习
    {
        public class Student
        {
            //字段,属性,方法,构造函数
    
            //析构函数
            //当程序结束的时候,析构函数才会执行
            //帮助我们释放资源
            //GC:垃圾回收器可以自动回收垃圾
            //使用析构函数可以马上回收垃圾,不必等GC来回收!
            ~Student()
            {
                Console.WriteLine("析构函数");
            }
            
    
            //构造函数
            public Student(string name,int age,char gender,int chinese,int math,int english)
            {
                //
                this.Name = name;
                this.Age = age;
                this.Gender = gender;
                this.Chinese = chinese;
                this.Math = math;
                this.English = english;
            }
    
            //******
            //this 关键字的用法
            public Student(string name):this(name,0,'a',0,0,0)
            {
                //this.Name = name;
            }
    
            public Student()
            {
                Console.WriteLine("Hello!");
            }
    
            private string _name;
            public string Name
            {
                get { return _name; }
                set { _name = value; }
            }
    
            private int _age;
            public int Age
            {
                get { return _age; }
                set {
                    if (value < 0 || value > 100)
                        value = 0;
    
                    _age = value; }
            }
    
            private char _gender;
            public char Gender
            {
                get {
                    if(_gender !=''&&_gender!='')
                       return  _gender='';
    
                    return _gender; }
                set { _gender = value; }
            }
    
            private int _chinese;
            public int Chinese
            {
                get { return _chinese; }
                set { _chinese = value; }
            }
    
            private int _math;
            public int Math
            {
                get { return _math; }
                set { _math = value; }
            }
    
            private int _english;
            public int English
            {
                get { return _english; }
                set { _english = value; }
            }
    
            public void SayHello()
            {
                Console.WriteLine("我叫{0},我今年{1}岁了,我是{2}生",this.Name,this.Age,this.Gender);
            }
    
            public void ShowScore()
            {
                Console.WriteLine("我叫{0},我的总成绩是{1},平均成绩是{2}", this.Name, this.Chinese + this.Math + this.English, (this.Chinese + this.Math + this.English)/3);
            }
    
        }
    }

    ***Xmind脑图软件

  • 相关阅读:
    如何禁用Xcode7下iOS9 App传输安全,并修复无法连接服务器错误
    iOS单例清除
    Git忽略规则及.gitignore规则不生效的解决办法
    javascript 在ie8中报“缺少标识符、字符串或数字“问题再现:
    树莓派 远程桌面 设置显示中文
    在IIS Express中调试时无法读取配置文件 错误
    Win7 下IIS(7.5)发布 ASP.NET MVC
    .Net中使用com组件后发生System.ArithmeticException异常的解决办法(Message=算术运算中发生溢出或下溢。)
    CodeSimth
    windows 开机启动 CassiniDev(IIS替代软件)
  • 原文地址:https://www.cnblogs.com/youguess/p/8040185.html
Copyright © 2011-2022 走看看