标签: interface

接口方法的参数类型和返回类型

我有一些问题,关于参数类型应该在接口方法中应该是什么以及返回类型应该是什么 - 在C#中使用集合时.

假设我想获得A的集合并返回B的集合.我知道有拇指规则我们应该始终获得最通用的类​​型并返回最具体的类型.所以这里是一个例子:

public interface A
{
   B[] MyMethod(IEnumerable<A> myAs);
}
Run Code Online (Sandbox Code Playgroud)

以下是我从这一行得到的问题:

  1. 当界面在我的系统内部时 - 我应该返回B集合的接口还是具体的?
  2. 数组返回类型比列表返回类型更好吗?
  3. myAs参数应该是我需要的最通用的接口类型,这意味着如果在我的代码中,myAs实际上是一个列表,那么参数应该是IEnumerable,ICollection还是IList?

谢谢

c# generics interface

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

使用泛型扩展接口的类

所以我正在滚动我自己的最大堆,并且我使用扩展接口的泛型类做错了.说我有这样一个类:

class SuffixOverlap:IComparable<SuffixOverlap>
{
    //other code for the class
     public int CompareTo(SuffixOverlap other)
    {
        return SomeProperty.CompareTo(other.SomeProperty);
    }
}
Run Code Online (Sandbox Code Playgroud)

然后我创建我的堆类:

class LiteHeap<T> where T:IComparable
{
    T[] HeapArray;
    int HeapSize = 0;
    public LiteHeap(List<T> vals)
    {
        HeapArray = new T[vals.Count()];
        foreach(var val in vals)
        {
            insert(val);
        }
    }

    //the usual max heap methods
}
Run Code Online (Sandbox Code Playgroud)

但是当我尝试这样做时:

LiteHeap<SuffixOverlap> olHeap = new LiteHeap<SuffixOverlap>(listOfSuffixOverlaps);
Run Code Online (Sandbox Code Playgroud)

我收到错误: The type SuffixOverlap cannot be used as a type parameter T in the generic type or method LiteHeap<T>. There is no …

c# generics interface

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

对象<?实现InterfaceA,InterfaceB>

我需要一个对象引用两个不同的接口,如下所示:

interface InterfaceA {
    public void a();
}

interface InterfaceB {
    public void b();
}

class Test() {
    Object <? implements InterfaceA, InterfaceB>; object;

    Test() {
        object.a();
        object.b();
    }
}
Run Code Online (Sandbox Code Playgroud)

我知道继承有这种方式:Class <? extends Main> a 解决方案可以是一个帮助类:class Helperclass implements InterfaceA, InterfaceB{}

感谢您的帮助和阅读:)

java casting interface

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

如何确保使用接口实现公共静态函数

从Java 8开始,我们可以在接口中定义静态和默认方法.但我需要确保一个公共静态方法说foo()要在实现特定接口的所有类中实现interface A.我该怎么做,或者它是否可能?

界面A:

package com.practice.misc.interfacetest;

public interface A {
    public static Object foo(); //Eclipse shows error : 'This method requires a body instead of a semicolon'
    String normalFunc();
}
Run Code Online (Sandbox Code Playgroud)

B级:

package com.practice.misc.interfacetest;

public class B implements A{

    @Override
    public String normalFunc() {
        return "B.normalFunc";
    }
//I need to ensure that I have to define function foo() too

}
Run Code Online (Sandbox Code Playgroud)

C级:

package com.practice.misc.interfacetest;

public class C implements A{

    @Override
    public String normalFunc() {
        return "C.normalFunc";
    }
//I …
Run Code Online (Sandbox Code Playgroud)

java interface java-8

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

简单的Java回调不起作用

interface CallableInterface
{
    void callBackMethod();
}

class Worker
{   
    public static ArrayList<String> names = new ArrayList<String>();
    static Timer t = new Timer();

    public void addToList(String newAdd, CallableInterface callback)
    {   
        names.add("BMW");
        names.add("DODGE");

        t.schedule(new TimerTask() {
            @Override
            public void run() {     
                names.add(newAdd);
            }
        }, 5000);

       callback.callBackMethod();
    }

    public void printList(){
        System.out.println(Worker.names);
    }
}


class Boss implements CallableInterface
{   
    Worker w1 = new Worker();

