zoukankan      html  css  js  c++  java
  • 构造函数

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace _Test
    {
        class Program
        {
            static void Main(string[] args)
            {
    
                //Student s = new Student("张三", 100, 100, 100);
                
                //Student zsStudent = new Student("张三", 18, '男', 100, 100, 100);
    
                //zsStudent.SayHello();
                //zsStudent.ShowScore();
                //Console.ReadKey();
    
               // Student xlStudent = new Student("小兰",78,79,89);
                Student xlStudent = new Student("小兰",19,'女');
    
                xlStudent.SayHello();
                xlStudent.ShowScore();
                Console.ReadKey();
    
    
    
            }
        }
    
    
        public class Student
        {
            //字段、属性、方法、构造函数
    
            //析构函数  构造函数
    
    
    
            //当程序结束的时候  析构函数才执行
            //帮助我们释放资源
            //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;
            }
    
            public Student(string name, int chinese, int math, int english) : this(name, 0, 'c', chinese, math, english)
            {
                //使用this后下面代码可省了。
                //this.Name = name;
                //this.Chinese = chinese;
                //this.Math = math;
                //this.English = english;
            }
    
            //构造函数重载
            public Student(string name, int age, char gender)
            {
                this.Name = name;
                if (age < 0 || age > 100)
                {
                    age = 0;
                }
                this.Age = age;
                this.Gender = gender;
            }
    
            //当我们不主动使用构造函数时,系统会自动生成如下构造函数
            public Student()
            {
    
            }
    
    
            //私有字段
            private string _name;
            //共有属性
            public string Name
            {
                //get控制是否可读
                get { return _name; }
                //set控制是否可写
                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);
            }
    
        }
    }
    
  • 相关阅读:
    编辑推荐
    mybatis
    学习网址记录
    关于详情页的具体制作(四)
    关于详情页的具体制作(三)
    关于详情页的具体制作(二)
    关于详情页的具体制作(一)
    关于事件循环的一些总结
    vue生命周期的一些总结
    对于home主页的切换处理
  • 原文地址:https://www.cnblogs.com/hao-1234-1234/p/6119604.html
Copyright © 2011-2022 走看看