标签: interface

接口实现了不同的接口,导致绑定到 Combobox 的问题

我正在学习 Fluent NHibernate,这个问题来自那个项目。

我有一个基类和一个基接口:

public abstract class Base : IBase
{
    public virtual Guid Id { get; set; }
    public virtual bool IsValid()
    {
        return false;
    }
}

public interface IBase
{
    Guid Id { get; set; }
    bool IsValid();
}
Run Code Online (Sandbox Code Playgroud)

我从以下继承了所有其他域类:

public class Item:Base, IItem
{
    public virtual string Name { get; set; }

    public override bool IsValid()
    {
        <snip>
    }
    <snip>
}

public interface IItem: IBase
{
    string Name { get; set; }
    <snip>
}
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试将所有项目的列表绑定到 winforms Combobox …

c# data-binding design-patterns interface winforms

0
推荐指数
1
解决办法
646
查看次数

在 C 中隐藏类型定义

我有一个.c带有函数和定义的文件(函数库),如下所示:

typedef long double big;
big foo(int x) { ... }
Run Code Online (Sandbox Code Playgroud)

我想创建这个库的一个接口,一个.h. 所以我这样做:

typedef long double big;
big foo(int);
Run Code Online (Sandbox Code Playgroud)

并typedef long double big;从.c文件中删除。但是通过这样做,我放弃了big接口中的类型定义,所以它并不是一个真正干净的接口。任何想法如何解决这个问题?

我知道我可以在我的.c文件中做到这一点:

struct foo {
  long double;
};
Run Code Online (Sandbox Code Playgroud)

然后在.h文件中做:

typedef struct foo big;
big foo(int);
Run Code Online (Sandbox Code Playgroud)

但是struct只为一个字段创建一个似乎很浪费,而且.每当我想读取big.

c struct types typedef interface

0
推荐指数
1
解决办法
964
查看次数

下载未使用 Curl C API 恢复

  1. 我正在尝试恢复因互联网故障而失败的下载。我用来检查 curl 下载成功的函数是:

    curl_multi_info_read
    
    Run Code Online (Sandbox Code Playgroud)

    此函数CURLE_COULDNT_CONNECT在 Internet 丢失时第一次调用时返回正确的错误代码 ( )。如果我再次尝试调用它,它会返回 NULL 指针,这意味着没有消息。实际上,我正在使用返回错误代码来检查是否存在互联网连接。这让我很困扰,因为如果没有互联网,它在第二次调用时不会返回任何错误代码。任何一个可以请告诉我如何利用这个功能来检查返回代码,因为这个错误代码(CURLE_COULDNT_CONNECT)是在检查网络的状态对我来说非常重要,因此继续从停止的地方,当我回来的连接下载。 ...

  2. 为了恢复下载,我正在使用

    curl_easy_setopt (curl, CURLOPT_RESUME_FROM, InternalOffset);
    
    Run Code Online (Sandbox Code Playgroud)

    每次我失去互联网连接时,我都会调用此函数来设置选项,以便在互联网连接恢复时可以恢复下载...


给丹尼尔·斯坦伯格的笔记:

以下是有关平台和 libcurl 版本的一些详细信息:

  • curl 版本 - libcurl 7.21.6
  • 平台 - Linux (Ubuntu)

注释:

  1. 是的。你的观点是对的。我从堆栈中删除了简单句柄,通过设置新选项 ( curl_easy_setopt(curl, CURLOPT_RESUME_FROM, InternalOffset))再次添加到多句柄,最后我做了多执行。如果他们没有互联网连接,它会返回正确的错误。我的问题是:每次当我失去互联网连接时,我是否需要重复上述步骤以获得正确的错误?如果我不执行这些步骤,curl_multi_info_read函数是否会始终返回 NULL。

  2. 我所做的另一项观察是,当互联网连接恢复时,下载开始恢复。它从之前停止的地方开始下载。这对我来说是一个惊喜。当它回到互联网时,curl 在内部负责恢复下载。如果这是正确的吗?我真的需要小心恢复下载还是让它在正确处理时卷曲?

c curl interface libcurl

0
推荐指数
1
解决办法
8923
查看次数

在接口方法定义中将函数作为参数传递

我目前正在尝试使用(可能)C# 的 Func 或 Action 类型来处理 lambda。

我想创建一个名为 IMyInterface 的接口,它定义了一个名为 CreateCRUD 的方法。这应该需要 5 个参数。第一个是字符串。接下来的四个是调用 create、read、update 和 delete 方法的函数。

interface IMyInterface
{
    void CreateCRUD(string name, Action<void> createFunc, Action<void> readFunc, Action<void> updateFunc, Action<void> deleteFunc);
}
Run Code Online (Sandbox Code Playgroud)

四个函数定义不应带任何参数且不返回任何内容。上面的代码不能编译。请指出我正确的方向。

c# parameters lambda interface .net-3.5

0
推荐指数
1
解决办法
1573
查看次数

通过继承和接口实现 Python 多态

我正在使用 Python 2.7(谷歌应用引擎)

学习了Python,我已经弄清楚语言是如何使用继承来实现多态的。

INHERITANCE基于多态性在PYTHON:

