我一直在努力学习Java,我正在研究一个项目.
我有一个名为Card的抽象类,我将其用作模板来创建子类.
它有一种设置卡的名称字段和颜色字段的方法以及设置成本的方法.
我有一个名为Workshop的卡子类.
我正在尝试使用Workshop in Workshop的方法来设置Workshop的名称和颜色字段.
为此,我正在调用方法super.setCardNameColor("Workshop", "Green")和super.setCardCost(0,0,0,0,0,0,0,0);
但是,为了做到这一点,Eclipse让我在类声明中使用(似乎是)双花括号.
我怀疑这与Anonymous Inner Classes有关,可能来自"超级"或类似的东西,但我的Google搜索都没有给我提供我需要的信息.任何人都可以在这里阐述一下这个问题吗?我正在使用sysout和getters来确保在Workshop的一个实例上正确设置了值,但是我对双括号感到困惑.提前感谢您的帮助!
编辑:这是代码
public abstract class Card
{
private String cardName = "";
private String cardColor = "";
private int coinCost = 0;
private int woodCost = 0;
private int brickCost = 0;
private int stoneCost = 0;
private int oreCost = 0;
private int glassCost = 0;
private int clothCost = 0;
private int paperCost = 0;
private int coinValue = 0;
private int pointValue …Run Code Online (Sandbox Code Playgroud) 我有一个以下形式的嵌套集合:
HashMap<String, HashMap<String, List<String>>> errorList;
Run Code Online (Sandbox Code Playgroud)
现在我使用双括号内联初始化它,如下所示
errorList.put(tempName, new HashMap<String, List<String>>() {{
put("upl", new ArrayList<String>() {{ add("Y"); add("Upload Success"); }});
}});
Run Code Online (Sandbox Code Playgroud)
tempName这位于 foreach 循环中,每次迭代时值都会发生变化。我这样做是因为我无法使用List<String>or的实例HashMap<String,List<String>>,因为每次我更改该实例中的值时,它都会反映在它嵌套的集合中。所以我被迫使用双括号初始化创建新实例。
事情是:我想使用列表对象。代替
new ArrayList<String>() {{ add("Y"); add("Upload Success"); }}
Run Code Online (Sandbox Code Playgroud)
我想使用一个变量。
我怎样才能做到这一点?
我刚开始学习java并且有一个非常基本的问题.我有一个标签,我希望在1到18之间的随机整数落在特定数字上时更改颜色.这些数字并不奇怪或偶数,所以我不能用它.
现在我有这个:
if (Random == 1 || Random == 2 || Random == 5 || Random == 7 || Random == 12 || Random == 14 || Random == 16 || Random == 18)
label_number.setForeground(SWTResourceManager.getColor(SWT.COLOR_BLUE));
else if (Random == 3 || Random == 4 || Random == 6 || Random == 8 || || Random == 9 | Random == 10 || Random == 11 || Random == 13 || Random == 15 || Random == 17)
label_wheelNumber.setForeground(SWTResourceManager.getColor(SWT.COLOR_GREEN));
Run Code Online (Sandbox Code Playgroud)
我知道它看起来很傻,而且我觉得这样做是个白痴.您有什么推荐的吗?我没上过课,所以任何解释都非常有用.谢谢
我想要实现的目标可能毫无意义,但我想找到答案来满足我的好奇心。
我正在尝试创建一个有效的语句,该语句将基于另一个列表创建一个新列表,并在末尾添加一个新值。
我是 lambda 表达式的新手,我不确定应该使用哪种列表。
该方法看起来像这样:
private List<Integer> conjoin(List<Integer> list, Integer newValue){
return new ArrayList<Integer>(list).stream()
.map(value -> list.indexOf(value)==(list.size()-1) ?
Arrays.asList(value, newValue) :
Arrays.asList(value))
.flatMap(Collection::stream)
.collect(Collectors.toList());
}
Run Code Online (Sandbox Code Playgroud)
但更加整洁和高效。
或者是否有一个内置方法可以执行我不知道的相同操作?
我正在寻找初始化ConcurrentHashMap的"CLEAN&Simple"方法.
使用Java 8,我有: -
private static final Map<String, String> myStreamedMap = Stream.of(
new AbstractMap.SimpleImmutableEntry<>("Key1", "Value1"),
new AbstractMap.SimpleImmutableEntry<>("Key2", "Value2"),
new AbstractMap.SimpleImmutableEntry<>("Key3", "Value3"),
new AbstractMap.SimpleImmutableEntry<>("Key4", "Value4")).
collect(Collectors.toMap((entry) -> entry.getKey(), (entry) -> entry.getValue()));
Run Code Online (Sandbox Code Playgroud)
无论如何,我提供了所需的最终结果
" new AbstractMap.SimpleImmutableEntry<>"
让人很难看到最新情况.
有什么方法可以"隐藏"这个并保持一条线?
UPDATE
想出了这个(明显的)解决方案
private static final Map<String, String> myStreamedMapWith = Stream.of(
with("Key1", "Value1"),
with("Key2", "Value2"),
with("Key3", "Value3"),
with("Key4", "Value4")).
collect(Collectors.toMap((entry) -> entry.getKey(), (entry) -> entry.getValue()));
private static AbstractMap.SimpleImmutableEntry<String, String> with(final String key, final String value) {
return new AbstractMap.SimpleImmutableEntry<>(key, value);
}
Run Code Online (Sandbox Code Playgroud) 我想学习 Java 的“新”语法和 API 中的可能性。更新我的意思是 10+(比方说 10-13)。它主要围绕 lambdas 的声明和存储与映射中的值相同的签名的不同实现。最近我主要和 Gosu 一起工作,我可以用这个片段:
var longInput = 10000000L
new LinkedHashMap<String, block(long) : long>( {
"byte" -> (\x -> x as byte as long),
"short" -> (\x -> x as short as long),
"int" -> (\x -> x as int as long),
"long" -> (\x -> x as long as long)
}).eachKeyAndValue(\key, value ->
print("${longInput} ${value(longInput) == longInput ? "can" : "cannot"} be converted to ${key}")
)
Run Code Online (Sandbox Code Playgroud)
我可以在 Java 10 中类似地做到这一点:
import java.util.*;
public …Run Code Online (Sandbox Code Playgroud) public static List<Long> abc = new ArrayList<Long>(){{ //Asks for SerialVersionUID
abc.add(5L);
abc.add(7L);
}};
public static List<Long> abc = new ArrayList<Long>();//Does not need SerialVersionUID
static{
abc.add(5L);
abc.add(7L);
}
Run Code Online (Sandbox Code Playgroud) 因为,我习惯于在javascript中编码,我想创建具有属性的对象.
这是我的代码:
jPanel2.add( new JPanel(){ this.add(new JButton("Add")); });
Run Code Online (Sandbox Code Playgroud)
你有什么建议吗 ?
考虑这个随机对象:
Object Test of Class TestClass:
String Name;
Integer Age;
procedure setName(n);
function getName(): String;
Run Code Online (Sandbox Code Playgroud)
在Delphi中,如果我们想要使用对象的许多属性和方法轻松工作,我们可以这样做:
Test.Name = 'EASI';
Test.Age = 34;
Test.setName('Eduardo Alcantara');
ShowMessage(Test.getName);
Run Code Online (Sandbox Code Playgroud)
......或者我们可以这样做:
with Test do
begin
Name = 'EASI';
Age = 32;
setName('Eduardo Alcântara');
ShowMessage(getName);
end
Run Code Online (Sandbox Code Playgroud)
在Java中是否有类似的结构,我们可以在Delphi中缩短语法?
如何手动填写以下HashMap?
public static final HashMap<String,int[]> AGE_GROUPS = {"18-24",{18,24},
"25-29",{25,29},
"30-39",{30,39},
"40-49",{40,49},
"50-59",{50,59},
"60-69",{60,69},
"70-79",{70,79},
"80+",{80,120}};
Run Code Online (Sandbox Code Playgroud) java ×10
hashmap ×2
lambda ×2
collections ×1
delphi ×1
dictionary ×1
eclipse ×1
gosu ×1
java-8 ×1
java-stream ×1
list ×1
object ×1