对于基本类型当中的boolean值,Getter方法一定要写成isXxx的形式,而setXxx规则不变。
例如:
package 类和对象;
public class Demo05 {
public static void main(String[] args) {
Student stu = new Student();
stu.setName("鹿晗");
stu.setAge(20);
stu.setMale(true);
System.out.println("姓名:"+ stu.getName());
System.out.println("年龄:"+ stu.getAge());
System.out.println("是否为男性:"+ stu.isMale());
}
}
package 类和对象;
public class Student {
private String name; //姓名
private int age; //年龄
private boolean male; //判断是否为男性
public void setMale(boolean b){
male = b;
}
public boolean isMale(){
return male;
}
public void setName(String str){
name = str;
}
public String getName(){
return name;
}
public void setAge(int num){
age = num;
}
public int getAge(){
return age;
}
}
运行结果: