# 快学scala习题（五）

1.Int.MaxValue代表着Int的最大值，一旦越过就会导致值变为负数，所以，需要在Increment里面加上判断语句

 class Counter{
private var value=Int.MaxValue;
def increment():Unit={
if(value<Int.MaxValue){
value=value+1
}
};
def current=value;

}

2.因为只有可读属性，所以需要用到val，仅生成get

 class BankAccount{
val balance:Int=0;
def deposit(){

}
def withdraw(){

}

}

3.

   class Time(val hours:Int,val minutes:Int){
def before(other: Time):Boolean={
if( hours>other.hours){
true;
}else if(hours==other.hours){
if(minutes>other.minutes)
true;
else
false;
}else{
false;
}

}
def show=hours+":"+minutes;
}

4.

class Time(val hours:Int,val minutes:Int){
def before(other: Time):Boolean={
if( hours>other.hours){
true;
}else if(hours==other.hours){
if(minutes>other.minutes)
true;
else
false;
}else{
false;
}

}
def show=hours*60+minutes;
}

5.使用javabean属性，旧版在scala.reflect.BeanProperty中，新版换到scala.Bean.BeanPropoerty，使用前请根据版本加入对应的包

import scala.beans.BeanProperty;
class Student{
@BeanProperty var name:String="";
@BeanProperty var id:Long=0;
}

javap的反编译方式如下

C:\Users\pengchen>javap -c C:\Users\pengchen\workspace\hello\bin\five$Student.cl ass Compiled from "five.scala" public class five$Student {
public final five $outer; public java.lang.String name(); Code: 0: aload_0 1: getfield #14 // Field name:Ljava/lang/String; 4: areturn public void name_$eq(java.lang.String);
Code:
2: putfield      #14                 // Field name:Ljava/lang/String;
5: return

public void setName(java.lang.String);
Code:
2: putfield      #14                 // Field name:Ljava/lang/String;
5: return

public long id();
Code:
1: getfield      #23                 // Field id:J
4: lreturn

public void id_$eq(long); Code: 0: aload_0 1: lload_1 2: putfield #23 // Field id:J 5: return public void setId(long); Code: 0: aload_0 1: lload_1 2: putfield #23 // Field id:J 5: return public java.lang.String getName(); Code: 0: aload_0 1: invokevirtual #29 // Method name:()Ljava/lang/String; 4: areturn public long getId(); Code: 0: aload_0 1: invokevirtual #32 // Method id:()J 4: lreturn public five five$Student$outer(); Code: 0: aload_0 1: getfield #36 // Field$outer:Lfive;
4: areturn

public five$Student(five); Code: 0: aload_1 1: ifnonnull 6 4: aconst_null 5: athrow 6: aload_0 7: aload_1 8: putfield #36 // Field$outer:Lfive;
12: invokespecial #41                 // Method java/lang/Object."<init>":
()V
16: ldc           #43                 // String
18: putfield      #14                 // Field name:Ljava/lang/String;
22: lconst_0
23: putfield      #23                 // Field id:J
26: return
}


6.改造5.2的person类，使Age<0时让其等于0，var保证其生成getter和setter

 class person(var Age:Int){
if(Age<0) Age=0;

}

7.改造person类，提供只读属性firstName和lastName，这里需要用到val类型，避免生成setter对象

 class PersonE(val Name:String){
def firstName=Name.split(" ")(0);
def LastName=Name.split(" ")(1)

}

8.创建一个car类，题目没看懂。。。。4个构造器都一样应该会报错才对。。。

  /*4组属性制造商必填，型号名称必填，型号年份选填-1，车牌选填“”可读可写*/
class Car(val Pub:String,val TypeName:String,val Year:Int= -1,var Num:String=""){

}

9用java或c++实现第8题同样的类，这个就不用写了吧

10.

1.class Employ{
2.    val name:String = "John Q. Public"
3.    var salary:Double = 0.0
4.}  

08-20 1314

12-06 1440

#### 快学Scala 课后习题答案集合

2015年09月13日 1.81MB 下载

12-10 1643

05-14 1229

08-20 1145

07-11 298

05-14 866

#### 快学Scala-课后习题答案-源码

2017年08月17日 178KB 下载

05-13 824