我有一个第三方C++ DLL,我从C#调用.
这些方法是静态的.
我想抽象它来做一些单元测试,所以我创建了一个带有静态方法的接口,但现在我的程序错误:
修饰符'static'对此项无效
MyMethod cannot be accessed with an instance reference; qualify it with a type name instead
Run Code Online (Sandbox Code Playgroud)
我怎样才能实现这种抽象?
我的代码看起来像这样
private IInterfaceWithStaticMethods MyInterface;
public MyClass(IInterfaceWithStaticMethods myInterface)
{
this.MyInterface = myInterface;
}
public void MyMethod()
{
MyInterface.StaticMethod();
}
Run Code Online (Sandbox Code Playgroud) 目前,TypeScript不允许在接口中使用get/set方法(访问器).例如:
interface I {
get name():string;
}
class C implements I {
get name():string {
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
此外,TypeScript不允许在类方法中使用Array Function Expression:例如:
class C {
private _name:string;
get name():string => this._name;
}
Run Code Online (Sandbox Code Playgroud)
有没有其他方法可以在接口定义上使用getter和setter?
我正在阅读"The Java Tutorial"(第二次).我刚刚完成了关于接口的部分(再次),但仍然不了解Java接口如何模拟多重继承.是否有比书中更清楚的解释?
有人可以向我解释一下编译器在第一次投射时不会抱怨,但是在第二次投射中是否会抱怨?
interface I1 { }
interface I2 { }
class C1 implements I1 { }
class C2 implements I2 { }
public class Test{
public static void main(){
C1 o1 = new C1();
C2 o2 = new C2();
Integer o3 = new Integer(4);
I2 x = (I2)o1; //compiler does not complain
I2 y = (I2)o3; //compiler complains here !!
}
}
Run Code Online (Sandbox Code Playgroud) 可能重复:
C#:接口 - 隐式和显式实现
有人会解释这两种野兽之间的差异以及如何使用它们.AFAIK,许多pre.2.0类在没有泛型类型的情况下实现,因此导致后一版本实现这两种接口.是否需要使用它们的唯一情况?
你能否深入解释如何使用它们?
谢谢
我是一个从事一个相当大的项目的团队的新手,拥有大量的组件和依赖项.对于每个组件,都有一个interfaces包放置了该组件的公开接口.这是一个好习惯吗?
我通常的做法一直是接口和实现在同一个包中.
我在PHP 7中使用返回类型提示遇到了一些问题.我的理解是提示: self意味着你打算让一个实现类返回自己.因此我: self在我的界面中使用来表示,但是当我尝试实际实现接口时,我遇到了兼容性错误.
以下是我遇到的问题的简单演示:
interface iFoo
{
public function bar (string $baz) : self;
}
class Foo implements iFoo
{
public function bar (string $baz) : self
{
echo $baz . PHP_EOL;
return $this;
}
}
(new Foo ()) -> bar ("Fred")
-> bar ("Wilma")
-> bar ("Barney")
-> bar ("Betty");
Run Code Online (Sandbox Code Playgroud)
预期的产出是:
弗雷德威尔玛巴尼贝蒂
我实际得到的是:
PHP致命错误:声明Foo :: bar(int $ baz):Foo必须与iFoo :: bar(int $ baz)兼容:第7行的test.php中的iFoo
问题是Foo是iFoo的一个实现,所以据我所知,实现应该与给定的接口完全兼容.我可以通过更改接口或实现类(或两者)来修复此问题,以通过名称而不是使用返回提示接口self,但我的理解是语义上self意味着"返回您刚刚调用方法的类的实例" ".因此,将其更改为接口意味着理论上我可以返回实现接口的任何实例,而我的意图是调用实例将返回的内容.
这是PHP的疏忽还是这是一个刻意的设计决定?如果它是前者有任何机会看到它在PHP 7.1中修复?如果没有,那么正确的返回提示是什么,你的界面希望你返回刚刚调用方法的实例进行链接的正确方法是什么?
是否可以在Java中创建接口实例?
在某处我读过使用内部匿名类,我们可以这样做,如下所示:
interface Test
{
public void wish();
}
class Main
{
public static void main(String[] args)
{
Test t=new Test()
{
public void wish()
{
System.out.println("output: hello how r u");
}
};
t.wish();
}
}
cmd> javac Main.java
cmd> java Main
output: hello how r u
Run Code Online (Sandbox Code Playgroud)
这是正确的吗?
亲爱的开发者,
我有这个问题,这对我来说似乎有点奇怪.看一下这段代码:
package coreinterfaces
type FilterInterface interface {
Filter(s *string) bool
}
type FieldFilter struct {
Key string
Val string
}
func (ff *FieldFilter) Filter(s *string) bool {
// Some code
}
type FilterMapInterface interface {
AddFilter(f *FilterInterface) uuid.UUID
RemoveFilter(i uuid.UUID)
GetFilterByID(i uuid.UUID) *FilterInterface
}
type FilterMap struct {
mutex sync.Mutex
Filters map[uuid.UUID]FilterInterface
}
func (fp *FilterMap) AddFilter(f *FilterInterface) uuid.UUID {
// Some code
}
func (fp *FilterMap) RemoveFilter(i uuid.UUID) {
// Some code
}
func (fp *FilterMap) GetFilterByID(i uuid.UUID) …Run Code Online (Sandbox Code Playgroud)