------------------------------------------------------------------------------
-
package com.troubleshooting.javase.exethis;
-
-
public class Girl {
-
private String name;
-
-
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);
-
}
-
}
-
-
package com.troubleshooting.javase.exethis;
-
-
public class Boy {
-
private String name;
-
private int age;
-
-
public String getName() {
-
return name;
-
}
-
-
public void setName(String name) {
-
this.name = name;
-
}
-
-
public int getAge() {
-
return age;
-
}
-
-
public void setAge(int age) {
-
this.age = age;
-
}
-
-
public void marry(Girl girl) {
-
System.out.println("我要娶" + girl.getName());
-
}
-
-
public void marry(Boy boy) {
-
System.out.println("我要娶" + boy.getName());
-
}
-
-
public void shout() {
-
if (this.age >= 22) {
-
System.out.println("我到了结婚年龄了!");
-
} else {
-
System.out.println("你还是先谈谈恋爱吧!");
-
}
-
}
-
-
}
-
-
package com.troubleshooting.javase.exethis;
-
-
public class TestBoyGirl {
-
public static void main(String[] args) {
-
Boy b1 = new Boy();
-
Girl g1 = new Girl();
-
b1.setName("Jerry");
-
b1.setAge(21);
-
g1.setName("lucy");
-
-
b1.marry(g1);
-
g1.marry(b1);
-
b1.shout();
-
}
-
}
显示结果:
我要娶lucy
我要嫁给Jerry
我要娶lucy
你还是先谈谈恋爱吧!
阅读(2429) | 评论(0) | 转发(0) |