JAVA 创建学生类

1 题目

编写程序实现如下功能:已知学生类有域变量(学号、班号、姓名、性别、年龄)和方法(获得学号、获得班号、获得性别、获得年龄、修改年龄,显示基本信息),定义一组学生对象,并初始化他们的基本信息,然后依次输出。

2 源代码

Student.java

//学生类
class Student
{
	private String sno; 
	private String classno; 
	private String name; 
	private char sex; 
	private int age;
	
	//有参数的构造函数
	public Student(String sno,String classno,String name,char sex, int age)
	{
		this.sno = sno;
		this.classno = classno;
		this.name = name;
		this.sex = sex;
		this.age = age;
	}
	//无参数的构造函数
	public Student()
	{}
	//获取学生的学号
    String getSno()
    {
    	return sno;
    } 
    //获取学生的班号
    String getClassno()
    {
    	return classno;
    } 
    //获取学生的性别
      char getSex()
    {
    	return sex;
    }
    //获取学生年龄
    int getAge()
    {
    	return age;
    }
    //更新学生的年龄信息
    void updateAge(int age)
    {
    	this.age = age;
    }
    //输出学生信息
    void print()
    {
    	System.out.println("学生信息:" + '\n');
    	System.out.println("       " + "学号" + "                   " + "班号" + "          " + "姓名" + "    " + "性别" + "   " + "年龄" + '\n');
    	System.out.println(this.sno + "   " + this.classno + "     " + this.name + "    " + this.sex + "   " + this.age + '\n');
    } 
}

Student_Information.java

public class Student_Information {

	public static void main(String[] args) 
	{
		char i;
		Student student[] = new Student[6];//定义一个数组
		//对数组里面的元素进行初始化
		student[0] = new Student("201704166051","电信1702班","Boly",'Y',18);
		student[1] = new Student("201704166052","电信1702班","Lily",'X',18);
		student[2] = new Student("201704166053","电信1702班","Jasy",'X',19);
		student[3] = new Student("201704166054","电信1702班","Tom",'Y',19);
		student[4] = new Student("201704166055","电信1702班","Jack",'Y',18);
		student[5] = new Student("201704166056","电信1702班","Bob",'Y',19);
		//显示各学生信息
		for(i = 0;i < 6;i++)
		{
			student[i].print();
		}
	}
}

3 运行结果

JAVA 创建学生类_第1张图片

你可能感兴趣的:(JAVA入门,java)