package com.atguigu.exercise;
public class Boy {
//属性
private String name;
private int age;
//构造器
public Boy(){
}
public Boy(String name){
this.name = name;
}
public Boy(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;
}
public void marry(Girl girl){
System.out.println("我想娶"+girl.getName());
}
public void shout(){
if(this.age >= 22){
System.out.println("你可以去合法登记了!");
}else{
System.out.println("你还不能结婚!");
}
}
}
package com.atguigu.exercise;
public class Girl {
// 属性
private String name;
private int age;
// 构造器
public Girl() {
}
public Girl(String name) {
this.name = name;
}
public Girl(String name, int age) {
this.name = name;
this.age = age;
}
// 方法
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void marry(Boy boy){
System.out.println("我想嫁给你"+ boy.getName());
boy.marry(this);
}
public int compare(Girl girl){
return this.age - girl.age;
}
}
package com.atguigu.exercise;
public class BoyGirlTest {
public static void main(String[] args) {
Boy boy = new Boy("罗密欧",21);
boy.shout();
Girl girl = new Girl("朱丽叶",18);
girl.marry(boy);
}
}