简体   繁体   English

如何使用超类调用泛型方法

[英]How to call a generic method using super class

I have got some methods that are common to two subclasses so i have put them in Abstract superclass but there is one method that uses a variable with two different values so i am confused how to implement that method the code is like: 我有两个子类共有的一些方法,所以我将它们放在Abstract超类中,但是有一个方法使用具有两个不同值的变量,所以我很困惑如何实现该方法,代码如下:

StandardMember class got this method with its own different value for remainingCredit=30 at starting StandardMember类在开始时使用其自己的值作为剩余的Credit = 30获得了该方法的不同值

public void borrowHolding(int holdingId)
            throws InsufficientCreditException, MultipleBorrowingException {
        // TODO Auto-generated method stub

        System.out.println("Hello");
        Holding tempHolding = Library.libCollection.getHolding(holdingId);
        if (Library.libCollection.getHolding(holdingId) != null) {
            if (tempHolding.isOnLoan()) {
                System.out.println("Can not be issued Currently on Load");
            } else {
                System.out.println("Can be issued");
                remainingCredit-=tempHolding.getDefaultLoanFee();
                System.out.println(getRemainingCredit());
                tempHolding.setLoanCheck(true);
                currentlyBorrowedHolding.put(holdingId, tempHolding);
                System.out.println(remainingCredit);
                System.out.println(holdingId);
            }
        }

PremiumMember Class got same method but the value of remainingCredit is 45 , Whereas all the methods common to them are implemented in this AbstractMember class which implements the Member interface . PremiumMember类具有相同的方法,但是剩余信用的值为45 ,而所有通用方法都在实现Member接口的 AbstractMember类中实现。 but when i try to call these method from other class i have to initialize the object of AbstractMember in Library Class like this 但是当我尝试从其他类调用这些方法时,我必须像这样在Library类中初始化AbstractMember的对象

Member member = new StandardMember();

which is very bad because I can not use the StandardMember object to run the PremiumMember Object's version of same method. 这是非常糟糕的,因为我无法使用StandardMember对象运行同一方法的PremiumMember对象版本。 so, either i should create new object of PremiumMember Class or i dont know what to do. 因此,要么我应该创建PremiumMember类的新对象,要么不知道该怎么办。 but if i create two objects then this member object is being used in borrowHolding method which is basically a cascading method in Library Class which in turn calls the borrowHolding method in Member Interface: 但是,如果我创建了两个对象,则此成员对象将在牢牢在类库中的级联方法中的heroHolding方法中使用,而该方法又在成员接口中调用roweHolding方法:

public void borrowHolding(int holdingId) throws InsufficientCreditException, MultipleBorrowingException {
        if(libCollection.holdingMap==null){
            System.out.println("Collection is Empty");
        }
        if(libCollection.holdingMap.containsKey(holdingId))
        member.borrowHolding(holdingId);        
    }

the problem is i can not create two objects because at runtime i can only call one method. 问题是我无法创建两个对象,因为在运行时我只能调用一个方法。 so help me out how to implement this method in Abstract class so that program will detect the difference that which object it should create. 因此,请帮助我了解如何在Abstract类中实现此方法,以便程序可以检测到应该创建哪个对象的区别。

If I understood you correctly, you have a Member abstract class with a field remainingCredit which has to be 30 in one subclass and 45 in another. 如果我对您的理解正确,则您有一个Member抽象类,其字段remainingCredit必须在一个子类中为30在另一个子类中为45

Use a protected constructor. 使用受保护的构造函数。

public abstract class Member {

    private int remainingCredit;
    // Other members
    // getters and setters

    protected Member(String memId, String memName, int remainingCredit) {
        this.memId = memId;
        this.memName = memName;
        this.remainingCredit = remainingCredit;
    }
}

public StandardMember extends Member {

    public StandardMember(String memId, String memName) {
        super(memId, memName, 30);
    }
}

public PremiumMember extends Member {

    public PremiumMember(String memId, String memName) {
        super(memId, memName, 45);
    }
}

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

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