请按照以下要求设计一个学生类Student,并进行测试。 1)Student类中包含姓名、成绩两个字段。 (2)分别给这两个字段定义自己的属性。 (3)Student类中定义两个构造方法,其中一个是无参的构造方法,另一个是接收两个参数 的构造方法,分别用于为姓名和成绩属性赋值。 (4)在Main0方法中分别调用不同的构造方法创建两个Student对象,并为属性和性 赋值。
class Student
{
public string Name { get; set; }
public int Age { get; set; }
public Student()
{
}
public Student(string name,int age)
{
Name = name;
Age = age;
}
public Student()
{
}
static void Main(string[] args)
{
Student stu=new Student("张三",18);
Student stu2=new Student();
stu2.name="李四";
stu2.age=19;
}
}
代码如上,万望采纳。
using System;
namespace ConsoleApp1
{
class Student
{
private string _Name;
private int _Score;
public string Name { get { return _Name; } set { _Name = value; } }
public int Score { get { return _Score; } set { _Score = value; } }
public Student() { }
public Student(string Name, int Score)
{
this.Name = Name;
this.Score = Score;
}
}
class Program
{
static void Main(string[] args)
{
Student s1 = new Student();
s1.Name = "s1";
s1.Score = 80;
Student s2 = new Student("s2",90);
Console.WriteLine(s1.Name + ":" + s1.Score);
Console.WriteLine(s2.Name + ":" + s2.Score);
Console.ReadKey();
}
}
}
public class Student { //1、定义成员变量 private String name;//姓名 private float score;//成绩 //2、通过构造方法初始化成员变量的值 public Student(String name, float score){ this.name = name; this.score = score; } //测试 public static void main(String[] args) { //new一个学生对象,并使用构造方法初始化成员变量 Student S01 = new Student("张三", 99); System.out.println("姓名:"+S01.name+"\n成绩"+S01.score); } }
您好,我是有问必答小助手,您的问题已经有小伙伴解答了,您看下是否解决,可以追评进行沟通哦~
如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~
ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632