标签: interface

为什么不能从Interface继承

在Java中,为什么我不能继承Interfaces?

如果情况是接口没有提供方法的实现,那么我们如何能够从Abstract Classes继承.

java inheritance interface

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

接口作为返回类型会产生问题

我在以下实现中有错误.它说OnlineWebStore_Process无法实现IWebStore接口,因为它们没有匹配的返回类型.但该方法返回Item,它实现了在IWebStore接口中用作返回类型的IItem接口.什么是这个问题的好方法?

public interface IItem
{
    string id { get; set; }
    string name { get; set; }
}

public interface IWebStore
{
    List<IItem> GetOnlineItems();
}

public class Item : IItem
{
    public Item(string _id)
    {
        id = _id;
    }

    public string id { get; set; }
    public string name { get; set; }
}

public class OnlineWebStore_Process : IWebStore
{
    public List<Item> GetOnlineItems()
    {
        List<Item> items = new List<Item>();

        return items
    }
}
Run Code Online (Sandbox Code Playgroud)

c# interface

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

界面奇怪的泛型行为

我想创建一个泛型类,它接受一些可比较的泛型类型的元素.所以我这样做:

public class Foo<T extends Comparable<T>>

在课堂上Foo我有类似的东西:

public void bar(T t)

我确信我可以写这样的代码:t.compareTo(v).

问题1:为什么在使用泛型时我们extends不implements使用接口?Comparable不是一个阶级.

\\

现在假设我想创建另一个与上面类似的类也实现该bar方法.我想创建这个界面:

public interface Face<T extends Comparable<T>> {
    public void bar(T t);
}
Run Code Online (Sandbox Code Playgroud)

然后我改变类Foo来实现Face(public class Foo<T extends Comparable<T>> implements Face).

问题2:执行此操作时,我收到以下编译错误:

Foo类型的方法栏(T)必须覆盖或实现超类型方法.

为什么是这样?

当我告诉Eclipse add the unimplemented methods我得到: public void bar(Comparable t)而不是... bar(T t).

java generics extends interface implements

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

c ++接口告诉我我的参数尚未声明

我有一个针对自动机的许多状态的泛型类.它声明如下:

#ifndef STATE_H_
#define STATE_H_
#include "Automat.h"

class State {
public:
virtual void readChar(char c, Automat* automat) = 0;
virtual ~State(){};
};

#endif /* STATE_H_ */
Run Code Online (Sandbox Code Playgroud)

我在eclipse中遇到这个错误:

此行有多个标记

  • 从'Automat*const'到'int*'的参数2没有已知的转换
  • virtual void State :: readChar(char,int*)
  • 'Automat'尚未宣布

我的自动贩卖机如下:

    #ifndef Automat_H_
    #define Automat_H_
    #include "../../Scanner/src/IScanner.h"
    #include "./States/State.h"

    class Automat {
    public:
        int count;
        State* current;
        IScanner* scanner;
        Automat(IScanner *s);
        void readChar(char c);
        void setState(State *s);
        void error();
        ~Automat();
    };

    #endif /* Automat_H_ */
Run Code Online (Sandbox Code Playgroud)

最后实现了Automat,我将省略一些方法.

#include "Automat.h"
#include "./States/StartState.h"


