标签:
1. 改进5.1节的Counter类,让它不要在Int.MaxValue是变成负数。class
Counter{
private
var
value
=
Int.MaxValue
def
increment() { value
=
if
( value < Int.MaxValue) value +
1
else
value }
def
current
=
value
}
val
myCounter
=
new
Counter()
myCounter.increment()
println(myCounter.current)
/*result
2147483647
*/
class
BankAccount(
val
balance
:
Double
=
0.0
){
def
deposit() {}
def
withdraw() {}
}
val
obj
=
new
BankAccount(
100.00
)
println(obj.balance)
/*result
100.0
*/
class Time( val hours : Int, val minutes : Int){ def before(other : Time) : Boolean = { if (hours == other.hours) minutes < other.minutes else hours < other.hours } } val a = new Time( 9 , 0 ) val b = new Time( 9 , 0 ) val c = new Time( 9 , 30 ) val d = new Time( 10 , 0 ) println(a.before(b)) println(a.before(c)) println(a.before(d)) /*result false true true */ |
class
Time(
val
hours
:
Int,
val
minutes
:
Int){
private
val
timeMinutes
=
hours *
60
+ minutes
def
before(other
:
Time)
:
Boolean
=
{
timeMinutes < other.timeMinutes
}
}
val
a
=
new
Time(
9
,
0
)
val
b
=
new
Time(
9
,
0
)
val
c
=
new
Time(
9
,
30
)
val
d
=
new
Time(
10
,
0
)
println(a.before(b))
println(a.before(c))
println(a.before(d))
/*result
false
true
true
*/
public
java.lang.String name();
public
void
name_$eq(java.lang.String);
public
java.lang.String getName();
public
void
setName(java.lang.String);
public
long
id();
public
void
id_$eq(
long
);
public
long
getId();
public
void
setId(
long
);
import
scala.beans.BeanProperty
class
Student{
@
BeanProperty
var
name
:
String
=
_
@
BeanProperty
var
id
:
Long
=
_
}
val
a
=
new
Student
a.name
=
"Jonathan"
a.id
=
43344506
L
printf(
"%s‘s id is %d\n"
, a.name, a.id)
a.setName(
"Frank"
)
a.setId(
43344599
L)
printf(
"%s‘s id is %d\n"
, a.getName(), a.getId())
/*result
G:\share\scala>scalac e5-5.scala
G:\share\scala>javap Student.class
Compiled from "e5-5.scala"
public class Student {
public java.lang.String name();
public void name_$eq(java.lang.String);
public void setName(java.lang.String);
public long id();
public void id_$eq(long);
public void setId(long);
public java.lang.String getName();
public long getId();
public Student();
}
G:\share\scala>scala e5-5.scala
Jonathan‘s id is 43344506
Frank‘s id is 43344599
*/
class
Person(
var
age
:
Int){
if
(age<
0
) age
=
0
}
val
a
=
new
Person(-
1
)
printf(
"The person‘s age is %d\n"
, a.age)
/*result
The person‘s age is 0
*/
class
Person(
val
name
:
String){
val
firstName
=
name.split(
" "
)(
0
)
val
lastName
=
name.split(
" "
)(
1
)
}
val
a
=
new
Person(
"Jonathan Chen"
)
printf(
"The person‘s lastName is %s\n"
, a.lastName)
printf(
"The person‘s firstName is %s\n"
, a.firstName)
/*result
The person‘s lastName is Chen
The person‘s firstName is Jonathan
*/
class
Car(
val
maker
:
String,
val
typeName
:
String,
val
year
:
Int,
var
id
:
String){
def
this
(maker
:
String, typeName
:
String){
this
(maker, typeName, -
1
,
""
)
}
def
this
(maker
:
String, typeName
:
String, year
:
Int){
this
(maker, typeName, year,
""
)
}
def
this
(maker
:
String, typeName
:
String, id
:
String){
this
(maker, typeName, -
1
, id)
}
override
def
toString
=
"Maker:%s, TypeName:%s, Year:%d, Id:%s"
.format(maker, typeName, year, id)
}
val
a
=
new
Car(
"BMW"
,
"A6"
)
val
b
=
new
Car(
"BMW"
,
"A6"
,
2015
,
"TheOne"
)
val
c
=
new
Car(
"BMW"
,
"A6"
,
2015
)
val
d
=
new
Car(
"BMW"
,
"A6"
,
"TheOne"
)
println(a)
println(b)
println(c)
println(d)
/*result
Maker:BMW, TypeName:A6, Year:-1, Id:
Maker:BMW, TypeName:A6, Year:2015, Id:TheOne
Maker:BMW, TypeName:A6, Year:2015, Id:
Maker:BMW, TypeName:A6, Year:-1, Id:TheOne
*/
class
Employee{
val
name
:
String
=
"Join Q. Public"
var
salary
:
Double
=
0.0
}
val
a
=
new
Employee
/*result
*/
标签:
原文地址:http://www.cnblogs.com/chenjo/p/4436592.html