做一个装饰器 Java 程序。为什么会显示不匹配?

Doing a decorator Java program. Why does it show a mismatch?

我有一个关于我正在为学校做的项目的问题。我必须执行以下操作:

Write a Java program that uses decorator classes to add capabilities to employees. In a typical company, an employee will be asked to perform a number of duties, such as Department Head, Safety Coordinator, Recruiter, or Community Liason. You should have additional ones besides those. Your Java program will create Employees and then decorate these employees at runtime.

Create an abstract class named Employee, with last name and description fields, and a getDescription() method. Create a concrete class named SalariedEmployee that extends Employee. Create an abstract class named ResponsibilityDecorator that is able to decorate an employee and return the employee's responsibility as a string. It will have an abstract getDescription method. Create some job category classes that extend the ResponsibilityDecorator class and implement the getDescription() method.

In your main test program, create at least 5 Employee objects and pass them to the constructors of each of your decorator classes. The first employee should be you so use your last name. Then, print each Employee by calling its getDescription() method. All employees should not have the same number of responsibilities. The program's output should look something like this for each :

[LastName]: Manager, Recruiter, CommunityLiaison, ProductionDesigner

这是我到目前为止所做的:

Employee.java:

import java.util.*;

public abstract class Employee 
{
    String lastName, description;

    public String getDescription()
    {
        return description;
    }

}

受薪Employee.java:

import java.util.*;

public abstract class SalariedEmployee extends Employee
{
    String lastName, description;

    public abstract String getDescription();
}

ResponsibilityDecorator.java:

import java.util.*;

public abstract class ResponsibilityDecorator
{
    public String getDescription()
    {
        return employee.getDescription();
    }

    Employee employee;

    public Employee getEmployee() {
        return employee;
    }

    public void setEmployee(Employee employee) {
        this.employee = employee;
    }

    public boolean equals(Object obj) {
        return employee.equals(obj);
    }

    public int hashCode() {
        return employee.hashCode();
    }

    public String toString() {
        return employee.toString();
    }

    public ResponsibilityDecorator(Employee employee) {
        super();
        this.employee = employee;
    }

    public ResponsibilityDecorator() {
        super();
        // TODO Auto-generated constructor stub
    }
}

Rodriguez.java(第一位员工class)

import java.util.*;

public abstract class Rodriguez extends ResponsibilityDecorator 
{
    Employee employee1;

    String lastName = "Rodriguez";
    String description = "Tech Support";

    public Rodriguez (Employee employee1)
    {
        this.employee = employee;
    }

    public String getDescription()
    {
        return description;
    }
}

Doe.java(第二个员工class):

import java.util.*;

public abstract class Doe extends ResponsibilityDecorator 
{
    Employee employee2;

    String lastName = "Doe";
    String description = "Security Guard, Police Officer";

    public Doe (Employee employee2)
    {
        this.employee = employee;
    }

    public String getDescription()
    {
        return description;
    }
}

Jill.java(第三个员工对象):

import java.util.*;

public abstract class Doe extends ResponsibilityDecorator 
{
    Employee employee2;

    String lastName = "Doe";
    String description = "Security Guard, Police Officer";

    public Doe (Employee employee2)
    {
        this.employee = employee;
    }

    public String getDescription()
    {
        return description;
    }
}

Maria.java(第四名员工class):

import java.util.*;

public abstract class Maria extends ResponsibilityDecorator
{
    Employee employee4;

    String lastName = "Maria";
    String description = "Receptionist, Valet, Cashier, Restock";

    public Maria (Employee employee4)
    {
        this.employee = employee;
    }

    public String getDescription()
    {
        return description;
    }
}

James.java(第五名员工class):

import java.util.*;

public abstract class James extends ResponsibilityDecorator
{
    Employee employee5;

    String lastName = "James";
    String description = "Manager, CEO, Economy, President, Analytics";

    public James (Employee employee5)
    {
        this.employee = employee;
    }

    public String getDescription()
    {
        return description;
    }
}

Test.java:

import java.util.*;

public class Test 
{

    public static void main(String[] args)
    {
        Employee employee1 = new Rodriguez();
        System.out.println("Rodriguez:" + 
                employee1.getDescription());

        Employee employee2 = new Doe();
        System.out.println("Doe" + 
                employee2.getDescription());

        Employee employee3 = new Jill();
        System.out.println("Jill:" + 
                employee3.getDescription());

        Employee employee4 = new Maria();
        System.out.println("Maria:" + 
                employee4.getDescription());

        Employee employee5 = new James();
        System.out.println("James:" + 
                employee5.getDescription());
    }
}

由于某种原因,它显示类型不匹配错误。我一直在网上搜索,甚至问过一些人。我尝试了很多修复,但它们只会产生另一个错误。我不知道如何解决这个问题。

您正在创建类型为 Employee 的 objects 的实例 但是根据您的代码,他们中 none 有关系或者是员工 class 的 children。

您定义了 Employee 和 Salary 员工是什么,但是您的 none 人 objects 扩展了它们。

这里好像有几个问题。让我们看一下作业文本:

Create an abstract class named Employee, with last name and description fields, and a getDescription() method.

这看起来不错

Create a concrete class named SalariedEmployee that extends Employee.

您将 SalariedEmployee 设为摘要 class。删除 abstract 并实现 getDescription 方法。

Create an abstract class named ResponsibilityDecorator that is able to decorate an employee and return the employee's responsibility as a string. It will have an abstract getDescription method.

根据我熟悉的装饰器模式,ResponsibilityDecorator 应该扩展 Employee 如果它应该 "decorate an employee" - 你有没有学过其他东西?

根据我希望看到的任务描述:

abstract class ResponsibilityDecorator extends Employee {
    Employee employee;
    ResponsibilityDecorator(Employee e) { this.employee = e; }
    abstract String getDescription();
}

Create some job category classes that extend the ResponsibilityDecorator class and implement the getDescription() method.

你还没有这样做,除非你考虑玛丽亚、罗德里格斯等 "job categories."

"Manager" 工作类别可能如下所示:

class Manager extends ResponsibilityDecorator {
    Manager(Employee employee) { super(employee); }
    String getDescription() { return employee.getDescription() + " Manager"; }
}

In your main test program, create at least 5 Employee objects

也许您混淆了这一步和上一步并创建了 5 名员工 classes(Maria、Rodriguez、...)?


请注意,此练习的解决方案会让专业开发人员摸不着头脑,因为这不是在程序中表示工作类别或职责的合理方式。

一些基本的 OOP 概念在代码中不匹配。
abstract class 不能用于初始化(就像你所做的那样)
Employee employee1 = new Rodriguez();
行代码没问题,但错误来自Rodriguez class
尝试将 class def 更改为:
public class Rodriguez extends ResponsibilityDecorator
这不会解决问题,因为 Rodriguez_ResponsibilityDecorator(type) 和 Employee(type) 之间没有关系,但可以进一步理解代码。(仅有一个 Employee 嵌入在 ResponsibilityDecorator 上的实例变量。它可以用来重新设计代码并使 link 带有 Employee,但我怀疑您是否会遵循模式。(作为替代解决方案注明)


通常所有 employees 都应该从 Employee 派生,然后适当的代码调整将是 public class Rodriguez extends Employee ... 并且在代码应该被调整之后。(从这里你应该开始)。再看看模式。

再提示一下,正确的Name_Rodriguez不是一个足够的class def,它更像是一个属性的实例。例如:Employee e1 = new CustomEmployee("Rodriguez","last name", age_int)CustomEmployee 是具体的 class 其中 extends Employee ...(员工可能是 abstractinterface