标签: interface

整数扩展数字但不能操作<?使用Integer扩展Number>

整数扩展数字,为什么我在底部得到错误?

interface Predicate<T> {
   public abstract boolean check(T t);
}

Predicate<? extends Number> predUpper = null;

predUpper.check(new Integer(73));
Run Code Online (Sandbox Code Playgroud)

错误:谓词类型中的方法检查(捕获#6-of?extends Number)不适用于参数(整数)

我读过:类型Map <String中的方法,捕获#1-of?extends Object>不适用和<?超级T>和<?在Java中扩展T>

java generics extends interface

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

使用界面但保留具体值

我试图创建一个与接口列表一起工作的函数,但我想保留这些列表的具体类值.例如:

public interface GUID_OBJECT
{
    string GUID { get; set; }
}

public class Car : GUID_OBJECT
{
    public string GUID { get; set; }
    public string CarSpecifikProp { get; set; }
}

public class Bike : GUID_OBJECT
{
    public string GUID { get; set; }
    public string BikeSpecifikProp { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
class Program
{
    static void Main(string[] args)
    {
        var cars = new List<Car>() { new Car() { GUID = "1", CarSpecifikProp = "carstuff" }, new Car() …
Run Code Online (Sandbox Code Playgroud)

c# interface

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

名称空间X已包含XX的定义?

我不明白我得到的这个错误.我曾多次尝试清理和构建我的项目.谁能帮我?Program.cs中

using System;
using System.Collections.Generic;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.IO;
using System.Threading.Tasks;

namespace HTTPrequestApp
{
    class Program
    {
        static void Main(string[] args)
        {
            var lstWebSites = new List<string>
            {
                "www.mearstransportation.com",
                "www.amazon.com",
                "www.ebay.com",
                "www.att.com",
                "www.verizon.com",
                "www.sprint.com",
                "www.centurylink.com",
                "www.yahoo.com"
            };
            string filename = @"RequestLog.txt";
            {
                using (var writer = new StreamWriter(filename, true))
                {
                    foreach (string website in lstWebSites)
                    {
                        for (var i = 0; i < 4; i++)
                        {
                            MyWebRequest request = new MyWebRequest();
                            request.Request(website);
                        }
                    }
                }
            }
        }
    } …
Run Code Online (Sandbox Code Playgroud)

.net c# interface http request

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

我们可以在java中实现接口内的任何方法

interface temp
{
   public int add(int a,int b)
   {
      return a+b;
   }
}
Run Code Online (Sandbox Code Playgroud)

我们可以在接口中实现上面的方法,或者我们只需要在接口中定义方法.

java interface

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

使用接口作为共享指针参数

如何将派生自接口的类传递给以接口为参数的函数?

我有一个接口和一个类设置类似这样的东西。

class Interface
{
public:
    virtual ~Interface() {}
    virtual void DoStuff() = 0;
};

class MyClass : public Interface
{
public:
    MyClass();
    ~MyClass();
    void DoStuff() override;
};

void TakeAnInterface(std::shared_ptr<Interface> interface);

int main()
{
    auto myInterface = std::make_shared<MyClass>();
    TakeAnInterface(myInterface);
}
Run Code Online (Sandbox Code Playgroud)

编译器抱怨No matching function call to TakeAnInterface(std::shared_ptr<MyClass>&)。为什么功能TakeAnInterface不能接收Interface类而不是MyClass?

c++ interface

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

Java CompareTo方法声明我无法将int转换为boolean,即使它们都没有使用

public int compareTo(Person p) {
    int res = 1;
    String personStr = p.getId();
    String thisId = this.getId();

    if(thisId.equals(personStr)){
        res = 0;
    }
    else if(thisId.compareTo(personStr)){
        res = -1;
    }

    return res;
}
Run Code Online (Sandbox Code Playgroud)

我已经实现了一个非常简单的compareTo方法,但是我没有收到错误消息.如果statemint中的条件给我一条消息,说它不能从int转换为boolean.我明白了,但问题是我正在使用netiher.我只是想比较两个简单的字符串,为什么会这样呢?

java interface compareto comparable

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

为什么接受接口的方法会拒绝该接口的实现?

我试图找出教科书中问题的答案,但我遇到了麻烦.问题是要求我检查输入数组以查看它是否包含适合某些功能的对象.除了当我尝试使用作为实现接口的类之一的输入实现该方法时,这个的每个特性似乎都正常工作.

例:

main(){
  boolean r1 = has(input, checkFor2);
}

public static <T> boolean has(T[] input, Check<T> c){
  // does its check algorithm and returns a boolean
}

static public interface Check<T>{
  boolean isContained(T item);
}

static public class checkFor2 implements Check<Integer>{
  public boolean isContained(Integer val){
    // does a check algorithm
  }
}

// Other check algorithms implementing Check<T> follow as well.
Run Code Online (Sandbox Code Playgroud)

我得到的错误是在main方法中调用"has"时.它说:

has(T[], main.Check<T>)main类型中的方法不适用于参数(int[], main.checkFor2)

它的建议是将方法中的类更改为特定的类而不是以这种方式编写它的目的的接口.

编码时我是否犯了某种菜鸟错误?

java generics interface

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

使类与foreach兼容

我有一个包装对象列表的类.我希望这个类与foreach循环兼容.

我已经看到了几个关于如何在SO上做到这一点的问题,但大多数答案都没有为我编译.

class TrackList
{
    private List<Track> tracks = new List<Track>();
}
Run Code Online (Sandbox Code Playgroud)

你会添加什么来使其适用于每个?

到目前为止,我有:

class TrackList : IEnumerable<Track>
{
    // ...

    public IEnumerator<Track> GetEnumerator()
    {
        return trackList.GetEnumerator();
    }

    System.Collections.IEnumerator System.Collections.IEnumerator.GetEnumerator()
    {
        return this.GetEnumerator();
    }
}
Run Code Online (Sandbox Code Playgroud)

但它说(翻译错误):

错误1'HP.TrackList'未实现接口成员'System.Collections.IEnumerable.GetEnumerator()'.'HP.TrackList.GetEnumerator()'无法实现'System.Collections.IEnumerable.GetEnumerator()',因为它没有与'System.Collections.IEnumerator'对应的返回类型.XXX\TrackList.cs 11

如果相反我使用:

IEnumerator<Track> IEnumerator<Track>.GetEnumerator()
{
    return this.GetEnumerator();
}
Run Code Online (Sandbox Code Playgroud)

给我同样的错误.

c# ienumerable ienumerator foreach interface

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

C#为什么接口不能实现这样的方法?解决我的问题的解决方法是什么?

为什么接口不能实现这样的方法?

public interface ITargetableUnit {
        //Returns whether the object of a class that implements this interface is targetable
     bool unitCanBeTargeted(){
        bool targetable = false;
        if(this is Insect){
            targetable = (this as Insect).isFasterThanLight();
        }
        else if(this is FighterJet){
            targetable = !(this as FighterJet).Flying;
        }
        else if(this is Zombie){
            targetable = !(this as Zombie).Invisible;
        }
        return targetable;
    }
}
Run Code Online (Sandbox Code Playgroud)

Insect和Zombie都已经从基类Creature派生而来,而FighterJet派生自类Machine但是,并非所有的Creature都是可定位的,并且不使用ITargetableUnit inteface.

是否有任何解决方法可以解决我所面临的问题?

c# implementation interface

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

为什么可以将变量分配给接口类型?

我是Java的新手,正在尝试学习界面的概念.我在网上看到了下面的代码.我知道接口无法实例化.我的问题是,WatchService,Path,WatchKey和WatchEvent都是接口,为什么变量可以分配给接口类型?实例化是一样的吗?

import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.nio.file.Paths;
import static java.nio.file.StandardWatchEventKinds.ENTRY_CREATE;
import java.nio.file.WatchEvent;
import java.nio.file.WatchKey;
import java.nio.file.WatchService;

public class WatchServices {

    public static void main(String[] args) throws IOException {
        WatchService ws1 = FileSystems.getDefault().newWatchService();

        Path p1 = Paths.get("/Users/justin/Desktop/Codes Netbean/JavaRandom");

        WatchKey wk1 = p1.register(ws1, ENTRY_CREATE);

        while(true){
            for(WatchEvent<?> event : wk1.pollEvents()){
                System.out.println(event.kind());
                Path file  = (Path)event.context();
                System.out.println(file);
            }
        } 
    }
}
Run Code Online (Sandbox Code Playgroud)

java interface instantiation

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