12.15

 

婚介所其实就是找对象的一个代理,请仿照我们的课堂例子论坛权限控制代理完成这个实际问题,其中如果年纪小于18周岁,婚介所会提示对不起,不能早恋!,并终止业务。

实验要求:

1. 提交类图;

2. 提交源代码;

3. 注意编程规范。

 

 

类图:

 

 

 

 

源代码:

 

public abstract class Center

{

    protected String name;

    public Center(String name){this.name=name;}

    public abstract void findlove(Person person);

}

 

 

public class Marry extends Center

{

     public Marry(String name){super(name);}

        public void findlove(Person person)

        {

            if(person.getAge()<18)

            {

                System.out.println(name+":"+person.getName()+",对不起!不能早恋!");

            }

            else if(person.getAge()<100)

            {

                System.out.println(name+":"+person.getName()+",我们会为您找到合适的伴侣!");

            }

            else

            {

                System.out.println(name+":"+person.getName()+",对不起!您年龄太大了!");

            }

        }

}

 

public class Person {

    protected String name;

    protected int age;

 

    public Person(String name,int age)

    {

        this.name=name;

        this.age=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 class Client

{

    public static void main(String args[])

    {

        Center center1,center2;

        center1=new Marry("婚介");

        center2=new Marry("婚介");

        Person b1=new Person("喜羊羊",29);

        Person b2=new Person("美羊羊",16);

        center1.findlove(b1);

        center2.findlove(b2);

    }

 

}

 

 

运行截图:

 

 

posted on 2024-12-15 21:46  HA_wind  阅读(1)  评论(0编辑  收藏  举报

导航