构造函数

STUDENT类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication3
{
   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;

       }


        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!='女')
                {

                    _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);
        }


    }
}

Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {

            Student stu = new Student("张三",18,'男',100,100,100);
            stu.SayHello();
            stu.ShowScore();

            Student stu1 = new Student("李四", 16, '女', 80, 70, 75);
            stu1.SayHello();
            stu1.ShowScore();
            Console.ReadKey();
        }
    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值