对于基本类型当中的boolean值,Getter方法一定要写成isXxx的形式,而setXxx规则不变

    1. public class demo06Student {
    2. private String name ; //姓名
    3. private int age ; //年龄
    4. private boolean male; //是不是男生
    5. public void setMale(boolean b){
    6. male = b;
    7. }
    8. public boolean isMale(){
    9. return male;
    10. }
    11. public void setName(String str){
    12. name = str;
    13. }
    14. public String getName(){
    15. return name ;
    16. }
    17. public void setAge(int num){
    18. age = num;
    19. }
    20. public int getAge(){
    21. return age;
    22. }
    23. }
    1. public class demo06 {
    2. public static void main(String[] args) {
    3. demo06Student stu = new demo06Student();
    4. stu.setName("鹿晗");
    5. stu.setAge(20);
    6. stu.setMale(true);
    7. System.out.println("姓名:" + stu.getName());
    8. System.out.println("年龄:" + stu.getAge());
    9. System.out.println("是不是男生:" + stu.isMale());
    10. }
    11. }