下面定义了一个学生类,请利用该类创建对象,使用对应的方法将姓名、性别和年龄输出在控制台。
(注意:当类中不存在无参的构造方法时,只能使用带参数的构造方法创建对象)
class Student {
private String name; // 定义姓名
private String sex; // 定义性别
private int age; // 定义年龄
// 利用构造方法初始化域
public Student(String name, String sex, int age) {
this.name = name;
this.sex= sex;
this.age = age;
}
public String getName() { // 获得姓名
return name;
}
public String getSex() { // 获得性别
return sex;
}
public int getAge() { // 获得年龄
return age;
}
}
这有什么问题么class student{ public string 学号{get;set;} public string 姓名{get;set;} public string 性别{get;set;} public string 年龄{get;set;} public void 输出(){Console.WriteLine(学号+姓名+性别+年龄)}}
热门标签: