|
final
User user = getUser();
|
| |
|
| |
val user: User = getUser()
|
|
final
User user = getuser();
|
| |
val a =
"$user is ${user.age}"
|
|
StringBuilder _builder =
new
StringBuilder();
_builder.append(user);
_builder.append(
" is "
);
_builder.append(user.getAge());
final
String a = _builder.toString()
|
| |
|
final
String a = b ==
null
?
""
: b;
|
| |
val a = b ?:
throw
BusinessException()
|
|
if
(b ==
null
) {
throw
new
BusinessException();
}
final
String a = b;
|
| |
|
final
String _tmp = getB();
final
a = _tmp ==
null
?
""
: _tmp;
|
| |
val a = getB()?.getC()?.getD() ?:
""
|
|
String _tmp;
final
B _b = getB();
if
(_b !=
null
) {
final
C _c = _b.getC();
if
(_c !=
null
) {
final
String _d = _c.getD();
if
(_d !=
null
) {
_tmp = _d;
}
else
{
_tmp =
""
;
}
else
{
_tmp =
""
;
}
}
else
{
_tmp =
""
;
}
final
String a = _tmp;
|
| |
|
public
final
class
User {}
|
| |
class
User(age: Int, name: String) {
init {
println(
"$name is $age"
)
}
}
|
|
public
final
class
User {
public
User(Integer age, String name) {
StringBuilder _builder =
new
StringBuilder();
_builder.append(name);
_builder.append(
" is "
);
_builder.append(age);
System.out.println(_builder.toString());
}
}
|
| |
class
User(val age: Int?, open var name: String?)
//没有new
val user = User(
18
,
"hello"
)
println(user.age)
user.name =
"bike"
|
|
public
final
class
User {
private
Integer age;
private
String name;
public
final
User(Integer age, String name) {
this
.age = age;
this
.name = name;
}
public
final
Integer getAge() {
return
this
.age;
}
public
String getName() {
return
this
.name;
}
public
void
setName(String name) {
this
.name = name;
}
}
final
User user =
new
User(
18
,
"hello"
);
System.out.println(user.getAge());
user.setName(
"bike"
);
|
| var有setter,val没有。 没有open的setter getter都是 final。 |
open
class
User(var age: Int)
|
|
public
class
User {
private
Integer age;
public
class
User(Integer age) {
checkParameterIsNotNull(age,
"age"
);
this
.age = age;
}
public
Integer getAge() {
return
this
.age;
}
public
void
setAge(Integer age) {
checkParameterIsNotNull(age,
"age"
);
this
.age = age;
}
}
|
| 自动插入校验空的代码 |
open
class
User {
val age: Int? =
0
var name: String? =
null
}
|
|
public
class
User {
private
Integer age =
0
;
private
String name =
null
;
public
final
Integer getAge() {
return
this
.age;
}
public
final
String getName() {
return
this
.name;
}
public
final
void
setName(String name) {
this
.name = name;
}
}
|
| |
class
A(a: String?): B(a), C
|
|
public
final
class
A
extends
B
implements
C {
public
A (String a) {
B(a);
}
}
|
| 继承,实现,继承时必须指定构造函数链。 |
|
| |
|
| |
|
public
class
A<T
extends
B> {}
|
| |
open
class
A<T> where T: B1, T: B2
|
|
public
class
A<T
extends
B1 & B2>
|
| |
fun test(age: Int?) : String? {
return
"OK"
}
|
|
public
final
String test(Integer age) {
return
"OK"
;
}
|
| |
fun test(age: Int) =
"OK"
|
|
public
final
String test(Integer age) {
checkParameterIsNotNull(age,
"age"
);
return
"OK"
;
}
|
| |
|
| |
|
| |
fun test(age: Int?, name: String? =
"name"
) {}
|
|
public
final
test(Integer age) {
test(age,
"name"
);
}
public
final
test(Integer age, String name) {
}
|
| |
val a =
if
(getValue())
"OK"
else
"NO"
|
|
final
String a = getValue() ?
"OK"
:
"NO"
;
|
| |
|
| a的类型自动推断了 |
|
for
(
int
i =
1
; i <
3
; i++) {}
|
| |
when (a) {
is TypeA => println(
"TypeA"
)
3
=> println(
"3"
)
test(a) => println(
"test"
)
else
=> println(
"else"
)
}
|
|
if
(a
instanceof
TypeA) {
System.out.println(
"TypeA"
);
}
else
if
(a.equals(
3
)) {
System.out.println(
"3"
);
}
else
if
(test(a)) {
System.out.println(
"test"
);
}
else
{
System.out.println(
"else"
);
}
|
| |
val a = when (b) {
is TypeA =>
1
3
=>
2
else
=>
3
}
|
|
Integer _tmp =
null
;
if
(a
instanceof
TypeA) {
_tmp =
1
;
}
else
if
(a.equals(
3
)) {
_tmp =
2
;
}
else
{
_tmp =
3
;
}
final
Integer a = _tmp;
|
| |
object A {
var name: String? =
null
fun test(a: String?) = a
}
|
|
public
final
class
A {
private
static
String name;
public
static
final
A INSTANCE;
private
A {}
static
{
INSTANCE =
new
A();
}
public
final
void
setName(String name) {
A.name = name;
}
public
final
String getName() {
return
A.name;
}
public
static
final
String test(String a) {
return
a;
}
}
|
| A类不能有实例变量。 |
val maxFun = {a: Int, b: Int ->
if
(a > b) a
else
b}
|
|
interface
_L {
Integer _f(Integer a, Integer b);
}
final
_L maxFun = (Integer a, Integer b) -> a > b ? a : b
|
| _L _f等是编译器自动生成的。 |
fun test(f: (Int, Int) -> Int): Int {
return
f(
1
,
2
)
}
test({a, b -> a + b})
//最后一个参数是闭包,该参数可以移动到小括号后
test() {a, b -> a + b}
//仅有一个闭包参数,小括号也可以省略
test {a, b -> a + b}
|
|
interface
_L {
Integer _f(Integer a, Integer b);
}
Integer test(_L f) {
return
f._f(
1
,
2
);
}
test((Integer a, Integer b) -> a + b);
test((Integer a, Integer b) -> a + b);
test((Integer a, Integer b) -> a + b);
|
| |
fun test(f: (Int) -> Int): Int {
return
f(
1
);
}
//闭包只有一个参数时,可用it代表
test { it +
2
}
|
|
interface
_L {
Integer _f(Integer a);
}
Integer test(_L f) {
return
f._f(
1
);
}
test((Integer a) -> a +
2
);
|
| |
fun String.isNullOrEmpty(): Boolean {
return
this
==
null
||
this
.length() ==
0
;
}
"aString"
.isNullOrEmpty()
|
|
public
static
Boolean isNullOrEmpty(String _this) {
return
_this ==
null
|| _this.length() ==
0
;
}
isNullOrEmpty(
"aString"
)
|
| DSL,或改进易读性用 |
infix fun Int.add(other: Int) =
this
+ other
val a =
3
add
4
|
|
public
static
Integer add(Integer _this, Integer other) {
return
_this + other;
}
final
Integer a = add(
3
,
4
);
|
| DSL用,一般不用 |
//该函数会在编译器尾递归优化
tailrec fun recursive(a: Int) : Int {
return
if
(a ==
0
)
0
else
recursive(a--)
}
|
| | |
//该函数会在编译期内联
inline fun isEven(a: Int) = a %
2
==
0
|
| | |
fun a() {
fun b() {}
b()
}
|
|
public
void
a() {
class
_B {
static
void
b() {}
}
_B.b();
}
|
| |