我们都知道你不能在 Java 中实例化一个接口(至少直接实例化)。
但是有这样的事情:
public class Test{
public interface Link {
void mySamplemethod();
String myString ="HELLO!";
}
public static void main(String []args){
Link b;
}
}
Run Code Online (Sandbox Code Playgroud)
b……究竟是什么,它怎么可能有实际用途?
我得到,“找不到类型或命名空间名称‘INRBQDeliveryRepository’(您是否缺少 using 指令或程序集引用?)”在这里:
public class DeliveryController : ApiController
{
private readonly INRBQDeliveryRepository _deliveryRepository;
Run Code Online (Sandbox Code Playgroud)
我试图引用的接口类型是:
namespace SeaStore.Data.Legacy.Interfaces
{
public interface INRBQDeliveryRepository
. . .
Run Code Online (Sandbox Code Playgroud)
这些是解决方案中的不同项目(NRBQ.Web 正在尝试访问其兄弟 SeaStore.Data.Legacy);添加“使用 SeaStore.Data.Legacy.Interfaces;” 使用“未找到”错误阻止编译的类没有帮助 - 事实上,我从上面提到的错误转到获得附加错误,特别是:
“类型或命名空间名称 'Data' 在命名空间 'SeaStore' 中不存在(您是否缺少程序集引用?) ”在此处的 Controller 类中:
using SeaStore.Data.Legacy.Interfaces;
Run Code Online (Sandbox Code Playgroud)
为什么我的其他项目界面不可见?
我正在尝试理解该compareTo方法。我写了这个类AboutcompareTo,但我被困在为什么/如何得到这个错误? - 代码快完成了。任何人都可以详细解释我做错了什么。谢谢
编码:
public class AboutCompareTo {
public static void main(String[] args) {
Fruit[] fruits = { new Fruit(2), new Fruit(3), new Fruit(1) };
java.util.Arrays.sort(fruits);
}
}
class Fruit implements Comparable<Fruit> {
private double weight;
public Fruit(double weight) {
this.weight = weight;
}
@Override
public int compareTo(Fruit o) {
Fruit f = (Fruit) o;
if (Fruit > o.Fruit) // <-- the error
return 1;
else if ((Fruit < o.Fruit)) // <-- the error
return -1; …Run Code Online (Sandbox Code Playgroud) 好的!这已经让我紧张了几个小时。
我正在使用提供的程序集,该程序集具有我需要实现的接口
public interface ICustomInterface
{
CustomType DoSomething(string name);
}
Run Code Online (Sandbox Code Playgroud)
在我的代码中,我喜欢这样:
public class MyClass: ICustomInterface
{
public MyClass()
{
}
// now I should implement the interface like this
public CustomType DoSomething(string name)
{
CustomType nType = new CustomType();
// do some work
return nType;
}
}
Run Code Online (Sandbox Code Playgroud)
到目前为止一切顺利,但在 MyClass 中的接口实现中,我需要使用,async await因此实现应该是这样的:
public class MyClass: ICustomInterface
{
public MyClass()
{
}
// now I should implement the interface like this
public async Task<CustomType> DoSomething(string name)
{
CustomType nType = …Run Code Online (Sandbox Code Playgroud) 我正在开发一个程序,我想使用接口。类似于 Bukkit 在他们的 Player 类中使用的方式,这里。我已经多次观看和阅读有关界面的教程,甚至尝试过自己测试但没有运气。
假设我有一个“用户”界面,带有一个返回字符串的方法占位符。该方法将命名为 getName()。
我想要做
User user = (INSTANTIATE SOMEHOW);
System.out.println(user.getName());
Run Code Online (Sandbox Code Playgroud)
这是在 Bukkit 的实现中以大致相同的方式实现的
Player player = Server.getPlayer("Jimmothy");
System.out.println(player.getName());
Run Code Online (Sandbox Code Playgroud)
我只是不知道如何让这样的事情发挥作用。为了实例化播放器类,您可以使用许多“getPlayer()”方法之一。唯一的问题是包含这些方法的类也是接口,所以我也不知道如何获取接口的实例。搜索代码也无济于事,因为我似乎无法找到这些类中的方法在哪里以及是否实际上是使用 Overrides 或其他东西构建的。(如果这就是覆盖的工作方式)。
谁知道呢,也许我只需要知道如何实例化或获取用户界面的实例,我就准备好了。我知道您不能使用“new ClassName();”创建接口实例。我从来没有真正理解他们,但我真的很想。
这似乎是一个非常愚蠢的问题,我相信答案非常简单,但帮助会很棒!:) 感谢您的阅读。
这是界面
public interface ITransporter
{
TransporterType TransporterType { get; set; }
TransportActionResults BeginTransport(
Project sourceProject,
ProjectBase targetProject);
bool CheckTargetExists(ProjectBase project);
}
Run Code Online (Sandbox Code Playgroud)
这ProjectBase是所有类型的项目类的抽象类。这是实现ITransporter接口的类中的方法。
public class LocalTransporter : ITransporter
{
public TransportActionResults BeginTransport(
Project sourceProject,
TargetLocalProject targetProject)
{
return TransportActionResults.Success;
}
Run Code Online (Sandbox Code Playgroud)
这个TargetLocalProject类继承自ProjectBase类。但是,我收到一条错误消息,指出“LocalTransporter 类未实现接口成员”并指向该BeginTransport方法。
这是我的问题。我需要BeginTransport使用基类来实现接口的方法,ProjectBase因为从基类继承了几个不同的项目类,我们不知道将使用哪一个。
此外,有几种不同的Transporter类型,因此我需要使用 interface 与它进行交互ITransporter。
注意:我考虑使用ProjectBase而不是实现方法,TargetLocalProject但是当LocalTransporter直接使用(而不是通过接口)时,参数的类型应该是特定的项目类型而不是基类。
我希望我说清楚了。那么,有没有办法在接口中使用基类但在实现的方法中使用子类来实现这个接口方法?
首先让我们考虑以下几点:
func process(body io.Reader) {
fmt.Printf("body == nil ? %+v\n", body == nil)
}
func main() {
var body *bytes.Buffer
fmt.Printf("body == nil ? %+v\n", body == nil)
process(body)
process(nil)
}
Run Code Online (Sandbox Code Playgroud)
这是输出:
body == nil ? true
body == nil ? false // Did you get this right?
body == nil ? true
Run Code Online (Sandbox Code Playgroud)
另一个例子:
type Container struct {
Reader io.Reader
}
func processContainer(container Container) {
fmt.Printf("container.Reader == nil ? %+v\n", container.Reader == nil)
}
func main() {
var body *bytes.Buffer …Run Code Online (Sandbox Code Playgroud) 我在 Java 中实现了两种类型的列表,一种只是标签列表,另一种在每行上提供两个按钮。
我创建了一个名为 IListView 的接口,它包含以下方法:
public void setDataSource(IListViewDataSource dataSource);
Run Code Online (Sandbox Code Playgroud)
问题在于,在创建第二种类型的列表时,它使用另一种称为IComplexListViewDataSource扩展前一种类型的数据源,并像这样实现前一种方法:
public void setDataSource(IComplexListViewDataSource dataSource);
Run Code Online (Sandbox Code Playgroud)
IntelliJ 告诉我我没有覆盖IListView接口中的方法,即使IComplexListViewDataSourceextends IListViewDataSource.
如果我是对的,如果我处理的是类而不是接口,这将毫无问题地工作。这是怎么回事,我怎样才能让它发挥作用?
几天前我开始学习 golang,发现reflect.Valueof()和Value.Elem()很混乱。这两个函数/方法有什么区别以及如何正确使用它们?
两个函数/方法都返回一个值,并根据 go doc
ValueOf 返回一个初始化为接口 i 中存储的具体值的新值。ValueOf(nil) 返回零值。
Elem 返回接口 v 包含的值或指针 v 指向的值。如果 v 的 Kind 不是 Interface 或 Ptr,它会发生恐慌。如果 v 为零,则返回零值。
我从 stackoverflow 上的一篇文章中找到了这段代码,但仍然不明白何时使用 .Elem()
func SetField(obj interface{}, name string, value interface{}) error {
// won't work if I remove .Elem()
structValue := reflect.ValueOf(obj).Elem()
structFieldValue := structValue.FieldByName(name)
if !structFieldValue.IsValid() {
return fmt.Errorf("No such field: %s in obj", name)
}
if !structFieldValue.CanSet() {
return fmt.Errorf("Cannot set %s field value", name)
}
structFieldType := …Run Code Online (Sandbox Code Playgroud) 我正在参加正式巡演。今天,我在做rot13reader 练习时遇到了一些奇怪的事情。
问题是当我使用break而不是return ttl, io.EOF,程序进入无限循环。但是,据我所知,在这个程序中,应该没有任何区别,break或者return ttl, io.EOF因为如果是break,下一行将return ttl, err在Read()方法的末尾,这与return ttl, io.EOF.
我想知道为什么。与 Go 如何处理 io.Reader 接口及其实现的底层机制有关吗?
这是代码。
package main
import (
"io"
"os"
"strings"
)
type rot13Reader struct {
r io.Reader
}
func (rr *rot13Reader) Read(b []byte) (n int, err error) {
rb := make([]byte, 8)
var ttl int
for {
n, err := rr.r.Read(rb)
if err == io.EOF {
return ttl, …Run Code Online (Sandbox Code Playgroud) interface ×10
java ×4
c# ×3
go ×3
pointers ×2
async-await ×1
break ×1
bukkit ×1
compareto ×1
inheritance ×1
null ×1
polymorphism ×1
reflection ×1
solution ×1