没有合适的资源?快使用搜索试试~ 我知道了~
C#之set与get方法的用法案例 c#经典案例.pdf
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 135 浏览量
2022-05-07
22:02:25
上传
评论
收藏 156KB PDF 举报
温馨提示
试读
2页
C#之set与get方法的用法案例 c#经典案例.pdf
资源推荐
资源详情
资源评论
C#之之set与与get⽅法的⽤法案例⽅法的⽤法案例
需求:学⽣输⼊姓名和语⽂、数学、英语,编程求出总分和平均分,并在屏幕上显⽰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();
}
}
}
运⾏结果:
资源评论
_webkit
- 粉丝: 30
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功