image.png

    1. package com.atguigu.exercise;
    2. public class ManKind {
    3. //属性
    4. private int sex;//性别
    5. private int salary;//薪资
    6. //构造器
    7. public ManKind(){
    8. }
    9. public ManKind(int sex,int salary){
    10. this.sex = sex;
    11. this.salary = salary;
    12. }
    13. //方法
    14. public void setSex(int sex){
    15. this.sex = sex;
    16. }
    17. public int getSex(){
    18. return sex;
    19. }
    20. public void setSalary(int salary){
    21. this.salary = salary;
    22. }
    23. public int getSalary(){
    24. return salary;
    25. }
    26. public void manOrWoman(){
    27. if(sex == 1){
    28. System.out.println("man");
    29. }else if(sex == 0){
    30. System.out.println("woman");
    31. }
    32. }
    33. public void employeed(){
    34. if(salary == 0){
    35. System.out.println("no job");
    36. }else if(salary != 0){
    37. System.out.println("job");
    38. }
    39. }
    40. }

    1. package com.atguigu.exercise;
    2. public class Kids extends ManKind{
    3. //属性
    4. private int yearsOld;
    5. //构造器
    6. public Kids(){
    7. }
    8. public Kids(int yearsOld){
    9. this.yearsOld = yearsOld;
    10. }
    11. //方法
    12. public void setYearsOld(int yearsOld){
    13. this.yearsOld = yearsOld;
    14. }
    15. public int getYearsOld(){
    16. return yearsOld;
    17. }
    18. public void printAge(){
    19. System.out.println("I am " + yearsOld + " years old");
    20. }
    21. }

    1. package com.atguigu.exercise;
    2. public class KindsTest {
    3. public static void main(String[] args){
    4. Kids somekid = new Kids(12);
    5. somekid.setSex(1);
    6. somekid.setSalary(0);
    7. somekid.manOrWoman();
    8. somekid.employeed();
    9. somekid.printAge();
    10. }
    11. }