gpt4 book ai didi

java - 实例化接口(interface)对象 - 如何?

转载 作者:行者123 更新时间:2023-12-01 10:17:17 25 4
gpt4 key购买 nike

遇到这行代码,它定义了 Cursor 类型的变量 - 一个接口(interface) - 在类 - CursorWrapper - 实现该接口(interface)中:

protected final Cursor mCursor;

然后这是该类的构造函数:

public CursorWrapper(Cursor cursor) { mCursor = cursor; }

这是怎么回事?似乎正在实例化一个接口(interface),但我认为你无法实例化接口(interface)。

最佳答案

What is going on here?

嗯,CursorWrapper 正在接受 Cursor 类型的对象作为构造函数参数,然后将其分配给一个字段。

It seems like an interface is being instantiated

您的语句都没有实例化任何内容。

but I thought you couldn't instantiate an interface

你不能。您可以实例化一个实现该接口(interface)的类。

<小时/>

CursorWrapper 包装 CursorCursor 是一个接口(interface)。有几个类实现了 Cursor。开发人员可以从几个地方获取 Cursor,主要是通过数据库查询和 ContentResolver 查询。

但是,例如the honey badger ,CursorWrapper 不在乎。 CursorWrapper 会愉快地包装任何 Cursor 实现。 CursorWrapper 本身是相当无用的;它旨在通过子类化 CursorWrapper 来更轻松地创建自定义包装器,就像我对 my LegacyCompatCursorWrapper 所做的那样。 .

现在,如果 CursorWrapper 只能包装来自数据库的 Cursor,那么它不应该将 Cursor 作为参数,而是一个SQLiteCursor

关于java - 实例化接口(interface)对象 - 如何?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35821890/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com