我有一些问题,关于参数类型应该在接口方法中应该是什么以及返回类型应该是什么 - 在C#中使用集合时.
假设我想获得A的集合并返回B的集合.我知道有拇指规则我们应该始终获得最通用的类型并返回最具体的类型.所以这里是一个例子:
public interface A
{
B[] MyMethod(IEnumerable<A> myAs);
}
Run Code Online (Sandbox Code Playgroud)
以下是我从这一行得到的问题:
谢谢
所以我正在滚动我自己的最大堆,并且我使用扩展接口的泛型类做错了.说我有这样一个类:
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 …
我需要一个对象引用两个不同的接口,如下所示:
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 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) 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) 有接口
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.
下面是我对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吗?
我想使用Ada 95实现类似于接口的东西(因此典型的OO接口不可用).我通过在记录中使用泛型和一组"指向方法"来完成它.代码如下.
编辑:我知道它可以通过将子程序作为形式参数传递给泛型包来完成,但我想避免传递太多参数.
我认为必须有更好的方法来实现我想要的东西,所以我想如果我是对的,如果是的话,我想看一个代码的例子.
"接口"在名为的通用包中声明Drivers.在那里,有一条记录,其中包含表示驱动程序的泛型类型的变量和包含其操作的记录:
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) 鉴于这些接口和类......
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++语言允许我编写一个模板函数,该函数将调用传递给函数的对象上的方法.
这里的问题是当我这样做时,我的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的源代码,因此无法对其进行更改.(例如,我不能使它们从公共基类继承并使用动态多态而不是模板函数来实现相同的效果).