标签: interface

我可以通过类的构造函数实例化接口的对象吗

我有一个这样的程序代码。

我的界面是

public interface MyInterface {
        public void m1(String name);
        public void m2(int num);
}
Run Code Online (Sandbox Code Playgroud)

我有一个实现上述接口的类。

    public class World implements MyInterface {
        public void m1(String name) {
               System.out.println(name);
        }

        public void m2(int num){
              System.out.println("Number is: "+num);
        }

        public static void main(String args[]) {
                MyInterface ob1  = new World(); //How it is instantiated
                MyInterface ob2  = new World();   //How this one too is instantiated
                ob1.m1("Jaguar"); 
                ob2.m2(5);

        }
}
Run Code Online (Sandbox Code Playgroud)

java constructor interface instantiation

-1
推荐指数
1
解决办法
1510
查看次数

将对象转换为接口返回 null

我有一个来自类的对象,它返回一个接口。但是当我将对象转换为该接口时,它返回 null。问题是什么?

public partial class CompanyConfigurations : SgConfigurationControl, `IWizardControl`

CompanyConfigurations c
c as IWizardControl
Run Code Online (Sandbox Code Playgroud)

c# casting interface

-1
推荐指数
1
解决办法
2341
查看次数

如何将int值附加到android中的改装的GET url

我有以下网址 http://54.169.227.89:90/DataAccessService.svc/GetProducts/2 我想使用我改装获取方法如何追加2这是我在登录时从sharepreference获得的公司ID

这是ma接口代码

public interface ProductsGet {


    String Company_id = OrderApplication.CompanyID_New;


    @GET("/DataAccessService.svc/GetProducts/")
    public void getProducts( Callback<List<ProductsNew>> response);
}
Run Code Online (Sandbox Code Playgroud)

android interface get-method retrofit

-1
推荐指数
1
解决办法
3695
查看次数

没有实现的接口没有编译错误

在最小的奇怪,但我相信存在一个解释...我有一个接口(IRepository)有6个方法由类实现.但是当我把代码用于实现接口时,VS2015并没有向我显示实现类的选项,如果我没有手动实现并编译项目它不会显示编译错误,不应该吗?如果它出现编译错误告诉我,我没有实现接口.

接口:

 public interface IRepository<T> where T : class
{       
    IQueryable<T> GetAll ();
    IQueryable<T> FindBy ( Expression<Func<T, bool>> predicate );
    void Add ( T entity );
    void Delete ( T entity );
    void Edit ( T entity );
    void Save ();
}
Run Code Online (Sandbox Code Playgroud)

应该实现IRepository但不实现的类,它不会抛出编译错误:

public class GenericRepository<T> where T : class, IRepository<T>
{
}
Run Code Online (Sandbox Code Playgroud)

c# interface visual-studio-2015

-1
推荐指数
1
解决办法
62
查看次数

C#实现与事件的接口

我有和界面如下所示:

public delegate void ScriptFinished();

public interface IScript
{
    event ScriptFinished ScriptFinishedEvent;
}
Run Code Online (Sandbox Code Playgroud)

现在我想实现如下所示的界面:

public class Script : IScript
{
    event ScriptFinished ScriptFinishedEvent;
}
Run Code Online (Sandbox Code Playgroud)

但我得到错误:

'Script.ScriptFinishedEvent'无法实现接口成员,因为它不是公共的.

好的,我添加public到活动中:

public interface IScript
{
    public event ScriptFinished ScriptFinishedEvent;
}
Run Code Online (Sandbox Code Playgroud)

但错误是:

修饰符'public'对此项无效

我在这里做错了什么,如何实现与事件的接口?

c# interface

-1
推荐指数
1
解决办法
259
查看次数

如何在不破坏客户端的情况下在C#中的接口中为方法添加参数?

我在C#应用程序中编写了一个公共接口,它具有以下方法:

IMimic OpenMimic(string mimicPath, string frameModel, string branch, int regionId);
Run Code Online (Sandbox Code Playgroud)

我还编写了一个实现此接口的抽象基类,同时,我编写了一个实现此基类的公共密封类.

现在,我需要为此方法添加一个参数,如下所示:

IMimic OpenMimic(string mimicPath, string frameModel, string branch, int regionId, IAlsContext mimicContext);
Run Code Online (Sandbox Code Playgroud)

这意味着我需要对我的基类和密封实现进行更改.

现在,有一些客户端应用程序(由我组织中的其他团队编写)使用此类并调用此方法的旧版本.如果我改变这个方法,我也被迫改变界面.

这不会打破客户吗?这是一个典型的突破性变化的例子吗?我该如何修复它以便客户不会破坏?问题是这个新参数是必需的参数,我不是接口编程的专家.

