有人可以帮助我理解为什么这不起作用:
public interface IInterface
{
string GetString(string start);
void DoSomething();
}
public class InterfaceImpl : IInterface
{
string IInterface.GetString(string start)
{
return start + " okay.";
}
void IInterface.DoSomething()
{
Console.WriteLine(this.GetString("Go")); // <-- Error: InterfaceImpl does not contain a definition for GetString
}
}
Run Code Online (Sandbox Code Playgroud)
我无法弄清楚为什么我不能调用在实现中最明确定义的函数.
谢谢你的帮助.
假设有一个名为A的类,其方法如下:
public void SomeMethod<T>(ref T para1)
{
...
}
Run Code Online (Sandbox Code Playgroud)
现在我有一个接口(让我们称之为ITest)强制执行一个方法(让我们称之为RequiredMethod())并且有一堆实现此接口的类.
在SomeMethod的声明中,我怎么能提到T需要实现ITest以便我可以做这样的事情?
public void SomeMethod<T implements ITest>(ref T para1)
{
...
para1.RequiredMethod();
...
}
Run Code Online (Sandbox Code Playgroud) 我是C#的新手,我不明白为什么我不能这样做.有人可以提供一个可以达到相同结果的链接或示例.
public interface IEffectEditorControl
{
object[] EffectParameterValues
{
get;
set { isDirty = true; }
}
bool isDirty { get; set; }
IEffect TargetEffect { get; set; }
}
Run Code Online (Sandbox Code Playgroud) 我有这个界面(简化)
public interface IImageSourceViewModel : INotifyPropertyChanged
{
/// <summary>
/// Gets or sets the lines view model.
/// </summary>
IObservableCollection<ILine> LinesViewModel { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我有这个:
public class LineViewModel : PropertyChangedBase, ILine
{
}
Run Code Online (Sandbox Code Playgroud)
我实现了如下界面(简体):
public class ImageSourceViewModel : IImageSourceViewModel
{
public IObservableCollection<LineViewModel> LinesViewModel { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
但我得到错误,实现中的LinesViewModel无法在接口中实现LinesViewModel,因为它具有不同的返回类型.
我怎样才能解决这个问题?
编译以下代码时,编译器显示错误:
InterfaceTest.java:19:错误:找不到符号knightObj.dispBK();
public class InterfaceTest{
public static interface Knight{
public void embark();
}
public static class BraveKnight implements Knight{
private int id;
public BraveKnight(int id){
this.id = id;
}
public void dispBK(){
System.out.println("ID: "+id);
}
public void embark(){
System.out.println("ID: "+id);
}
}
public static void main(String[] args){
Knight knightObj = new BraveKnight(101);
knightObj.dispBK();
}
}
Run Code Online (Sandbox Code Playgroud)
可能的原因是什么?
@Override
public void onBackPressed()
{
// ...
dialog.setPositiveButton(getText(R.string.yes), new OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
super.onBackPressed();
}
});
}
Run Code Online (Sandbox Code Playgroud)
在线给我错误super,我完全理解为什么.但我该如何解决呢?
我在Windows 7上的Visual Studio 2013 Pro中使用C#5/.NET 4.5.
我想处理项目集合,其中唯一的要求是项目实现特定的接口.集合中的项目不一定是公共继承树的一部分(即集合不会基于共同的祖先),它们可以是任意类,但都将实现指定的接口(因此目标是基于集合)在他们都实现的通用接口上).
我遇到了麻烦.如果我有一组实现所需接口的项目,我无法将该集合传递给需要基于接口的集合的方法.
这里有一些代码可以使它更具体.
using System.Collections.Generic;
class Test
{
public interface IDrawable
{
void Draw();
}
public class Shape : IDrawable
{
public void Draw() {}
}
public void DrawItems(List<IDrawable> itemsToDraw) {}
public Test()
{
List<Shape> shapes = new List<Shape>();
DrawItems(shapes);
}
}
Run Code Online (Sandbox Code Playgroud)
对DrawItems的调用会生成编译器错误.该方法期望实现IDrawable的项集合.我正在传递一组Shape对象.由于Shape对象确实实现了IDrawable,我希望我可以逃脱这个.
看起来这不起作用,但我想了解原因.我查阅了关于协方差和逆变的信息,但没有找到任何专门针对这种情况的信息.
关于为什么这不起作用的任何想法或信息?
我有一个界面,目前看起来像这样.但我希望它也实现IDisposable
public interface IFoo<out TOut>() where TOut : class, new()
{
}
Run Code Online (Sandbox Code Playgroud)
我想说点什么
public interface IFoo<out TOut>() : IDisposable : where TOut : class, new()
{
}
Run Code Online (Sandbox Code Playgroud)
我该怎么做呢?
当附近发生爆炸时,我正试图代表靠近窗户的人戴眼镜.main是爆炸期间应该做什么的草图.有些东西应该收集爆炸附近的物体清单,并为每个物体做特定的事情(例如粉碎或熔化).玻璃和窗户如预期的那样破碎,但出于某种原因,人类也会破碎.为什么?
package main
import "fmt"
type Human struct { Glasses }
type Glasses struct {}
type Shatterable interface { shatter() }
func (g Glasses) shatter() {}
type Window struct {}
func (w Window) shatter() {}
func main() {
h := Human{Glasses{}}
objectsInProximity := []interface{}{h,h.Glasses,Window{}}
for _,o := range objectsInProximity {
shatter(o)
}
}
func shatter(i interface{}) {
s, ok := i.(Shatterable)
if ok {
fmt.Printf("shattering a %T\n", s)
s.shatter()
}
}
Run Code Online (Sandbox Code Playgroud)
$ go run a.go
shattering a main.Human …Run Code Online (Sandbox Code Playgroud) 我在Scala类中使用Java接口时遇到问题.这是问题所在.
首先,这是简化的Java接口:
java.util.concurrent.TimeUnit;
import Message;
public interface JavaInterface {
public Message get(int param1, Long param2, TimeUnit unit);
}
Run Code Online (Sandbox Code Playgroud)
然后,我有一个尝试使用此接口的Scala类
java.util.concurrent.TimeUnit
import Message
class ScalaClass extends JavaInterface {
def get(param1:Int, param2: Long, unit:TimeUnit):Message = {
new Message("Just a test")
}
}
Run Code Online (Sandbox Code Playgroud)
但是当它编译时,Scala编译器一直抱怨scalaClass中没有实现get()函数:
类ScalaClass需要是抽象的,因为:[error]它有1个未实现的成员.[error]/**从类ScalaClass中可以看出,缺少的签名如下所示.[错误]*为方便起见,这些可用作存根实现.[错误]*/[错误] def get(x $ 1:Int,x $ 2:Long,x $ 3:java.util.concurrent.TimeUnit):Message = ???
我怀疑这可能是因为Scala不喜欢Java int,但是我无法让它工作,有没有人有任何想法?
谢谢你