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

huangapple go评论102阅读模式
英文:

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

问题

这是你提供的程序和代码的翻译部分:

Employee.java:

  1. import java.util.*;
  2. public abstract class Employee
  3. {
  4. String lastName, description;
  5. public String getDescription()
  6. {
  7. return description;
  8. }
  9. }

SalariedEmployee.java:

  1. import java.util.*;
  2. public abstract class SalariedEmployee extends Employee
  3. {
  4. String lastName, description;
  5. public abstract String getDescription();
  6. }

ResponsibilityDecorator.java:

  1. import java.util.*;
  2. public abstract class ResponsibilityDecorator
  3. {
  4. public String getDescription()
  5. {
  6. return employee.getDescription();
  7. }
  8. Employee employee;
  9. public Employee getEmployee() {
  10. return employee;
  11. }
  12. public void setEmployee(Employee employee) {
  13. this.employee = employee;
  14. }
  15. public boolean equals(Object obj) {
  16. return employee.equals(obj);
  17. }
  18. public int hashCode() {
  19. return employee.hashCode();
  20. }
  21. public String toString() {
  22. return employee.toString();
  23. }
  24. public ResponsibilityDecorator(Employee employee) {
  25. super();
  26. this.employee = employee;
  27. }
  28. public ResponsibilityDecorator() {
  29. super();
  30. // TODO Auto-generated constructor stub
  31. }
  32. }

Rodriguez.java (第一个员工类):

  1. import java.util.*;
  2. public abstract class Rodriguez extends ResponsibilityDecorator
  3. {
  4. Employee employee1;
  5. String lastName = "Rodriguez";
  6. String description = "Tech Support";
  7. public Rodriguez (Employee employee1)
  8. {
  9. this.employee = employee;
  10. }
  11. public String getDescription()
  12. {
  13. return description;
  14. }
  15. }

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

  1. import java.util.*;
  2. public abstract class Doe extends ResponsibilityDecorator
  3. {
  4. Employee employee2;
  5. String lastName = "Doe";
  6. String description = "Security Guard, Police Officer";
  7. public Doe (Employee employee2)
  8. {
  9. this.employee = employee;
  10. }
  11. public String getDescription()
  12. {
  13. return description;
  14. }
  15. }

Jill.java (第三个员工类):

  1. import java.util.*;
  2. public abstract class Jill extends ResponsibilityDecorator
  3. {
  4. Employee employee3;
  5. String lastName = "Jill";
  6. String description = "HR Manager, Recruiter, Team Lead";
  7. public Jill (Employee employee3)
  8. {
  9. this.employee = employee;
  10. }
  11. public String getDescription()
  12. {
  13. return description;
  14. }
  15. }

Maria.java (第四个员工类):

  1. import java.util.*;
  2. public abstract class Maria extends ResponsibilityDecorator
  3. {
  4. Employee employee4;
  5. String lastName = "Maria";
  6. String description = "Receptionist, Valet, Cashier, Restock";
  7. public Maria (Employee employee4)
  8. {
  9. this.employee = employee;
  10. }
  11. public String getDescription()
  12. {
  13. return description;
  14. }
  15. }

James.java (第五个员工类):

  1. import java.util.*;
  2. public abstract class James extends ResponsibilityDecorator
  3. {
  4. Employee employee5;
  5. String lastName = "James";
  6. String description = "Manager, CEO, Economy, President, Analytics";
  7. public James (Employee employee5)
  8. {
  9. this.employee = employee;
  10. }
  11. public String getDescription()
  12. {
  13. return description;
  14. }
  15. }

Test.java:

  1. import java.util.*;
  2. public class Test
  3. {
  4. public static void main(String[] args)
  5. {
  6. Employee employee1 = new Rodriguez();
  7. System.out.println("Rodriguez: " +
  8. employee1.getDescription());
  9. Employee employee2 = new Doe();
  10. System.out.println("Doe: " +
  11. employee2.getDescription());
  12. Employee employee3 = new Jill();
  13. System.out.println("Jill: " +
  14. employee3.getDescription());
  15. Employee employee4 = new Maria();
  16. System.out.println("Maria: " +
  17. employee4.getDescription());
  18. Employee employee5 = new James();
  19. System.out.println("James: " +
  20. employee5.getDescription());
  21. }
  22. }

关于你提到的类型不匹配错误,主要是因为你的员工类没有按照正确的继承体系进行构建。你定义了抽象类并试图创建实例,但在类的设计和实例化上存在混淆。我已经对这些类进行了必要的更改,以使它们符合继承关系并能够正确地构造对象。请注意,你仍然需要根据你的需求进行进一步的修改和完善。

英文:

I have a question regarding a program I'm doing for school. I have to do the following:

> 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

Here's what I've done so far:

Employee.java:

  1. import java.util.*;
  2. public abstract class Employee
  3. {
  4. String lastName, description;
  5. public String getDescription()
  6. {
  7. return description;
  8. }
  9. }