谢谢,

c# interface

-1
推荐指数
1
解决办法
221
查看次数

在Golang中为switch中的类型实现逻辑OR

我有一个[]interface{}迭代,并检查交换机中每个元素的类型.我想为几种数字类型添加一个'catch-all'的情况,即int || float32 || float64.

我们似乎能够检查一个元素是否是一个单独的不同类型,但我无法弄清楚用||(或)检查多个类型的语法.

这可能吗?我试过的(游乐场):

package main

import (
    "fmt"
)

func main() {

    things := []interface{}{"foo", 12, 4.5, true}

    for _, thing := range things {

        switch t := thing.(type) {

        // How can we implement logical OR for types to implement a catch-all for numerics?
        // Throws error: "type <type> is not an expression"
        // case ( int || float64 ) :
        //  fmt.Printf("Thing %v is a 'numeric' …
Run Code Online (Sandbox Code Playgroud)

types interface go

-1
推荐指数
2
解决办法
2694
查看次数

为什么持有接口类型的接口属性不能用包含实现接口的具体类型的属性实现?

实现界面时

public interface IMainViewModel
{
    ICommand DoStuffCommand { get; }
}
Run Code Online (Sandbox Code Playgroud)

为什么是

public class MainViewModel
{
    DelegateCommand DoStuffCommand { get; }
}
Run Code Online (Sandbox Code Playgroud)

public class DelegateCommand : ICommand { ... }
Run Code Online (Sandbox Code Playgroud)

不允许?

属性DoStuffCommand无法从接口实现属性IMainViewModel.类型应该是ICommand.


作为DelegateCommand实现ICommand,接口契约保证我能够使用DelegateCommand 完全相同的方式使用any ICommand.


我想知道为什么接口interface-property不能用具体类型实现,因为从我的观点来看,我不认为它是错误的(或改变行为或破坏功能).

c# interface

-1
推荐指数
1
解决办法
37
查看次数

默认情况下,接口不是公开的吗?

我记得默认情况下该接口是公共的。我错了吗?当我尝试从接口实现“公共静态”方法时。VS告诉我,该接口中的方法比“公共静态”方法更难访问。当我将public添加到接口时,它已修复。默认情况下,接口不是公开的吗?

interface IDoubleFloatEventInvoker {
 void AddListener(EventName eventName, UnityAction<float, float> unityAction);
}

public static void AddInvoker(EventName eventName, IDoubleFloatEventInvoker invoker)
{
    foreach (UnityAction<float, float> listener in doubleFloatListeners[eventName])
    {
        invoker.AddListener(eventName, listener);
    }
    doubleFloatInvokers[eventName].Add(invoker);
}
Run Code Online (Sandbox Code Playgroud)

“ VS说IDoubleFloatEventInvoker比AddInvoker函数更难访问”

c# static interface access-modifiers

-1
推荐指数
1
解决办法
50
查看次数

Golang:模拟具有相同方法名称的AWS服务

我正在使用两个AWS服务(Glue和DynamoDB),并且这两个服务都具有相同的方法名称,CreateTable因此在模拟这些服务时会CreateTable is ambiguous出错。

胶水:CreateTable https://docs.aws.amazon.com/sdk-for-go/api/service/glue/#Glue.CreateTable

DynamoDB:CreateTable https://docs.aws.amazon.com/sdk-for-go/api/service/dynamodb/#DynamoDB.CreateTable

有什么办法解决这个问题?

码:

type UpdateWorkflow struct {
    glueIface glueiface.GlueAPI
    dbIface   dynamodbiface.DynamoDBAPI
    tableName string
}

func NewUpdateWorkflow(tableName string) *UpdateWorkflow {
    sess := sessions.NewSession()
    return &UpdateWorkflow{
        dbIface:   dynamodb.New(sess),
        glueIface: glue.New(sess),
        tableName: tableName,
    }
}
Run Code Online (Sandbox Code Playgroud)

单元测试:

// MockUpdateWorkflow is a mock implementation of gluetestutils and dynamodb service
type MockUpdateWorkflow struct {
    glueiface.GlueAPI
    dynamodbiface.DynamoDBAPI
    mock.Mock
}

func setup() (*UpdateWorkflow, *MockUpdateWorkflow) {
    mockClient := new(MockUpdateWorkflow)
    mockServices := &UpdateWorkflow{
        glueIface: mockClient,
        dbIface:   mockClient,
        tableName: mockTableName,
    } …
Run Code Online (Sandbox Code Playgroud)

unit-testing interface go amazon-web-services

-1
推荐指数
1
解决办法
26
查看次数