我会尽量问这个问题的通用性(没有双关语)。
假设我有一个用作汽车容器的对象“车库”。它看起来像这样:
public abstract class Garage {
public abstract List<? extends Car> getCars();
public abstract void addCarToGarage(Car car);
}
Run Code Online (Sandbox Code Playgroud)
因为我很有钱,我会有不同类型的车库,有的只有我的跑车,有的只有我的SUV等等。
所以我的车库之一看起来像这样:
public class SportCarGarage extends Garage {
@Override
public List<SportCar> getCars() {
// code to return the cards
}
@Override
public void addCarToGarage(Car car){
// code to add car to garage
}
}
Run Code Online (Sandbox Code Playgroud)
现在你可能已经看到我的问题了。我可以实现,getCars()以便它返回一个列表SportCar。但是如果我打电话addCarToGarage,我需要检查每辆车的类型,如果有人试图在我的 SportCarGarage 中添加 SUV,我不会收到语法错误。
我想要这样的东西:
public abstract class Garage {
public abstract List<? extends Car> getCars();
public abstract void addCarToGarage(? extends …Run Code Online (Sandbox Code Playgroud) 这个方法定义有什么问题?
public static List<T extends MyObject> T find() {
}
Run Code Online (Sandbox Code Playgroud)
编译说:
Syntax error, insert ";" to complete MethodDeclaration
Run Code Online (Sandbox Code Playgroud) List<List> d=new Arraylist<List>();
Run Code Online (Sandbox Code Playgroud)
我该如何对列表进行排序d?如果我使用Collection.sort(d);我收到以下错误:
Bound mismatch: The generic method sort(List<T>) of type Collections is not
applicable for the arguments (List<List>). The inferred type List is not a
valid substitute for the bounded parameter <T extends Comparable<? super T>>
Run Code Online (Sandbox Code Playgroud) 我想计算0.95的值.这是我的方法:
public static final int VAR = 5;
private static double getDouble(){
double dis = (double)(VAR/100);
dis = (double)(1-dis);
return dis;
}
Run Code Online (Sandbox Code Playgroud)
但是,它输出1.0 ?? 如果我在main方法中键入相同的代码,我得到0.95.我的错误在哪里?
使用java.util.Scanner;我希望控制台允许用户从大写和小写输入.例如:
if (user.equals("test") {
System.out.println("hello");
}
Run Code Online (Sandbox Code Playgroud)
但是我希望控制台能够接受所有类型的情况,因此用户可以输入喜欢TEST和可能TeSt,并且控制台会将其视为"测试"
我最近开始实现一些数据结构,并且,我正在尝试使所有内容尽可能“可扩展”。
public abstract class AbstractNode<E extends Element, U extends AbstractNode<E, U>> { ... }
public class BinarySearchTree<Element> extends Tree<Element, Node<Element>> { ... }
public class Element implements Cloneable { ... }
public class Node<E extends Element> extends AbstractNode<E, Node<E>> { ... }
public abstract class Tree<E extends Element, T extends AbstractNode<E, T>> { ... }
Run Code Online (Sandbox Code Playgroud)
BinarySearchTree消息提示我在课堂上遇到很多错误Bound mismatch: The type Element is not a valid substitute for the bounded parameter <E extends Element> of the type Node<E>。为什么!?我在那里做错了什么?
另外,在BinarySearchTree类中,我得到了 …
public enum Day {
SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
THURSDAY, FRIDAY, SATURDAY
}
Run Code Online (Sandbox Code Playgroud)
如果我输入的是"SUNDAY" String,我怎样才能得到相应的Day.SUNDAY?
class ServerSocket{
....
public ServerSocket(int port) throws IOException {
this(port, 50, null);
}
....
}
Run Code Online (Sandbox Code Playgroud)
我知道这个关键字用于表示活动对象或当前对象.在这段代码中,"this"的含义是什么?我得到这个代码(在这里)
如何在Java中为Mongo DB编写此查询的等效项:
db.mydata.find({$where: "this.fields.name.toLowerCase().indexOf('ar') > 0"})
Run Code Online (Sandbox Code Playgroud)
?
该查询用于查找包含"AR"(马丁,标记等)的所有名称.
我目前正在使用QueryBuilder,但它不支持这种查询格式.
任何的想法?
我是Java的新手,现在正在学习Java教程.当我输入准确的代码时LabmdaScopeTest.java,我遇到了错误package java.util.funciton does not exist.代码如下.
import java.util.funciton.Consumer;
public class LambdaScopeTest {
public int x = 0;
class FirstLevel {
public int x = 1;
void methodInFirstLevel(int x) {
// The following statement causes the complier to generate
// the error "local variables referenced from a lambda expression
// must be final or effectively final" in statemen A:
//
// x = 99;
Consumer<Integer> myConsumer = (y) ->
{
System.out.println("x = " + x); // …Run Code Online (Sandbox Code Playgroud)