(1)创建一个学生类clsStudent。
(2)在类clsStudent中添加相应的字段。
(3)在类clsStudent中添加相应的属性。
(4)在类clsStudent中添加构造函数。
(5)学生类对象的创建及对象成员的引用。
(6)在学生类中实现Grow()方法的重载。
(7)通过静态字段实现学生人数的统计
学生信息包括学号、姓名、年龄等属性,也包括长大、入学、毕业等行为。在此,通过定义一个学生类clsStudent,来初步实现学生信息的管理。所定义的学生类中有3个私有属性:nNum(学号)、strName(姓名)、nAge(年龄);函数:clsStudent(构造函数)。其中,使用构造函数为clsStudent类的对象赋值,以实现对象的初始化。运用方法重载机制,对于学生类clsStudent的Grow()方法进行重载;运用类的静态成员,实现对于学生的人数的统计。
public class clsStudent
{
private string nNum;
private string strName;
private int nAge;
private int StudentCount; //学生的人数
static clsStudent()
{
StudentCount++;
}
public clsStudent(string nNum, string strName, int nAge)
{
this.nNum = nNum;
this.strName = strName;
this.nAge = nAge;
}
public void Grow()
{
MessageBox.Show(this.strName + "长大了");
}
public void Grow(string msg)
{
MessageBox.Show(this.strName + "长大了," + msg);
}
}
学生类对象的创建及对象成员的引用:
clsStudent c = new clsStudent("1001", "小明", 20);
c.Grow(); //弹出窗口:小明长大了
c.Grow("哈哈~"); //弹出窗口:小明长大了,哈哈~
MessageBox.Show("学生人数" + clsStudent.StudentCount.ToString()); //弹出窗口:学生的人数1追问
{
private string nNum;
private string strName;
private int nAge;
private int StudentCount; //学生的人数
static clsStudent()
{
StudentCount++;
}
public clsStudent(string nNum, string strName, int nAge)
{
this.nNum = nNum;
this.strName = strName;
this.nAge = nAge;
}
public void Grow()
{
MessageBox.Show(this.strName + "长大了");
}
public void Grow(string msg)
{
MessageBox.Show(this.strName + "长大了," + msg);
}
}
学生类对象的创建及对象成员的引用:
clsStudent c = new clsStudent("1001", "小明", 20);
c.Grow(); //弹出窗口:小明长大了
c.Grow("哈哈~"); //弹出窗口:小明长大了,哈哈~
MessageBox.Show("学生人数" + clsStudent.StudentCount.ToString()); //弹出窗口:学生的人数1追问
好像是窗体,我们要控制台,不过还是谢谢了,辛苦了
追答原来代码是手写没测试过。想了一下,
private int StudentCount; //学生的人数
这行应该加个static
private static int StudentCount = 0;//学生的人数
温馨提示:答案为网友推荐,仅供参考
第1个回答 2013-05-16
第一,太简单了,但是要写好多字,第二,分太少了。追问
public class clsStudent {
private long nNum;
private String strName;
private int nAge;
public clsStudent(long nNum, String strName, int nAge) {
super();
this.nNum = nNum;
this.strName = strName;
this.nAge = nAge;
}
public long getnNum() {
return nNum;
}
public void setnNum(long nNum) {
this.nNum = nNum;
}
public String getStrName() {
return strName;
}
public void setStrName(String strName) {
this.strName = strName;
}
public int getnAge() {
return nAge;
}
public void setnAge(int nAge) {
this.nAge = nAge;
}
public void grow(){
//to do
}
public void grow(int age){
//to do
}
public void entrance(){
//to do
}
public void graduation(){
//to do
}
}
虽然是15分,但是是全部家当了,求帮忙
追答米有装vs,用java写了个,先凑合用吧。
package SingleDemo;public class clsStudent {
private long nNum;
private String strName;
private int nAge;
public clsStudent(long nNum, String strName, int nAge) {
super();
this.nNum = nNum;
this.strName = strName;
this.nAge = nAge;
}
public long getnNum() {
return nNum;
}
public void setnNum(long nNum) {
this.nNum = nNum;
}
public String getStrName() {
return strName;
}
public void setStrName(String strName) {
this.strName = strName;
}
public int getnAge() {
return nAge;
}
public void setnAge(int nAge) {
this.nAge = nAge;
}
public void grow(){
//to do
}
public void grow(int age){
//to do
}
public void entrance(){
//to do
}
public void graduation(){
//to do
}
}
第2个回答 2013-05-16
想问下Grow()函数是干啥的?