每个循环的通常形式是:
for(Foo bar: bars){
bar.doThings();
}
Run Code Online (Sandbox Code Playgroud)
但是如果我想在循环之后保留bar,我就不能使用for循环:
Foo bar = null;
// - Syntax error on token "bar", Identifier expected after this token
for(bar: bars){
if(bar.condition())
break;
}
bar.doThings();
Run Code Online (Sandbox Code Playgroud)
for循环得到上面提到的语法错误.
为什么是这样? 我对解决方法不感兴趣,但只是对此限制背后的考虑因素感到好奇.
相反,对于普通的for循环,变量可以在外部声明或根本不声明...
int i = 1;
for(;i<max;i++){
for(;;){
// Do things
}
}
Run Code Online (Sandbox Code Playgroud) 我正在努力处理数据类和多态.我想从不变性中受益,但仍然能够更新我的状态.为此,我希望能够使用该copy功能.
我们举个例子吧.我有这个类层次结构:
interface Aging {
val age : Int
}
data class Cheese (
override val age : Int
// more Cheese specific properties
) : Aging
data class Wine (
override val age : Int,
val grape : String
// more Wine specific properties
) : Aging
Run Code Online (Sandbox Code Playgroud)
现在我希望能够做这样的事情(但是这不工作):
class RipeningProcess(){
fun ripen(products : List<Aging>) =
// Not possibe, no copy function on Aging
products.map { it.copy(age = it.age + 1) }
}
Run Code Online (Sandbox Code Playgroud)
如何以多态方式创建更新的副本?
我试图给接口一个copy …
最近我在进入通用铸造问题时重构了一个通用的方法,我无法解释.最后我意识到我可以完全没有T型(只是自己内联),但我仍然很好奇为什么转换失败.我创建了这个最小的例子来说明问题.
有人能解释一下为什么转换失败并且解决方法有效吗?
public <K, T extends List<K>> void castLists(List<T> list, K kForBinging) {
Map<Integer, List<T>> map = mapSizeToList(list);
// Type mismatch: cannot convert from Map<Integer,List<T>> to Map<Integer,List<List<K>>>
// Map<Integer, List<List<K>>> expandedMap = map;
// Added after accepting answer, legal assignment:
Map<Integer, ? extends List<? extends List<K>>> expandedMap = map;
// Originally proposed 'work around'
Map<Integer, ?> lessSpecific = map;
@SuppressWarnings("unchecked")
Map<Integer, List<List<K>>> canCast = (Map<Integer, List<List<K>>>)lessSpecific;
// ...
}
public <A> Map<Integer, List<A>> mapSizeToList(List<A> list) {
Map<Integer, List<A>> map = …Run Code Online (Sandbox Code Playgroud) 为junit-quickcheck编写生成器时,可以轻松使用提供的Ctor或Fields反射方法。但是直接在我的业务模型上应用反射会阻止我限制生成的数据(除非我将我的业务模型与快速检查注释交错)。
例如:
class Price {
public final BigDecimal price;
public final BigDecimal discountPercentage;
// With a constructor
}
Run Code Online (Sandbox Code Playgroud)
价格字段可以是任何(合理的)BigDecimal(假设最大为 1000),但折扣必须在 0 到 100 之间。
如何以惯用的方式编写生成器?
我当前的方法是创建一个类来指定要生成的模型:
class PriceFields {
public @InRange(max="1000") BigDecimal price;
public @InRange(max="100") BigDecimal discountPercentage;
public Price toPrice(){
return new Price(price, discountPercentage);
}
}
Run Code Online (Sandbox Code Playgroud)
还有一个像这样的生成器:
public class PriceGenerator extends Generator<Price> {
public PriceGenerator() {
super(Price.class);
}
@Override
public Price generate(SourceOfRandomness random, GenerationStatus status) {
return gen().fieldsOf(PriceFields.class)
.generate(random, status) …Run Code Online (Sandbox Code Playgroud) 我有一个与我的团队一起使用的相当大的svn集中存储库.现在我想在本地使用git(-svn),所以我做了一个git-svn克隆.这很好,但是git-svn克隆需要很长时间才能完成.相比之下,git克隆通常不需要那么长时间.
一个同事如何复制我的svn克隆,以便他不需要再次检查并转换整个svn历史记录?即没有与svn的互动.
当然,我们希望同事能够使用自己的凭据执行fetch和dcommit到中央svn存储库.
日Thnx.
java ×3
data-class ×1
foreach ×1
generics ×1
git ×1
junit ×1
kotlin ×1
quickcheck ×1
syntax ×1