繁体   English   中英

Java如何知道要使用哪种接口实现?

[英]How does Java know which implementation of an Interface to use?

我对Java如何知道调用方法时将使用哪种接口实现感到好奇。

因此,下面是我难以理解其“神奇”工作原理的代码片段:

接口“ LotListener”:

public interface LotListener
{

    public void bidUpdate( Lot lot, Bid bid);
}

“ LotListener”的实现是BidStatistics和Person。

现在,这里是将所有BidStatistics和Person更改为接口之前的代码:

public class Lot
{

    private HashSet<**Person**> bidders;

    private **BidStatistics** bidStats;


    public Lot(int number, String description, **BidStatistics** bidStats)
    {
        bidders = new HashSet<**Person**>();

        this.bidStats = bidStats;   

    }


    public boolean bidFor(Bid bid)
    {
        **Person** nextBidder;

        bidders.add( bid.getBidder() );
        Iterator iterateBids = bidders.iterator();

        if(highestBid == null) {
            // There is no previous bid.
            highestBid = bid;
            bidStats.bidUpdate( this, bid );
            return true;
        }
        else if(bid.getValue() > highestBid.getValue()) {
            // The bid is better than the previous one.
            highestBid = bid;
            while ( iterateBids.hasNext() )
            {
                nextBidder = (LotListener) iterateBids.next();
                nextBidder.bidUpdate( this, bid );
            }
            bidStats.bidUpdate( this, bid );
            return true;
        }
        else {
            // The bid is not better.
            return false;
        }
    }

现在,当我将所有Person和BidStatistics更改为LogListener接口时:

public class Lot
{
    private HashSet<**LotListener**> bidders;

    private **LotListener** bidStats;


    public Lot(int number, String description, **LotListener** bidStats)
    {
        bidders = new HashSet<**LotListener**>();

        this.bidStats = bidStats;   

    }

    public boolean bidFor(Bid bid)
    {
        **LotListener** nextBidder;

        bidders.add( bid.getBidder() );
        Iterator iterateBids = bidders.iterator();

        if(highestBid == null) {
            // There is no previous bid.
            highestBid = bid;
            bidStats.**bidUpdate**( this, bid );
            return true;
        }
        else if(bid.getValue() > highestBid.getValue()) {
            // The bid is better than the previous one.
            highestBid = bid;
            while ( iterateBids.hasNext() )
            {
                nextBidder = (LotListener) iterateBids.next();
                nextBidder.**bidUpdate**( this, bid );
            }
            bidStats.**bidUpdate**( this, bid );
            return true;            
    }

该代码仍然有效。 我的问题是为什么?

如何知道何时使用Person中的bidUpdate实现以及何时使用BidStatistics中的bidUpdate实现?

编辑:真的很抱歉有关某些代码的**。 我试图用粗体突出显示它,但是我想那是行不通的。

这称为虚拟调度

在声明为接口的变量上调用方法时,Java将在实例的vtable中查找要调用的方法,该vtable是在基于类创建实例时设置的。

因此,它实际上由类调用该对象是其运行时实例的实现定义。

变量的类型是LotListener ,但是变量LotListener对象的类型仍然是PersonBidStatistics

因为它指向该类的实际实例。

不知道 您可以通过提供适当类型的对象来告诉它。

暂无
暂无

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

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