我们在一个中型项目(超过6个月的3个开发人员)上工作,需要做出以下决定:我们希望将接口与具体实现分开.第一种是将接口存储在单独的文件中.
我们想进一步分离数据:我们希望在一个.CS文件中添加一个带有接口的项目(CSPROJ)以及带有帮助类的另一个.CS文件(比如此接口中使用的一些公共类,一些枚举等).然后,我们想要另一个项目(CSPROJ),它具有工厂模式,具体的接口实现和其他"工作者"类.
任何想要创建实现此接口的对象的类都必须包含第一个包含接口和公共类的项目,而不是实现本身.
这个解决方案有一个很大的缺点:它将程序集的数量乘以2,因为每个"普通"项目都有一个带有interace的项目和一个带有实现的项目.
你会推荐什么?您认为将所有接口放在一个单独的项目中而不是在其自己的项目中的一个接口上是一个好主意吗?
我想知道某个类的C#扩展方法是否可以作为接口的实现?我有什么:
一个表面:
public interface IEventHandler
{
void Notify(SEvent ev, IEventEmmiter source);
}
Run Code Online (Sandbox Code Playgroud)
实现它的类:
class Sim : IEventHandler
{
/*public void Notify(SEvent ev, IEventEmmiter source)
{
Console.WriteLine("Got notified: " + ev.Name);
}*/
}
Run Code Online (Sandbox Code Playgroud)
还有一个包含扩展方法的类:
public static class ReflectiveEventDispatcher
{
public static void Notify(this IEventHandler handler, SEvent ev)
{
if (handler.GetType().GetMethod("Handle" + ev.Name) != null)
{
// C# WTF?
object[] prms = new object[0];
prms[0] = ev;
handler.GetType().GetMethod("Handle" + ev.Name).Invoke(handler, prms);
}
else
{
throw new System.NotImplementedException("This object doesn't have appropriate handler …Run Code Online (Sandbox Code Playgroud) 有没有一种方法在java中指定,泛型类的类型参数必须是一个接口(而不仅仅是扩展它!)
我想做的是以下内容:
public class MyClass<X extends SomeInterface, Y extends SomeOtherClass & X>
Run Code Online (Sandbox Code Playgroud)
这意味着Y必须是SomeOtherClass的子类并实现X.编译器目前得到的是
X型不是界面; 它不能指定为有界参数
那么,我怎样才能告诉编译器X必须始终是一个接口?
编辑:
好的,我想我稍微简化了我的问题.让我们使用我的实际应用程序域来使其更清晰:
我有一个用于表示图表的API.一图包含节点和边缘对象.所有这三个类都实现了Shape接口.形状可以具有子形状,父形状并且属于图形.
问题是,我需要制作这个API的两个版本:一个只具有基本功能的开源和一个具有更多功能的扩展版本.但是,扩展API必须只提供返回扩展类型的方法(ExtendedDiagram,ExtendedNode,ExtendedEdge和(此处出现问题)ExtendedShape).
所以我有这样的事情:
/* BASIC CLASSES */
public interface Shape<X extends Shape<X,Y>, Y extends Diagram<X,Y>>{
public List<X> getChildShapes();
public X getParent();
public Y getDiagram();
...
}
public class Diagram<X extends Shape<X,Y>, Y extends Diagram<X,Y>> implements Shape<X,Y>{...}
public class Edge<X extends Shape<X,Y>, Y extends Diagram<X,Y>> implements Shape<X,Y>{...} …Run Code Online (Sandbox Code Playgroud) 如果我有一个带有协变类型参数的泛型接口,如下所示:
interface IGeneric<out T>
{
string GetName();
}
Run Code Online (Sandbox Code Playgroud)
如果我定义这个类层次结构:
class Base {}
class Derived1 : Base{}
class Derived2 : Base{}
Run Code Online (Sandbox Code Playgroud)
然后我可以使用显式接口实现在单个类上实现两次接口,如下所示:
class DoubleDown: IGeneric<Derived1>, IGeneric<Derived2>
{
string IGeneric<Derived1>.GetName()
{
return "Derived1";
}
string IGeneric<Derived2>.GetName()
{
return "Derived2";
}
}
Run Code Online (Sandbox Code Playgroud)
如果我使用(非泛型)DoubleDown类并将其强制转换为IGeneric<Derived1>或IGeneric<Derived2>按预期运行:
var x = new DoubleDown();
IGeneric<Derived1> id1 = x; //cast to IGeneric<Derived1>
Console.WriteLine(id1.GetName()); //Derived1
IGeneric<Derived2> id2 = x; //cast to IGeneric<Derived2>
Console.WriteLine(id2.GetName()); //Derived2
Run Code Online (Sandbox Code Playgroud)
但是,转换x为IGeneric<Base>,给出以下结果:
IGeneric<Base> b = x;
Console.WriteLine(b.GetName()); //Derived1 …Run Code Online (Sandbox Code Playgroud) 我读了这个代码,其中接口抛出异常,但实现它的类不会抛出一个或捕获一个,为什么会这样?在java中它是合法的还是安全的?
import java.rmi.*;
public interface MyRemote extends Remote {
public String sayHello() throws RemoteException;
}
import java.rmi.*;
import java.rmi.server.*;
public class MyRemoteImpl extends UnicastRemoteObject implements MyRemote{
public String sayHello() {
return "Server says, 'Hey'";
}
public MyRemoteImpl() throws RemoteException {}
public static void main (String[] args) {
try {
MyRemote service = new MyRemoteImpl();
Naming.rebind("RemoteHello", service);
} catch(Exception ex)
{
ex.printStackTrace();
}
}
}
Run Code Online (Sandbox Code Playgroud) 我编写了一组通过已发布的接口属性相互链接的组件.它们已注册并安装在设计包中.
使用已发布的接口属性在Delphi中并不常见,因此,不出所料,似乎效果不佳.
当组件驻留在同一表单上时,它工作正常,但不同表单上的组件之间的接口属性链接会导致问题.
与指向另一个表单上的组件的对象链接不同,IDE似乎无法识别接口链接.我的意思最好用一个例子来描述,当你在IDE中打开2个表单,并在它们之间有组件之间的链接,然后尝试切换到表单视图时,文本(Alt + F12)将导致IDE正确地抱怨:
Module 'UnitXXX.pas' has open descendents or linked modules. Cannot close.
但是如果属性是一个接口,那么这不会发生,而是发生的事情是链接被切断(当你使用Notification机制清除引用时这是最好的情况,否则你会留下无效的指针)
另一个问题,可能是同一个错误的结果是,当您在IDE中打开项目时,表单将重新打开的顺序是未定义的,因此IDE可以尝试打开包含具有到组件的接口链接的组件的表单另一种形式,但其他形式尚未重新创建.因此,这有效地导致AV或切断链接.
早在90年代,我使用时Datasets,Datasources我记得表格之间的链接消失的类似问题,所以这有点类似.
作为临时解决方法,我添加了重复的已发布属性,对于每个Interface属性,我添加了另一个声明为的属性TComponent.这使得Delphi意识到表单之间存在联系,但至少可以说这是一个丑陋的解决方法.
所以我想知道我能做些什么来解决这个问题?这是一个IDE错误,可能无法直接修复,但也许我可以覆盖某些内容或以其他方式挂接到流机制,以更有效地解决此错误.
我从未如此深入到流机制,但我怀疑Fixup机制应该处理这个问题.有一个,csFixups TComponentState所以我希望可以解决方法.
编辑:使用D2007.
更新:
上传到http://www.filedropper.com/fixupbugproject2的新更新的可重现示例
添加后property ComponentReference: TComponent,可以轻松比较和跟踪界面与组件流.
我将问题缩小到汇编级别,这有点超出我的深度.
在单位的程序GlobalFixupReferences中,classes它调用:
(GetOrdProp(FInstance, FPropInfo) <> 0)
最终执行:
function TInterfacedComponent.GetInterfaceReference: IInterface;
begin
// uncomment the code bellow to avoid exception
{ if (csLoading in ComponentState) and (FInterfaceReference = nil) then
// leave result unassigned …Run Code Online (Sandbox Code Playgroud) 如何编写实现此TypeScript接口的类(并保持TypeScript编译器满意):
interface MyInterface {
(): string;
text2(content: string);
}
Run Code Online (Sandbox Code Playgroud)
我看到了这个相关的答案: 如何让一个类在Typescript中实现一个调用签名?
但这仅在接口仅具有裸功能签名时才有效.如果要实现其他成员(例如函数text2),它将不起作用.
假设我有两个接口:
public interface I1
{
default String getGreeting() {
return "Good Morning!";
}
}
public interface I2
{
default String getGreeting() {
return "Good Afternoon!";
}
}
Run Code Online (Sandbox Code Playgroud)
如果我想实现它们,将使用什么实现?
public class C1 implements I1, I2
{
public static void main(String[] args)
{
System.out.println(new C1().getGreeting());
}
}
Run Code Online (Sandbox Code Playgroud) 我无法理解如何正确地确保nil在这种情况下不存在某些事情:
package main
type shower interface {
getWater() []shower
}
type display struct {
SubDisplay *display
}
func (d display) getWater() []shower {
return []shower{display{}, d.SubDisplay}
}
func main() {
// SubDisplay will be initialized with null
s := display{}
// water := []shower{nil}
water := s.getWater()
for _, x := range water {
if x == nil {
panic("everything ok, nil found")
}
//first iteration display{} is not nil and will
//therefore work, on the second iteration …Run Code Online (Sandbox Code Playgroud) 在java中,当一个接口扩展另一个接口时:
这有Java的主要概念!
编辑:
public interface FiresDragEvents {
void addDragHandler(DragHandler handler);
void removeDragHandler(DragHandler handler);
}
public interface DragController extends FiresDragEvents {
void addDragHandler(DragHandler handler);
void removeDragHandler(DragHandler handler);
void dragEnd();
void dragMove();
}
Run Code Online (Sandbox Code Playgroud)
在eclipse中除了实现的方法之外还有实现标志DragController.
当我鼠标悬停它,它说它实现了方法!
interface ×10
java ×4
c# ×3
generics ×2
covariance ×1
delphi ×1
delphi-2007 ×1
design-time ×1
exception ×1
extends ×1
go ×1
ide ×1
implements ×1
java-8 ×1
null ×1
pointers ×1
types ×1
typescript ×1