class pet:
  number_of_legs = None
  age = None

  def sleep(self):
    print "zzz"

  def count_legs(self):
    print "I have %s legs." % self.number_of_legs
    return self.number_of_legs


class dog(pet):
  def bark(self):
    print "woof"

doug = dog()

doug.number_of_legs = 5;

print "Doug has %s legs." % doug.count_legs()
doug.bark()
Run Code Online (Sandbox Code Playgroud)

我的问题是:Python 是否具有松散耦合的多态性,即使用interface而不是inheritance. 例如,在 Java 中,没有祖先关系的类可以实现相同的接口。见HouseDog和PetRock下面的例子。

INTERFACE基于多态性在JAVA:

class Animal{
   int legs;
   double weight;

   public int getLegs(){return this.legs;} …
Run Code Online (Sandbox Code Playgroud)

python java polymorphism interface

0
推荐指数
1
解决办法
1835
查看次数

在编写可能最终传递任何类型结构的函数时,我可以使用接口作为参数吗?

我正在尝试编写一个最终可能采用任何结构的函数...让我们说它是这样的:

func setDate(s timestamp, data interface{}){
    data.Date = timestamp
}
Run Code Online (Sandbox Code Playgroud)

我意识到我不需要在现实生活中设置这个值的功能,我试图更多地了解接口的工作方式等.

struct interface go

0
推荐指数
1
解决办法
42
查看次数

C# - 一个接口,两个实现:如何将方法调用从一个实现重定向到另一个实现?

我的界面有两个相同名称的实现,如下所示:

interface IDBCaller
{ 
    IEnumerable<User> RetrieveUserList();
    void Method1();
    void Method2();
}

//First implementation of IDBCaller in project1  
class DBCaller : IDBCaller
{

    public IEnumerable<User> RetrieveUserList()
    {
        return new List<User>();
    }

    public void Method1()
    {
        //doing something
    }

    public void Method2()
    {
        //doing something
    }
}
//Second implementation of IDBCaller in project2 
class DBCaller: IDBCaller
{

    public IEnumerable<User> RetrieveUserList()
    {
        return null; //Currently returns null but the desire state is to call the RetrieveUserList in Implementation1 from project 1
    }
    public …
Run Code Online (Sandbox Code Playgroud)

c# interface

0
推荐指数
1
解决办法
84
查看次数

获取通过接口获得的var的指针

在以下代码中

var a int
var b interface{}

b = a

fmt.Printf("%T, %T \n", a, &a)
fmt.Printf("%T, %T \n", b, &b)
Run Code Online (Sandbox Code Playgroud)

输出:

int, *int 
int, *interface {}
Run Code Online (Sandbox Code Playgroud)

我希望&b的类型是int的指针.

我有两个问题:

1)为什么它是接口{}上的指针?

2)我怎样才能获得原始类型的指针?

pointers interface go address-operator

0
推荐指数
1
解决办法
71
查看次数

通过接口了解Java中的异常

我有以下代码,其中包含两个具有相同名称的两个方法的接口。但是,每种方法都会引发不同类型的Exception。

public interface xyz {
void abc() throws IOException;
}
public interface qrs {
void abc() throws FileNotFoundException;
}
public class Implementation implements xyz, qrs {
// insert code
{ /*implementation*/ }
}
Run Code Online (Sandbox Code Playgroud)

我知道在继承中,如果子类方法覆盖超类方法,则子类的throw子句可以包含超类的throws子句的子集,并且它一定不能抛出更多异常。但是,我不确定接口中如何处理异常。

对于abc()类中函数的实现Implementation,此方法可以引发两个异常还是仅引发一个异常?例如,以下方法有效吗?

public void abc() throws FileNotFoundException, IOException
Run Code Online (Sandbox Code Playgroud)

任何见解都表示赞赏。

java oop interface

0
推荐指数
1
解决办法
61
查看次数

具有多个接口的模拟类

我正在尝试测试使用两个不同接口的方法。使用Moq,我可以配置interfaces方法并设置一个return对象,但是无论我设置为Returns,只是执行的第一个方法都返回值,第二个方法返回null。

这是一个例子:

接口1

public interface IUserRepository
{
    User GetUserById(int id);
}
Run Code Online (Sandbox Code Playgroud)

接口2

public interface ICallApiService
{
    ApiResponseDto ValidateUser();
}
Run Code Online (Sandbox Code Playgroud)

我要测试的课程

public class UserServices : IUserServices
{
    private IUserRepository _userRepository;
    private ICallApiService _callApiService;

    public UserServices(IUserRepository userRepository, ICallApiService callApiService)
    {
        _userRepository = userRepository;
        _callApiService = callApiService;
    }
    public User GetUserById(int id)
    {
        //result always have a value set to result
        var result = _callApiService.ValidateUser();

        //result2 is always null
        var result2 = _userRepository.GetUserById(result.UserId);

        return result2;
    }

}
Run Code Online (Sandbox Code Playgroud)

测试方法

[TestMethod]
public void TestMethod1()
{ …
Run Code Online (Sandbox Code Playgroud)

c# unit-testing interface moq .net-core

0
推荐指数
1
解决办法
53
查看次数