描述
【描述】
定义一个Student类,表示学生信息。Student类有学号(id,整型)、姓名(name,字符串)、性别(sex,字符型,m表示男性,f表示女性)三个私有数据域;有参构造方法将学号、姓名、性别设置为给定的参数;成员方法display显示学生的信息。
注意,Student类的定义应该这样开始:
class Student {
也就是说,Student类的class前面不要有public。
【输入】
输入学号、姓名、性别。
【输出】
输出学号、姓名、性别。
【输入示例】
1000
Tan
m
【输出示例】
1000
Tan
m`
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
int id=input.nextInt();
String s=new String();
s=input.next();
char c=input.next().charAt(0);
input.close();
Student st=new Student(id,s,c);
st.display();
}
}
class Student {
private int id;
String name=new String();
private char sex;
public Student(int a,String b,char c)
{
id=a;
name=b;
sex=c;
}
public void display()
{
System.out.println(id);
System.out.println(name);
System.out.println(sex);
}
}