题目描述
1.定义一个学生类
属性包括:学生编号、学生姓名、学生年龄
属性私有化,对外提供公开的set和get方法
提供无参构造和有参数构造方法
提供一个display()方法,通过该方法打印学生信息。
编写测试程序创建对象,调用display()方法。
实现效果
学生学号:20201901
学生姓名:李四
学生年龄:20
Java代码实现
public class Homework1902 {
public static void main(String[] args) {
Student student = new Student();
//通过set方式赋值
student.setStuId(20201901);
student.setName("李四");
student.setAge(20);
//调用display()方法
student.display();
}
}
//学生类
class Student{
//学生编号
private int stuId;
//学生姓名
private String name;
//学生年龄
private int age;
//set和get方法
public int getStuId(){
return stuId;
}
public void setStuId(int stuId){
this.stuId = stuId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
//提供无参构造和有参数构造方法
public Student(