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

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

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

问题

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

Employee.java:

import java.util.*;

public abstract class Employee 
{
    String lastName, description;

    public String getDescription()
    {
        return description;
    }
}

SalariedEmployee.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 (第一个员工类):

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 (第二个员工类):

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 Jill extends ResponsibilityDecorator 
{
    Employee employee3;
    
    String lastName = "Jill";
    String description = "HR Manager, Recruiter, Team Lead";
    
    public Jill (Employee employee3)
    {
        this.employee = employee;
    }
    
    public String getDescription()
    {
        return description;
    }
}

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

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 (第五个员工类):

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

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

英文:

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:

import java.util.*;

public abstract class Employee 
{
	String lastName, description;

	public String getDescription()
	{
		return description;
	}

}

SalariedEmployee.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 (first employee 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 (second employee 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 (third employee object):

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 (fourth employee 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 (fifth employee 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());
	}
}

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。你是否学习了其他内容?

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

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

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

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

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

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

> 在你的主测试程序中,至少创建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:

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.

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

A "Manager" job category could look like this:

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

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:

确定