package top.qaqaq.java.P235;
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 top.qaqaq.java.P235;
public class Girl {
private String name;
private int age;
public Girl() {
}
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);
}
/**
*
* @Description 比较两个对象的大小
* @author RichieZhang
* @date 2022年10月2日上午11:59:03
* @param girl
* @return 正数:当前对象大;负数:当前对象小;0:当前对象与形参对象相等
*/
public int compare(Girl girl) {
// if(this.age > girl.age) {
// return 1;
// }else if(this.age < girl.age) {
// return -1;
// }else {
// return 0;
// }
return this.age - girl.age;
}
}
package top.qaqaq.java.P235;
public class BoyGirlTest {
public static void main(String[] args) {
Boy boy = new Boy("罗密欧",21);
boy.shout();
Girl girl = new Girl("朱丽叶", 18);
girl.marry(boy);
Girl girl1 = new Girl("祝英台",19);
int compare = girl.compare(girl1);
if(compare > 0) {
System.out.println(girl.getName() + "大");
}else if(compare < 0){
System.out.println(girl1.getName() + "大");
}else {
System.out.println("一样大");
}
}
}