112,689
社区成员
发帖
与我相关
我的任务
分享c# 编写一- 个学生类(Student) , 包含字段、属性、方法、构造函数、析构函数、索引器等成员,实现输入姓名、学号、5门考试成绩,输出考试总成绩和平均成绩
以下是一个C#中的学生类(Student),包含字段、属性、方法、构造函数和析构函数的示例。请注意,这是一个基本的示例,你可以根据自己的需求进行修改和扩展。
using System;
public class Student
{
// 字段
private string name;
private int studentId;
private int[] examScores;
// 属性
public string Name
{
get => name;
set => name = value;
}
public int StudentId
{
get => studentId;
set => studentId = value;
}
public int[] ExamScores
{
get => examScores;
set => examScores = value;
}
// 构造函数
public Student(string name, int studentId, int[] examScores)
{
this.name = name;
this.studentId = studentId;
this.examScores = examScores;
}
// 方法
public int CalculateTotalScore()
{
int total = 0;
for (int i = 0; i < examScores.Length; i++)
{
total += examScores[i];
}
return total;
}
public double CalculateAverageScore()
{
int total = CalculateTotalScore();
return (double)total / examScores.Length;
}
// 析构函数
~Student()
{
Console.WriteLine($"Student {name} ({studentId}) has been disposed.");
}
}
这个类包含了一个学生的姓名(name)、学号(studentId)和5门考试的分数(examScores)。它还有一些方法可以计算学生的总分数和平均分数。同时,包含一个析构函数,当对象被垃圾回收时,会打印出一个消息。需要注意的是,析构函数在.NET中并不常用,通常用于释放非托管资源,对于大多数的.NET应用程序,垃圾回收机制可以自动处理内存回收,不需要显式编写析构函数。
```c
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace
{
public class Person
{
public string id;
public string name;
public string ID
{
get{ return id;}
set { id = value; }
}
public string Name
{
get { return name; }
set { name = value; }
}
public void InPreson()
{
Console.WriteLine("请输入ID:");
ID= Convert.ToString(Console.ReadLine());
Console.WriteLine("请输入Name:");
Name = Convert.ToString(Console.ReadLine());
}
public void OutPerson()
{
Console.WriteLine("ID:{0}",ID);
Console.WriteLine("Name:{0}", Name);
}
}
public class Student : Person
{
private string sClass;
private double cScore;
public string SClass
{
get { return sClass; }
set { sClass = value; }
}
public double CScore
{
get { return cScore; }
set { cScore = value; }
}
public void InStudent()
{
Console.Write("学生");
base.InPreson();
Console.WriteLine("班级: ");
SClass = Convert.ToString(Console.ReadLine());
Console.WriteLine("语文成绩:");
CScore = Convert.ToDouble(Console.ReadLine());
Console.WriteLine();
}
public void OutStudent()
{
base.OutPerson();
Console.WriteLine("班级:{0}", sClass);
Console.WriteLine("语文成绩:{0}", cScore);
Console.WriteLine();
}
}
public class Teacher:Person
{
private string jobTitle;
private string department;
public string JobTitle
{
get { return jobTitle; }
set { jobTitle = value; }
}
public string Department
{
get { return department; }
set { department = value; }
}
public void InTeacher()
{
Console.Write("老师");
base.InPreson();
Console.WriteLine("职称:");
JobTitle = Convert.ToString(Console.ReadLine());
Console.WriteLine("部门:");
Department = Convert.ToString(Console.ReadLine());
Console.WriteLine();
}
public void OutTeacher()
{
base.OutPerson();
Console.WriteLine("职称:{0}", jobTitle);
Console.WriteLine("部门:{0}", department);
Console.WriteLine();
}
}
class Program
{
static void Main(string[] args)
{
Student lihua = new Student();
Teacher panlaoshi = new Teacher();
lihua.InStudent();
lihua.OutStudent();
panlaoshi.InTeacher();
panlaoshi.OutTeacher();
}
}
}