C#之set与get方法的用法案例
作者:悬弧 时间:2021-08-09 01:17:18
需求:学生输入姓名和语文、数学、英语,编程求出总分和平均分,并在屏幕上显示XX的总分和平均分
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();
}
}
}
运行结果:
来源:https://blog.csdn.net/huangxuanheng/article/details/46649309
标签:C#,set,get
0
投稿
猜你喜欢
Java中实现在一个方法中调用另一个方法
2022-05-15 10:02:11
关于RedisTemplate之opsForValue的使用说明
2023-07-09 16:53:04
详解SpringBoot定时任务说明
2022-08-11 05:03:11
c#获取相同概率随机数的算法代码
2022-09-07 21:18:13
浅谈spring中scope作用域
2022-08-18 10:42:53
使用Logback日志保存到相对路径的操作
2021-10-11 16:42:30
mybatis-plus3.0.1枚举返回为null解决办法
2023-11-07 16:59:50
SpringBoot整合mybatis-plus进阶详细教程
2023-11-27 05:13:12
Java try()语句实现try-with-resources异常管理机制操作
2022-02-06 18:29:16
IDEA 2020.3最新永久激活码(免费激活到 2099 年,亲测有效)
2023-07-14 05:37:43
详解C#对路径...的访问被拒绝解决过程
2021-06-01 11:12:10
IDEA部署JavaWeb项目到Tomcat服务器的方法
2023-11-02 23:21:22
Java 设计模式中的命令模式详情
2023-11-15 23:25:33
使用Spring Boot AOP处理方法的入参和返回值
2022-03-02 07:25:41
springboot多环境配置文件及自定义配置文件路径详解
2021-09-30 03:55:54
详解安装Eclipse Maven插件的几种方法
2022-06-01 06:15:20
详解SpringBoot项目整合Vue做一个完整的用户注册功能
2022-02-13 21:46:35
为IObservable实现自己的运算符(详解)
2022-04-02 08:58:15
java GUI编程之paint绘制操作示例
2023-11-24 17:58:39
Spring MVC请求参数的深入解析
2021-11-26 22:55:25