    public Boss(String carName)
    {   
        w1.addToList(carName, this);
    }
    public void callBackMethod()
    {
        w1.printList();
    }
}


public class IntroCallbacks
{ 
    public …
Run Code Online (Sandbox Code Playgroud)

java interface callback

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

如果参数是同一个接口的不同实现,compareTo()返回什么?

有接口

interface Animal extends Comparable<Animal> {
}
Run Code Online (Sandbox Code Playgroud)

和2个班级

class Dog implements Animal {

}
Run Code Online (Sandbox Code Playgroud)

和

class Cat implements Animal {

}
Run Code Online (Sandbox Code Playgroud)

compareTo(Animal o)当争论不是同一个具体实施时应该返回什么Animal?

应该扔IllegalArgumentException吗?

例如,如果我将Dog实例传递给Cat.compareTo().我无法比较它们,因为它们是不同的类型.我不能称super.compareTo()他们的超级是Object没有实现的类型Comparable.铸造Dog到Cat会抛出ClassCastException.

java interface comparable

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

C#项未添加到List <String>

下面是我对Azure的ICollector接口的实现.

class TestCollector<T> : ICollector<T>
{
    public List<T> Collector => new List<T>();

    public void Add(T item)
    {
        Collector.Add(item);
    }
}
Run Code Online (Sandbox Code Playgroud)

在TestCollector上添加项目时,收集器的计数仍保持为0.

它没有被添加的原因是什么?我应该为TestCollector创建一个单独的构造函数并在里面初始化Collector吗?

c# interface list data-structures

0
推荐指数
2
解决办法
134
查看次数

实现类似于Ada 95界面的最佳方法是什么?

我想使用Ada 95实现类似于接口的东西(因此典型的OO接口不可用).我通过在记录中使用泛型和一组"指向方法"来完成它.代码如下.

编辑:我知道它可以通过将子程序作为形式参数传递给泛型包来完成,但我想避免传递太多参数.

我认为必须有更好的方法来实现我想要的东西,所以我想如果我是对的,如果是的话,我想看一个代码的例子.

"接口"在名为的通用包中声明Drivers.在那里,有一条记录,其中包含表示驱动程序的泛型类型的变量和包含其操作的记录:

drivers.ads

generic 
    type T is private;
    type Error is private;
    NOT_IMPLEMENTED_CODE : Error;

package Drivers is

    type Driver is private;

    -- Need to declare these types because I compile with Ada 95.
    type ToStringPtr is access function(self : in T) return String;
    type ReadLinePtr is access procedure(self : in T; buffer : out String; err : out Error);

    type DriverOps is
    record
        to_string_op : ToStringPtr := null;
        read_line_op : ReadLinePtr := null; …
Run Code Online (Sandbox Code Playgroud)

interface ada ada95

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

通用参数 - 使用具体类型编译,但实现的接口不编译

鉴于这些接口和类......

public interface IPage
{
    string PageTitle { get; set; }
    string PageContent { get; set; }
}


public abstract class Page
    : IPage
{
    public string PageTitle { get; set; }
    public string PageContent { get; set; }
}


public class AboutPage
    : Page
    , IPage
{
}


public interface IPageAdminViewModel<out T>
    where T : IPage
{
    IEnumerable<T> Pages { get; }
}


public abstract class PageAdminViewModel<T>
    : IPageAdminViewModel<T>
    where T: IPage
{
    public IEnumerable<T> Pages { get; set; …
Run Code Online (Sandbox Code Playgroud)

c# generics interface

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

通过模板函数调用未知类型的方法

C++语言允许我编写一个模板函数,该函数将调用传递给函数的对象上的方法.

这里的问题是当我这样做时,我的IDE(NetBeans 8.2)会抱怨它无法解决该方法.这是有道理的,因为在我尝试编译代码之前,用于"T"的类型是未知的,但它给我一个警告的事实使我有点担心这样做是不是错误的编程实践.

请考虑以下代码:

struct S1 {
    void method() const {
        cout << "Method called from S1!" << endl;
    }
};

struct S2 {
    void method() const {
        cout << "Method called from S2!" << endl;
    }
};

template <typename T>
void call_method(const T& object) {

    // IDE reports a warning "can't resolve the template based identifier method."
    object.method();
}
Run Code Online (Sandbox Code Playgroud)

用法示例:

S1 obj1;
S2 obj2;

call_method(obj1);
call_method(obj2);
Run Code Online (Sandbox Code Playgroud)

这段代码将编译并正常工作,但IDE总是会抱怨.这可以吗?或者是否有一个更好的设计,我应该知道,以获得相同的预期结果.

期望的结果是编写一个可以使用S1或S2对象的函数,而无需关心它是哪一个,只要它们提供包含"method()"的接口即可.

假设我无法访问S1和S2的源代码,因此无法对其进行更改.(例如,我不能使它们从公共基类继承并使用动态多态而不是模板函数来实现相同的效果).

c++ templates interface generic-programming

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