image.png

    1. package com.atguigu.exercise;
    2. public class Boy {
    3. //属性
    4. private String name;
    5. private int age;
    6. //构造器
    7. public Boy(){
    8. }
    9. public Boy(String name){
    10. this.name = name;
    11. }
    12. public Boy(String name,int age){
    13. this.name = name;
    14. this.age = age;
    15. }
    16. //方法
    17. public void setName(String name){
    18. this.name = name;
    19. }
    20. public String getName(){
    21. return name;
    22. }
    23. public void setAge(int age){
    24. this.age = age;
    25. }
    26. public int getAge(){
    27. return age;
    28. }
    29. public void marry(Girl girl){
    30. System.out.println("我想娶"+girl.getName());
    31. }
    32. public void shout(){
    33. if(this.age >= 22){
    34. System.out.println("你可以去合法登记了!");
    35. }else{
    36. System.out.println("你还不能结婚!");
    37. }
    38. }
    39. }

    1. package com.atguigu.exercise;
    2. public class Girl {
    3. // 属性
    4. private String name;
    5. private int age;
    6. // 构造器
    7. public Girl() {
    8. }
    9. public Girl(String name) {
    10. this.name = name;
    11. }
    12. public Girl(String name, int age) {
    13. this.name = name;
    14. this.age = age;
    15. }
    16. // 方法
    17. public String getName() {
    18. return name;
    19. }
    20. public void setName(String name) {
    21. this.name = name;
    22. }
    23. public void marry(Boy boy){
    24. System.out.println("我想嫁给你"+ boy.getName());
    25. boy.marry(this);
    26. }
    27. public int compare(Girl girl){
    28. return this.age - girl.age;
    29. }
    30. }

    1. package com.atguigu.exercise;
    2. public class BoyGirlTest {
    3. public static void main(String[] args) {
    4. Boy boy = new Boy("罗密欧",21);
    5. boy.shout();
    6. Girl girl = new Girl("朱丽叶",18);
    7. girl.marry(boy);
    8. }
    9. }