Automat::Automat(IScanner *s) {
    current = …
Run Code Online (Sandbox Code Playgroud)

c++ interface

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

使用字符串到接口的GetType

我为Control DLL创建了一个DLL加载器(这些控件具有相同的接口iControlPlugin),一切都很好用.但是我现在需要为设备驱动程序创建一个DLL加载器.我开始查看我的Control Loader代码,并意识到代码是相同的,除非定义了接口类型.因此,我想使我的控制加载器功能更通用,以便它可以用于不同的接口类型.

我修改了函数,以便它返回一个字典,我遇到的问题是我做了一个检查,以确保DLL文件包含匹配的类型,在我的情况下,这是两种接口类型之一.但我总是无效.

这是我原来的工作功能,专门针对一种类型(iLPPControlPlugin)

public Dictionary<string, iLPPControlPlugin> LoadPlugins(string folder)
{
    /* Create a Dictionary object to store the loaded DLL's */
    Dictionary<string, iLPPControlPlugin> plugsins = new Dictionary<string,iLPPControlPlugin>();

    /* Get all the DLL files from the path specified */
    string[] files = Directory.GetFiles(folder, "*.dll");

    /* Go through each DLL... */
    foreach (string file in files)
    {
        /* Load the file, as an assembly. */
        Assembly assembly = Assembly.LoadFile(file);

        /* Get the type, can be more than one */
        var types …
Run Code Online (Sandbox Code Playgroud)

c# dll interface gettype

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

使用as关键字的接口异常whitout强制转换

我试图创建一个泛型类,它接收基于接口的泛型参数,代码是异常

所以我测试了,我有以下内容

interface InterfaceA { }
interface InterfaceB<T> where T:InterfaceA { }
class ClassA : InterfaceA { }
class ClassB : InterfaceB<ClassA> { }
Run Code Online (Sandbox Code Playgroud)

基本测试

InterfaceB<InterfaceA> my_object_1 = new ClassB(); // Not Working (Compile error)
InterfaceB<InterfaceA> my_object_2 = (InterfaceB<InterfaceA>)new ClassB(); // Not Working (Throw Exception)
InterfaceB<InterfaceA> my_object_3 = new ClassB() as InterfaceB<InterfaceA>; // Working but still null
Run Code Online (Sandbox Code Playgroud)

有人可以解释为什么my_object_1,my_object_2和my_object_3出现问题?

c# interface

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

为什么在java中,接口中的静态最终变量可以被本地var覆盖?

我知道静态意味着,内存中只有一个实例.我知道final意味着它不能被改变或子类化,我也知道在java接口中定义的任何变量都是静态final

所以现在问题是,为什么我可以在"XFace"类的界面"MyFace"中超越最终的静态变量"a"?

例:

public interface MyFace {
    static final int a = 15;

    void smile();
}
Run Code Online (Sandbox Code Playgroud)

然后在课堂上,我可以轻松地骑过a,与当地人一起,

public class XFace implements MyFace {

    @Override
    public void smile() {
        int a=3;  // over riding interface's a variable and suprsingly it works !
        System.out.println(a*2);  // will print 6
    }
Run Code Online (Sandbox Code Playgroud)

为什么我可以在smile()方法中定义int a = 3?是不是"一个"最终和静态?它怎么可能被覆盖?

java static final interface

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

接口实现和通用功能

我有以下要求,

  • 将有2个(或更多)不同的类来执行相同类型的操作(以不同的方式).因此我决定创建一个界面.然后我用我的界面实现了这两个类.
  • 现在,从另一个类我将使用Interface类型的对象并调用函数.一切正常.
  • 然后一个新的要求来创建一个适用于这两个类的通用功能.
  • 我不想在两个类中定义相同的功能.并且接口不允许函数定义.
  • 首先,我认为抽象类是有用的.因为它允许函数定义和抽象函数.但抽象类不能实例化,我还需要创建具有单独类类型的对象.

对不起,我找不到一个简单的方法来定义我的问题.感觉就像Spring框架提供的解决方案.但我需要知道如何从Java/C#应用程序中实现这一点.

c# java oop abstract-class interface

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

获取和设置集合中的方法

List接口允许我们在索引处使用get()方法获取对象.

我们如何在像LinkedHashSet这样的set接口中获取特定索引处的对象

java collections interface

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

实现接口时的可选参数用法

我正在使用laravel orm组件,并在部署生命服务器时观察到以下内容.

我收到了错误

Declaration of Illuminate\Database\Eloquent\Collection::toJson() 
must be compatible 
with Illuminate\Support\Contracts\JsonableInterface::toJson($options = 0) 
Run Code Online (Sandbox Code Playgroud)

我部署在安装了php v5.4.6的ubuntu服务器上.在安装了php v5.3.5的本地机器上工作时没有得到这个.

接口声明如下

public function toJson($options = 0);
Run Code Online (Sandbox Code Playgroud)

而抽象类的实现是

public function toJson()
{
    return json_encode($this->toArray());
}
Run Code Online (Sandbox Code Playgroud)

我不认为这会是一个问题,因为接口声明的参数是可选的,抽象类实现它而不需要参数.

这是php v5.4的一个东西,它提供了什么优势?

另外,解决这个问题的最佳方法是什么?接口是由很多类实现的,所以我试图不破坏依赖; 你会建议在生命服务器上降级我的php版本吗?

php interface laravel

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