SalariedEmployee.java:

  1. import java.util.*;
  2. public abstract class SalariedEmployee extends Employee
  3. {
  4. String lastName, description;
  5. public abstract String getDescription();
  6. }

ResponsibilityDecorator.java:

  1. import java.util.*;
  2. public abstract class ResponsibilityDecorator
  3. {
  4. public String getDescription()
  5. {
  6. return employee.getDescription();
  7. }
  8. Employee employee;
  9. public Employee getEmployee() {
  10. return employee;
  11. }
  12. public void setEmployee(Employee employee) {
  13. this.employee = employee;
  14. }
  15. public boolean equals(Object obj) {
  16. return employee.equals(obj);
  17. }
  18. public int hashCode() {
  19. return employee.hashCode();
  20. }
  21. public String toString() {
  22. return employee.toString();
  23. }
  24. public ResponsibilityDecorator(Employee employee) {
  25. super();
  26. this.employee = employee;
  27. }
  28. public ResponsibilityDecorator() {
  29. super();
  30. // TODO Auto-generated constructor stub
  31. }
  32. }

Rodriguez.java (first employee class)

  1. import java.util.*;
  2. public abstract class Rodriguez extends ResponsibilityDecorator
  3. {
  4. Employee employee1;
  5. String lastName = "Rodriguez";
  6. String description = "Tech Support";
  7. public Rodriguez (Employee employee1)
  8. {
  9. this.employee = employee;
  10. }
  11. public String getDescription()
  12. {
  13. return description;
  14. }
  15. }

Doe.java (second employee class):

  1. import java.util.*;
  2. public abstract class Doe extends ResponsibilityDecorator
  3. {
  4. Employee employee2;
  5. String lastName = "Doe";
  6. String description = "Security Guard, Police Officer";
  7. public Doe (Employee employee2)
  8. {
  9. this.employee = employee;
  10. }
  11. public String getDescription()
  12. {
  13. return description;
  14. }
  15. }

Jill.java (third employee object):

  1. import java.util.*;
  2. public abstract class Doe extends ResponsibilityDecorator
  3. {
  4. Employee employee2;
  5. String lastName = "Doe";
  6. String description = "Security Guard, Police Officer";
  7. public Doe (Employee employee2)
  8. {
  9. this.employee = employee;
  10. }
  11. public String getDescription()
  12. {
  13. return description;
  14. }
  15. }

Maria.java (fourth employee class):

  1. import java.util.*;
  2. public abstract class Maria extends ResponsibilityDecorator
  3. {
  4. Employee employee4;
  5. String lastName = "Maria";
  6. String description = "Receptionist, Valet, Cashier, Restock";
  7. public Maria (Employee employee4)
  8. {
  9. this.employee = employee;
  10. }
  11. public String getDescription()
  12. {
  13. return description;
  14. }
  15. }

James.java (fifth employee class):

  1. import java.util.*;
  2. public abstract class James extends ResponsibilityDecorator
  3. {
  4. Employee employee5;
  5. String lastName = "James";
  6. String description = "Manager, CEO, Economy, President, Analytics";
  7. public James (Employee employee5)
  8. {
  9. this.employee = employee;
  10. }
  11. public String getDescription()
  12. {
  13. return description;
  14. }
  15. }

Test.java:

  1. import java.util.*;
  2. public class Test
  3. {
  4. public static void main(String[] args)
  5. {
  6. Employee employee1 = new Rodriguez();
  7. System.out.println("Rodriguez:" +
  8. employee1.getDescription());
  9. Employee employee2 = new Doe();
  10. System.out.println("Doe" +
  11. employee2.getDescription());
  12. Employee employee3 = new Jill();
  13. System.out.println("Jill:" +
  14. employee3.getDescription());
  15. Employee employee4 = new Maria();
  16. System.out.println("Maria:" +
  17. employee4.getDescription());
  18. Employee employee5 = new James();
  19. System.out.println("James:" +
  20. employee5.getDescription());
  21. }
  22. }

For some reason, it shows a type mismatch error. I 've been searching around the Internet and I even asked some people. I tried many fixes, but they just create another error. I don't know how to fix this problem.

答案1

得分: 0

你正在创建类型为“Employee”的对象实例,
但根据您的代码,它们中没有一个与Employee类存在关系或为其子类。

您定义了什么是Employee和Salary Employee,但您的people对象都未继承它们。

英文:

You are creating instances of objects of Type Employee
But none of them have a relationship or are children of the Employee class according to your code.

You defined what a Employee and Salary employee is, but none of your people objects extends them.

答案2

得分: 0

这里似乎有几个问题。让我们来看一下任务文本:

> 创建一个名为Employee的抽象类,包含姓氏和描述字段,以及一个getDescription()方法。

这看起来没问题。

