Chinaunix首页 | 论坛 | 博客
  • 博客访问: 346938
  • 博文数量: 81
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 847
  • 用 户 组: 普通用户
  • 注册时间: 2015-03-25 22:29
个人简介

执一不失,能君万物http://weidian.com/s/284520723?wfr=c

文章分类

全部博文(81)

文章存档

2016年(11)

2015年(70)

我的朋友

分类: Java

2015-07-17 18:28:01


------------------------------------------------------------------------------

点击(此处)折叠或打开

  1. package com.troubleshooting.javase.exethis;

  2. public class Girl {
  3.     private String name;

  4.     public String getName() {
  5.         return name;
  6.     }

  7.     public void setName(String name) {
  8.         this.name = name;
  9.     }

  10.     public void marry(Boy boy) {
  11.         System.out.println("我要嫁给" + boy.getName());
  12.         boy.marry(this);
  13.     }
  14. }

  15. package com.troubleshooting.javase.exethis;

  16. public class Boy {
  17.     private String name;
  18.     private int age;

  19.     public String getName() {
  20.         return name;
  21.     }

  22.     public void setName(String name) {
  23.         this.name = name;
  24.     }

  25.     public int getAge() {
  26.         return age;
  27.     }

  28.     public void setAge(int age) {
  29.         this.age = age;
  30.     }

  31.     public void marry(Girl girl) {
  32.         System.out.println("我要娶" + girl.getName());
  33.     }

  34.     public void marry(Boy boy) {
  35.         System.out.println("我要娶" + boy.getName());
  36.     }

  37.     public void shout() {
  38.         if (this.age >= 22) {
  39.             System.out.println("我到了结婚年龄了!");
  40.         } else {
  41.             System.out.println("你还是先谈谈恋爱吧!");
  42.         }
  43.     }

  44. }

  45. package com.troubleshooting.javase.exethis;

  46. public class TestBoyGirl {
  47.     public static void main(String[] args) {
  48.         Boy b1 = new Boy();
  49.         Girl g1 = new Girl();
  50.         b1.setName("Jerry");
  51.         b1.setAge(21);
  52.         g1.setName("lucy");

  53.         b1.marry(g1);
  54.         g1.marry(b1);
  55.         b1.shout();
  56.     }
  57. }
显示结果:
我要娶lucy
我要嫁给Jerry
我要娶lucy
你还是先谈谈恋爱吧!



阅读(2355) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~