标题:定义一个学生类student
只看楼主
叶子死灵
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2014-12-14
结帖率:0
 问题点数:0 回复次数:3 
定义一个学生类student
要求:
1.字段包括姓名、性别、身高、年龄、学生人数count等信息;
2.方法包括设置以上字段的方法setinfo()和显示类的各个字段信息display()。
3.编写构造函数,完成对象初始化操作。
4.构造函数的重载。
5.编写一个变性手术的方法,用于改变已知学生的性别,即男性变女性或女性变男性
求帮写下代码,谢谢
搜索更多相关主题的帖子: display count 变性手术 女性 信息 
2014-12-14 20:42
xydddaxia
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:贵宾
威 望:33
帖 子:466
专家分:2307
注 册:2009-3-20
得分:0 
程序代码:
public class Student
{
    public Student()
    {

    }
    public Student(string name, string sex, int age)
    {
        Name = name;
        sex = Sex;
        Age = age;
    }
    public string Name { get; set; }
    public string Sex { get; set; }
    public int Age { get; set; }
    public void SetInfo(string name, string sex, int age)
    {
        Name = name;
        sex = Sex;
        Age = age;
    }
    public string Display()
    {
        return string.Format("姓名:{0} 性别:{1} 年龄:{2}", Name, Sex, Age);
    }
    public void SetSex(string sex)
    {
        if (sex != "" && sex != "")
        {
            throw new Exception("SexError");
        }
        this.Sex = sex;
    }
}

站在春哥的肩膀上
2014-12-15 14:33
叶子死灵
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2014-12-14
得分:0 
回复 2楼 xydddaxia
谢了啊
2014-12-15 17:33
叶子死灵
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2014-12-14
得分:0 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 实验五案例一
{
    class student
    {
        private string name;
        private string sex;
        private double hight;
        private int age;
        static private int count=0;
        public student()
        {
            count++;
        }
        public student(string name, string sex, double hight, int age)
        {
            this.name = name;
            this.sex = sex;
            this.hight = hight;
            this.age = age;
            count++;
        }
        public void setinfo(string name, string sex, double hight, int age)
        {
            this.name = name;
            this.sex = sex;
            this.hight = hight;
            this.age = age;
        }
        public void display()
        {
            Console.WriteLine("性名:{0}\n性别:{1}\n身高:{2}\n年龄:{3}\n学生总人数:{4}",name,sex,hight,age,count);
        }
        static public void display(student []a)
        {
            foreach (student S in a)
            {
                S.display();
                Console.WriteLine();
            }
        }
        static public student changesex(student a)
        {
            if(a.sex=="男")
            {
                a.sex="女";
            }
            else
                a.sex="男";
            return a;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            student S1 = new student();
            student S2 = new student("李四", "男", 173.5, 20);
            student[] S = new student[2];
            S[0] = new student("张三", "男", 172, 11);
            S[1] = new student("李四", "男", 171, 12);
            S1.setinfo("张三","男",174,21);
            student.changesex(S1);
            S1.display();
            Console.WriteLine();
            S2.display();
            Console.WriteLine();
            student.display(S);
            Console.ReadLine();
        }
    }
}
2014-12-15 22:51



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-439937-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.107689 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved