page contents

C#之set与get方法的用法案例

本文讲述了C#之set与get方法的用法案例!具有很好的参考价值,希望对大家有所帮助。一起跟随六星小编过来看看吧,具体如下:

attachments-2022-05-Z9qkjH1F628ee649d299d.png

本文讲述了C#之set与get方法的用法案例!具有很好的参考价值,希望对大家有所帮助。一起跟随六星小编过来看看吧,具体如下:

需求:学生输入姓名和语文、数学、英语,编程求出总分和平均分,并在屏幕上显示XX的总分和平均分

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//学生输入姓名和语文、数学、英语,编程求出总分和平均分,并在屏幕上显示XX的总分和平均分
namespace Student_management_system
{
    class Student
    {
        private String name;   //学生姓名
        private int chinese;  //语文成绩
        private int math; //数学成绩
        private int english;  //英语成绩
        public String student_name   //这个不是一个方法,它是一个变量,当对象调用该变量时,就要给这个对象的name属性赋值,或者获取该变量的值
        {
           set{   //直接在里面定义set方法,这样对象就可以通过这样调用来赋值了,如 Student s;s.student_name="唐僧";
            this.name=value;
            }
           get{   //定义get方法,对象可以这样获取get方法里面返回来的name值,如s.student_name;
            return name;
            }
        }
        public int student_chinese
        {
            set
            {
                this.chinese = value;
            }
            get
            {
                return this.chinese;
            }
        }
        public int student_math
        {
            set
            {
                this.math = value;
            }
            get
            {
                return this.math;
            }
        }
        public int student_english
        {
            set
            {
                this.english = value;
            }
            get
            {
                return this.english;
  
            }
        }
        public Student(String name, int chinese, int math, int english)
        {
            this.name = name;
            this.chinese = chinese;
            this.math = math;
            this.english = english;
        }
        public int sum()  //求总分
        {
            int sum = this.chinese + this.english + this.math;
            
            return sum;
        }
        public float average()   //求平均分
        {
            float avg = sum() / 3;
            return avg;
        }
        static void Main(string[] args)
        {
            Student s = new Student();
            Console.WriteLine("请输入学生姓名");
            s.student_name = Console.ReadLine();
            Console.WriteLine("请输入学生科目成绩:");
            s.student_chinese =Convert.ToInt32(Console.ReadLine());
            s.student_english = Convert.ToInt32(Console.ReadLine());
            s.student_math = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine(s.name + "的语文是" + s.student_chinese + "分,数学是" + s.student_math + "分,英语是" + s.student_english + "分,总分:" + s.sum()+",平均分:" + s.average());
            s.student_chinese = 69;
            s.student_math = 100;
            Console.WriteLine("修改分数后-->" + s.name + "的语文是" + s.student_chinese + "分,数学是" + s.student_math + "分,英语是" + s.student_english + "分,总分:" + s.sum() + ",平均分:" + s.average());
            //加上这句话,否则一运行就会闪退,即刚出现命令窗口就会马上消失
            Console.ReadLine();
        }
    }
}

运行结果:

到此这篇关于C#之set与get方法的用法案例的文章就介绍到这了,更多相关技术内容咨询欢迎前往并持续关注六星社区了解详情。

想高效系统的学习Python编程语言,推荐大家关注一个公众号:Python编程学习圈。每天分享行业资讯、技术干货供大家阅读,关注即可免费领取整套Python入门到进阶的学习资料以及教程,感兴趣的小伙伴赶紧行动起来吧。

attachments-2022-05-rLS4AIF8628ee5f3b7e12.jpg

  • 发表于 2022-05-26 10:31
  • 阅读 ( 336 )
  • 分类:C/C++开发

你可能感兴趣的文章

相关问题

0 条评论

请先 登录 后评论
轩辕小不懂
轩辕小不懂

2403 篇文章

作家榜 »

  1. 轩辕小不懂 2403 文章
  2. 小柒 1312 文章
  3. Pack 1135 文章
  4. Nen 576 文章
  5. 王昭君 209 文章
  6. 文双 71 文章
  7. 小威 64 文章
  8. Cara 36 文章