整数扩展数字,为什么我在底部得到错误?
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>
我试图创建一个与接口列表一起工作的函数,但我想保留这些列表的具体类值.例如:
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) 我不明白我得到的这个错误.我曾多次尝试清理和构建我的项目.谁能帮我?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) interface temp
{
public int add(int a,int b)
{
return a+b;
}
}
Run Code Online (Sandbox Code Playgroud)
我们可以在接口中实现上面的方法,或者我们只需要在接口中定义方法.
如何将派生自接口的类传递给以接口为参数的函数?
我有一个接口和一个类设置类似这样的东西。
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?
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.我只是想比较两个简单的字符串,为什么会这样呢?
我试图找出教科书中问题的答案,但我遇到了麻烦.问题是要求我检查输入数组以查看它是否包含适合某些功能的对象.除了当我尝试使用作为实现接口的类之一的输入实现该方法时,这个的每个特性似乎都正常工作.
例:
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)
它的建议是将方法中的类更改为特定的类而不是以这种方式编写它的目的的接口.
编码时我是否犯了某种菜鸟错误?
我有一个包装对象列表的类.我希望这个类与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)
给我同样的错误.
为什么接口不能实现这样的方法?
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.
是否有任何解决方法可以解决我所面临的问题?
我是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) interface ×10
java ×5
c# ×4
generics ×2
.net ×1
c++ ×1
comparable ×1
compareto ×1
extends ×1
foreach ×1
http ×1
ienumerable ×1
ienumerator ×1
request ×1