源码
package ExerciseMethod;
/*
定义一个标准的JavaBean类
*/
public class Student4 {
private String name;
private int age;
//无参构造
public Student4(){}
//全参构造
public Student4(String name,int age){
this.name=name;
this.age=age;
}
//设置名字
public void setName(String name) {
this.name = name;
}
//获取名字
public String getName() {
return name;
}
//设置编号
public void setAge(int age) {
this.age = age;
}
//获取编号
public int getAge() {
return age;
}
}
测试类
package ExerciseMethod;
public class TextStudent4 {
public static void main(String[] args) {
//空参创建对象
Student4 stu1=new Student4();
stu1.setName("张三");
stu1.setAge(22);
System.out.println(stu1.getName());
System.out.println(stu1.getAge());
System.out.println();
//全参创建对象
Student4 stu2=new Student4("李四",22);
System.out.println(stu2.getName());
System.out.println(stu2.getAge());
}
}
结果
张三
22
李四
22