编一个学生类(Student)

编一个学生类(Student),其中包含以下内容:

属性:学号studentNo,姓名studentName,性别studentGender,年龄studentAge。

方法:构造方法,显示学号方法showNo(),显示姓名方法showName(),显示性别方法showSex(),显示年龄方法showAge(),修改年龄方法modifyAge()。

主类(S3_1)包含:主方法main(),在其中创建两个学生对象s1和s2并初始化,两个对象的属性自行确定,然后分别显示这两个学生的学号、姓名、性别、年龄,然后修改s1的年龄并显示修改后的结果。

class Student{
     String studentNo;
     String studentName;
     String studentGender;
     int studentAge;
    public Student(String no,String name,String gender,int age){
        studentNo=no;
        studentAge=age;
        studentName=name;
        studentGender=gender;
    }
    public void showno(){
        System.out.println("学号"+studentNo);
    }
    public void showage(){
        System.out.println("年龄"+studentAge);
    }
    public void showname(){
        System.out.println("名字"+studentName);
    }
    public void showgender(){
        System.out.println("性别"+studentGender);
    }
    public void modifyAge(int age){
        studentAge=age;
        System.out.println("修改后的年龄为"+studentAge);
    }
}
public class S3_1 {
    public static void main(String[] args) {
        Student S1=new Student("001","xiaoming","男",20);
        System.out.println("学生一的信息为");
        S1.showno();
        S1.showname();
        S1.showgender();
        S1.showage();
        S1.modifyAge(200);
        Student S2=new Student("002","小红","女",3);
        System.out.println("学生二的信息为");
        S2.showno();
        S2.showname();
        S2.showgender();
        S2.showage();
    }
}

编一个学生类(Student)_第1张图片

你可能感兴趣的:(java实验,java,开发语言)