简体   繁体   English

将对象传递给抽象类?

[英]Passing an object to an abstract class?

I am taking a programming course at the local college. 我正在当地大学上编程课程。 My instructor doesn't speak the best English, so sometimes it's hard to understand what he's asking for. 我的老师不会说最好的英语,所以有时候很难理解他的要求。

The first part of the assignment, which I have already completed, is as follows: A mechanic shop has 100 customers, each of which is an object in an array, which holds various data. 我已经完成了分配的第一部分,如下所示:一家机械厂有100个客户,每个客户都是一个数组中的对象,其中包含各种数据。 The Base class is Service, and there are four derived classes for specific services (tires, oil, radiator, and brakes). 基本类是服务,针对特定服务(轮胎,机油,散热器和制动器)有四个派生类。 Each customer is assigned one service. 每个客户都被分配一项服务。 A list of all customers with the details is printed. 将列出所有客户的详细信息。

The second part, I do not understand what he is asking for at all: We are printing coupons if the last service was done over a certain number of months ago. 第二部分,我根本不理解他的要求:如果最后一次服务是在几个月前完成的,我们正在打印优惠券。 I have already made calculations for the dates, and if the service was done a certain amount of time ago, I need to pass one object in the service array to the abstract coupon class. 我已经对日期进行了计算,如果服务是在一定时间之前完成的,则需要将服务数组中的一个对象传递给抽象优惠券类。 His exact instructions are these: 他的确切指示如下:

Coupon is an Abstract parent class. 优惠券是Abstract的父类。 The abstract class should have variable year, month and day. 抽象类应具有可变的年,月和日。 Make a Service Coupon extend the Abstract coupon class. 制作服务优惠券可扩展Abstract优惠券类。 Several child classes - BrakeService Coupon, Tire Coupon, Engine Oil Coupon, Radiator Coupon extend the Service Coupons. 几个子类-BrakeService优惠券,轮胎优惠券,机油优惠券,散热器优惠券扩展了服务优惠券。 Add an Expiry date to a random date in 2013 and Make it final in the constructor of the objects. 将到期日期添加到2013年的随机日期中,并在对象的构造函数中将其设置为最终日期。 The service object calls the coupon passing the service object. 服务对象调用传递服务对象的优惠券。 The coupon determines the type of service object it receives and accordingly prints the message. 优惠券确定其接收的服务对象的类型,并相应地打印消息。 This should be simple. 这应该很简单。

Here is what I think it is asking for (purely as an exercise for demonstrating we know how to use abstract and inheritance): 这是我认为所要求的(纯粹是作为演示我们知道如何使用抽象和继承的练习):

http://www.java-forums.org/attachments/new-java/4632-passing-object-abstract-class-rtc3ild.png http://www.java-forums.org/attachments/new-java/4632-passing-object-abstract-class-rtc3ild.png

Am I supposed to be passing the object to the abstract class? 我应该将对象传递给抽象类吗? If so, am I supposed to take my main class and make it an extension of the abstract coupon class? 如果是这样,我是否应该参加我的主班并将其扩展为抽象优惠券类?

What I have now is the abstract Coupon class extending the concrete Service class, with the ServiceCoupon class extending Coupon, and four concrete classes extending that. 我现在拥有的是抽象的Coupon类,它扩展了具体的Service类,而ServiceCoupon类则扩展了Coupon,还有四个具体的类对此进行了扩展。 I am not sure how to call the Coupon class or pass it the object from its base class though. 我不确定如何调用Coupon类或将其从基类传递给对象。

Sorry that I can't be more clear. 对不起,我不清楚。 I don't know what he wants either. 我也不知道他想要什么。

edit: **The main problem I am having is that I want to pass an object to a different class that is abstract. 编辑: **我遇到的主要问题是我想将一个对象传递给另一个抽象的类。 I cannot instantiate this class because it is abstract, so I do not know how to pass an object to the method. 我无法实例化此类,因为它是抽象的,因此我不知道如何将对象传递给方法。

I think what he wants is a factory pattern, ie you pass in your Service and the factory determines the type of Service and then returns the appropriate Coupon implementation. 我想他要的是一个工厂模式,即你在你的传递Service ,工厂决定的类型Service ,然后返回相应的Coupon的实现。 Something based on a Map would probably do the trick 基于Map东西可能会解决问题

final Map<Class<? extends Service>, Class<? extends Coupon>> myMap;

public Coupon getCoupon(final Service service) {
  final Coupon coupon = myMap.get(service.getClass()).newInstance();
  //init stuff
  return coupon;
}

Obviously this would only work if you had a noargs constructor for your Coupon . 显然,这只有在您为Coupon使用noargs构造函数时才有效。

Coupon should not extend the Service class. 优惠券不应扩展Service类。 You should have two inheritance trees, one for coupon and one for services. 您应该有两棵继承树,一棵用于优惠券,一棵用于服务。

I Guess the second part of question is asking for something like this: 我猜问题的第二部分是要求这样的事情:

import java.util.*;
abstract class Coupon
{
    int year;
    int month;
    int day;
}
class Service extends Coupon
{
    public String getCouponMessage(Coupon coupon)
    {
        if (coupon instanceof BrakeService)
        {
            return "BreakService message";
        }
        else if (coupon instanceof Tire)
        {
            return "TireService message";
        }
        else if (coupon instanceof Engine_Oil)
        {
            return "Engin_Oil Message";
        }
        else if (coupon instanceof Radiator)
        {
            return "Radiator Message";
        } 
        else
        {
            return "Invalid Service";
        }
    }
}
class BrakeService extends Service
{
    final Date exp_date;
    public BrakeService(int day,int month , int year)
    {
        Calendar cal = Calendar.getInstance();
        this.day = day;
        this.month=month;
        this.year=year;
        cal.set(year, month, day);
        exp_date = cal.getTime();
    }
}
class Tire extends Service
{
    final Date exp_date;
    public Tire(int day,int month , int year)
    {
        Calendar cal = Calendar.getInstance();
        this.day = day;
        this.month=month;
        this.year=year;
        cal.set(year, month, day);
        exp_date = cal.getTime();
    }
}
class Engine_Oil extends Service
{
    final Date exp_date;
    public Engine_Oil(int day,int month , int year)
    {
        Calendar cal = Calendar.getInstance();
        this.day = day;
        this.month=month;
        this.year=year;
        cal.set(year, month, day);
        exp_date = cal.getTime();
        System.out.println(exp_date);
    }
} 
class Radiator extends Service
{
    final Date exp_date;
    public Radiator(int day,int month , int year)
    {
        Calendar cal = Calendar.getInstance();
        this.day = day;
        this.month=month;
        this.year=year;
        cal.set(year, month, day);
        exp_date = cal.getTime();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM