简体   繁体   English

二传手与抽象类

[英]Setter with Abstract Class

I'm not sure how to call a setter method that's in an abstract class from another class. 我不确定如何调用另一个类的抽象类中的setter方法。 Here's what I have: 这是我所拥有的:

public abstract class Media {
private String loanedTo;
    public String getLoanedTo() {
        return loanedTo;
    }
    public void setLoanedTo(String loanedTo) {
        this.loanedTo = loanedTo;
    }
    private Date dueOn;
    public Date getDueOn() {
        return dueOn;
    }
    public void setDueOn(Date dueOn) {
        this.dueOn = dueOn;
    }
}

public class Library {
public void loan(Loanable item, String name){
        Calendar calcDueDate = Calendar.getInstance();
        calcDueDate.add(Calendar.DAY_OF_MONTH, item.getDaysToLoan());
        dueOn = calcDueDate.getTime();
        loanedTo = name;
    }
}

How can I set the dueOn and loanedTo variables from Library that doesn't extend Media which can't be instantiated? 如何设置不扩展无法实例化的Media的Library中的Dueton和loanedTo变量? I couldn't find an answer when I searched. 搜索时找不到答案。 Just beginning to learn this stuff. 刚开始学习这些东西。

well, you have to make a concrete class like 好吧,你必须做一个具体的课程,例如

public class Book extends Media {
}

If you never have a concrete subclass of an abstract class, then your abstract class can never be used. 如果您从来没有抽象类的具体子类,那么您的抽象类将永远无法使用。

There may be some misconceptions in play here. 这里可能存在一些误解。

  1. First, your "Abstract" class really is not abstract (there are no unimplemented method signatures). 首先,您的“抽象”类实际上不是抽象的(没有未实现的方法签名)。
  2. Your Library Class will, one way or another, need to reference an object of a class that extends Media 您的库类将需要以某种方式引用扩展Media的类的对象

Check back to the problem you are asked to solve and make sure you are not intended to implement Library as a class that extends Media. 返回到要求您解决的问题,并确保您不打算将Library实现为扩展Media的类。

If you tried to instantiate the abstract Media class, you would fail (because you can't instantiate abstract classes). 如果尝试实例化抽象Media类,则会失败(因为您无法实例化抽象类)。

If you had a concrete class, say Book , and had it extends Media , then you could invoke book.setLoanedTo("Bob") and book.setDueOn(someJavaDate) . 如果您有一个具体的类,请说Book ,并extends Media ,然后可以调用book.setLoanedTo("Bob")book.setDueOn(someJavaDate)

Try something like this: 尝试这样的事情:

 public abstract class Media {
            private String loanedTo;
            public String getLoanedTo() {
                return loanedTo;
            }
            public void setLoanedTo(String loanedTo) {
                this.loanedTo = loanedTo;
            }
            private Date dueOn;
            public Date getDueOn() {
                return dueOn;
            }
            public void setDueOn(Date dueOn) {
                this.dueOn = dueOn;
            }
        }
        public class ConcreteMedia extends Media {
            private String loanedTo;
            public String getLoanedTo() {
                return loanedTo;
            }
            public void setLoanedTo(String loanedTo) {
                this.loanedTo = loanedTo;
            }
            private Date dueOn;
            public Date getDueOn() {
                return dueOn;
            }
            public void setDueOn(Date dueOn) {
                this.dueOn = dueOn;
            }
        }

        public class Library {
            public void loan(Loanable item, String name){
                Calendar calcDueDate = GregorianCalendar.getInstance();
                calcDueDate.add(Calendar.DAY_OF_MONTH, item.getDaysToLoan());
                Media concrete = new ConcreteMedia();
                dueOn = concrete.setDueOn(calcDueDate.getTime());
                loanedTo = concrete.SetLoanedTo(name);
            }
        }

I think your Library.loan method's Item param needs to have the methods you've implemented in abstract Media. 我认为您的Library.loan方法的Item参数需要具有您在抽象Media中实现的方法。 Create an interface Loanable with the method declarations. 使用方法声明创建一个可贷接口。 You can have Media implement the interface and remain abstract if you want. 您可以让Media实现该接口,并根据需要保持抽象。

public interface Loanable {
   String getLoanedTo();
   void setLoanedTo(String loanedTo);
   Date getDueOn();
   void setDueOn();
}


public abstract class Media implements Loanable {
   private String loanedTo;
   public String getLoanedTo() {
      return loanedTo;
   }
   public void setLoanedTo(String loanedTo) {
      this.loanedTo = loanedTo;
   }
   private Date dueOn;
   public Date getDueOn() {
      return dueOn;
   }
   public void setDueOn(Date dueOn) {
      this.dueOn = dueOn;
   }
}


public class Library {
   public void loan(Loanable item, String name){
      Calendar calcDueDate = Calendar.getInstance();
      calcDueDate.add(Calendar.DAY_OF_MONTH, item.getDaysToLoan());
      dueOn = calcDueDate.getTime();
      loanedTo = name;
      item.setDueOn(dueOn);
      item.setLoanedTo(loanedTo);
}

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

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