实验14:代理模式

[实验任务一]:婚介所

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

实验要求:

1. 提交类图;

2. 提交源代码;

3. 注意编程规范。

package org.example;

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);
    }
}

package org.example;

public abstract class Center
{
    protected String name;
    public Center(String name){this.name=name;}
    public abstract void findlove(Person person);
}

package org.example;

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()+",对不起!您年龄太大了!");
        }
    }
}

package org.example;

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;
    }
}

posted @ 2023-12-09 20:14  会秃头的小白  阅读(5)  评论(0编辑  收藏  举报