> 创建一个名为SalariedEmployee的具体类,它继承自Employee。

你将SalariedEmployee定义为了抽象类。移除abstract关键字并实现getDescription方法。

> 创建一个名为ResponsibilityDecorator的抽象类,它能够装饰一个员工并返回员工的责任描述作为字符串。它将拥有一个抽象的getDescription方法。

根据我熟悉的装饰器模式,如果ResponsibilityDecorator应该“装饰一个员工”,它应该继承自Employee。你是否学习了其他内容?

根据任务描述,我期望看到:

  1. abstract class ResponsibilityDecorator extends Employee {
  2. Employee employee;
  3. ResponsibilityDecorator(Employee e) { this.employee = e; }
  4. abstract String getDescription();
  5. }

> 创建一些扩展ResponsibilityDecorator类并实现getDescription()方法的工作类别类。

除非你将Maria、Rodriguez等视为“工作类别”,否则你还没有完成这一步。

一个“经理”工作类别可能如下所示:

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

> 在你的主测试程序中,至少创建5个Employee对象。

也许你将这一步和前面的步骤混淆了,创建了5个员工类(Maria、Rodriguez等)?


请注意,这个练习的解决方案会让专业开发人员感到困惑,因为这不是在程序中表示工作类别或责任的合理方式。

英文:

There seem to be several problems here. Let's go through the assignment text:

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

This looks OK

> Create a concrete class named SalariedEmployee that extends Employee.

You made SalariedEmployee an abstract class. Remove abstract and implement the getDescription method.

> 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.

According to the decorator pattern I'm familiar with, ResponsibilityDecorator should extend Employee if it's supposed to "decorate an employee" - have you been taught something else?

From the description of the task I would expect to see:

  1. abstract class ResponsibilityDecorator extends Employee {
  2. Employee employee;
  3. ResponsibilityDecorator(Employee e) { this.employee = e; }
  4. abstract String getDescription();
  5. }

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

You have not done this, unless you consider Maria, Rodriguez etc "job categories."

A "Manager" job category could look like this:

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

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

Perhaps you confused this step the previous one and created 5 employee classes (Maria, Rodriguez, ...)?


Do note that the solution to this exercise would leave professional developers scratching their head, because this is not a reasonable way to represent job categories or responsibilities in a program.

答案3

得分: 0

一些基本的面向对象编程概念在代码中不匹配。
<br>
abstract class 不能在初始化时使用(就像你所做的那样)
<br> Employee employee1 = new Rodriguez();
<br> 代码行是正确的,但错误源于 Rodriguez
<br>尝试使用以下更改类定义:
<br>public class Rodriguez extends ResponsibilityDecorator
<br>这不会解决问题,因为 Rodriguez_ResponsibilityDecorator(类型)与 Employee(类型)之间没有关系,但这是在理解代码方面更进一步的一步。(仅在 ResponsibilityDecorator 中嵌入了一个 Employee 的实例变量。它可以用于重新设计代码并与 Employee 建立联系,但我怀疑你是否会遵循这种模式。(作为替代解决方案)

<br>通常,所有的 employees 都应该派生自 Employee,然后适当地调整代码将是 public class Rodriguez extends Employee ... 在此之后应该调整代码。(从这里开始)。再次看一下模式。
<br>
<br>还有一个提示,适当的 Name_Rodriguez 不是一个合适的 class def,它更像是一个实例的属性。例如:Employee e1 = new CustomEmployee(&quot;Rodriguez&quot;,&quot;last name&quot;, age_int),而 CustomEmployee 是一个具体的类,它 extends Employee ...(Employee 可以是 abstract 或者 interface

英文:

Some basic OOP concepts are mismatch in the code.
<br>
abstract class cannot be used on initialization (as you did)
<br> Employee employee1 = new Rodriguez();
<br> line code is fine, but the error is derived from Rodriguez class
<br>Try change class def with:
<br>public class Rodriguez extends ResponsibilityDecorator
<br>This will not fix the issue since no relation between Rodriguez_ResponsibilityDecorator(type) and Employee(type) but is a step further in understanding the code.(only have an instance var as Employeeembedded on ResponsibilityDecorator. It could be used to redesign the code and made the link with an Employee, but I doubt that you'll follow the pattern.(noted as an alternate solution)

<br>Usually all employees should derived from Employee then a proper code-adjustment will be public class Rodriguez extends Employee ... and after the code should be adapted.(from here you should start).Just look again on pattern.
<br>
<br>One more hint, proper Name_Rodriguez is not an adequate class def, it's more like a property of an instance. Eg:Employee e1 = new CustomEmployee(&quot;Rodriguez&quot;,&quot;last name&quot;, age_int) and CustomEmployee it's a concrete class which extends Employee ... (Employee could be abstract or interface)

huangapple
  • 本文由 发表于 2020年4月6日 05:25:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/61049718.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定