Scala練習五
1. 改進Counter類,讓它不要在Int.MaxValue時變成負數
程式程式碼:
- class Counter {
- private var value=100
- def increment(){
- if(value<Int.MaxValue)
- value+=1
- else
- value
- }
- def current=value
- }
- object Counter{
- def main(args: Array[String]): Unit = {
- val max=Int.MaxValue
- println("Int型別的最大值:"+max)
- val counter=new Counter
- for(i <- 1 to (max))
- counter.increment()
- println("經過"+max+"增加後Value值為:"+counter.current)
- }
- }
執行結果:
Int型別的最大值:2147483647
經過2147483647增加後Value值為:2147483647
2. 編寫一個BankAccount類,加入deposit和withdraw方法,和一個只讀的balance屬性
程式程式碼:
- class BankAccount {
- private var balance=0.0
- def deposit(depamount:Double){
- balance+=depamount
- }
- def withdraw(drawamount:Double){
- balance-=drawamount
- }
- def current=balance
- }
- object BankAccount{
- def main(args: Array[String]): Unit = {
- val Drawamount=800
- val Depamount=1000
- val acc=new BankAccount
- println("存入金額:"+Depamount)
- acc.deposit(Depamount)
- println("餘額:"+acc.current)
- println("取出金額:"+Drawamount)
- acc.withdraw(Drawamount)
- println("餘額為:"+acc.current)
- }
- }
執行結果:
存入金額:1000
餘額:1000.0
取出金額:800
餘額為:200.0
3. 編寫一個Time類,加入只讀屬性hours和minutes,和一個檢查某一時刻是否早於另一時刻的方法 before(other:Time):Boolean。Time物件應該以new Time(hrs,min)方式構建。其中hrs以軍用時間格式呈現(介於0和23之間)
程式程式碼:
- class Time(val hours:Int , val minutes:Int) {
- def before(other:Time):Boolean={
- hours<other.hours||(hours==other.hours&&minutes<other.minutes)
- }
- override def toString():String={
- hours+":"+minutes
- }
- }
- object Time{
- def main(args: Array[String]): Unit = {
- val t1=new Time(10,30)
- val t2=new Time(10,50)
- val t3=new Time(11,10)
- println("t1時刻是:"+t1.toString())
- println("t2時刻是:"+t2.toString())
- println("t3時刻是:"+t3.toString())
- println("t1時刻早於t2嗎:"+t1.before(t2))
- println("t3時刻早於t2嗎:"+t3.before(t2))
- }
- }
執行結果:
t1時刻是:10:30
t2時刻是:10:50
t3時刻是:11:10
t1時刻早於t2嗎:true
t3時刻早於t2嗎:false
4. 重新實現前一個類中的Time類,將內部呈現改成午夜起的分鐘數(介於0到24*60-1之間)。不要改變公有介面。也就是說,客戶端程式碼不應因你的修改而受影響
程式程式碼:
- class NewTime(val hours:Int,val minutes:Int) {
- def before(other:NewTime):Boolean={
- hours < other.hours||(hours==other.hours&&minutes<other.minutes)
- }
- override def toString():String={
- hours*60+""+minutes
- }
- }
- object NewTime{
- def main(args: Array[String]): Unit = {
- val t1=new NewTime(10,30)
- val t2=new NewTime(10,50)
- val t3=new NewTime(11,10)
- println("t1時刻:"+t1.toString())
- println("t2時刻:"+t2.toString())
- println("t3時刻:"+t3.toString())
- println("t1時刻比t2早嗎:"+t1.before(t2))
- println("t3時刻比t2早嗎:"+t3.before(t2))
- }
- }
執行結果:
t1時刻:60030
t2時刻:60050
t3時刻:66010
t1時刻比t2早嗎:true
t3時刻比t2早嗎:false
5. 建立一個Student類,加入可讀寫的JavaBeans屬性name(型別為String)和id(型別為Long)。有哪些方法被生產?你可以在Scala中呼叫JavaBeans的getter和setter方法嗎?應該這樣做嗎?
程式程式碼:
class Student {
var name:String=null
var id:Long=0
}
執行命令:javap -private Student
執行結果:
使用@BeanProperty增加相容JavaBean的get和set方法:
import scala.beans.BeanProperty
class Student {
@BeanProperty
var name:String=null
@BeanProperty
var id:Long=0
}
執行命令: javap -private Student
執行結果:
6. 在Person類中提供一個主構造器,將負年齡轉換為0
程式程式碼:
- class Person(var age:Int){
- if(age < 0) age=0
- }
- object Person{
- def main(args: Array[String]): Unit = {
- val age1 = 10
- val age2 = -20
- println("將Tom的年齡初始化為:"+age1)
- val Tom=new Person(age1)
- println("Tom的實際年齡為:"+Tom.age)
- println("將Tom的年齡初始化為:"+age2)
- val Jhon=new Person(age2)
- println("Jhon的實際年齡為:"+Jhon.age)
- }
- }
執行結果:
將Tom的年齡初始化為:10
Tom的實際年齡為:10
將Jhon的年齡初始化為:-20
Jhon的實際年齡為:0
7. 編寫一個Person類,其主構造器接受一個字串,該字串包含名字,空格和姓名,如new Person("Fred Smith")。提供只讀屬性firstName和lastName。主構造器引數應該是var,val還是普通引數,為什麼
描述:name應該設定為val以防止被修改
程式程式碼:
- class Person(val name:String) {
- private val namearr=name.split("")
- def FirstName=namearr(0)
- def LastName=namearr(1)
- }
- object Person{
- def main(args: Array[String]): Unit = {
- val person=new Person("Xinyu Jiang")
- //name引數自動轉為私有欄位,並生成公有getter
- println("person的名稱為:"+person.name)
- println("person的FisrtName:"+person.FirstName)
- println("person的LastName:"+person.LastName)
- }
- }
執行結果:
person的名稱為:Xinyu Jiang
person的FisrtName:Xinyu
person的LastName:Jiang
8. 建立一個Car類,以只讀屬性對應制造商,型號名稱,型號年份以及一個可讀寫的屬性用於車牌。提供四組構造器。每個構造器都要求製造商和型號為必填。型號年份和車牌可選,如果未填,則型號年份為-1,車牌為空串。你會選擇哪一個作為你的主構造器,為什麼
程式程式碼:
- class Car(val manufactor:String,val model:String=null,val year:String=null,var number:Int= -1){
- }
- object Car{
- def main(args: Array[String]): Unit = {
- val Chevrolet=new Car("通用","雪佛蘭-愛唯歐")
- val Volkswagen=new Car("一汽","大眾-斯柯達","2015-1-1")
- val Volvo=new Car("吉利","Volvo-s40","2015-1-2",666666)
- val nameArr=Array("雪佛蘭","大眾","沃爾沃")
- val carArr=Array(Chevrolet,Volkswagen,Volvo)
- Outinfo(nameArr,carArr)
- }
- def Outinfo(carName:Array[String],carArr:Array[Car])={
- for(i <- 0 until carName.length){
- println(carName(i))
- println("汽車製造商為: "+carArr(i).manufactor)
- println("汽車型號為: "+carArr(i).model)
- println("汽車產年份為: "+carArr(i).year)
- println("汽車車牌號為: "+carArr(i).number)
- }
- }
- }
執行結果:
雪佛蘭
汽車製造商為: 通用
汽車型號為: 雪佛蘭-愛唯歐
汽車產年份為: null
汽車車牌號為: -1
大眾
汽車製造商為: 一汽
汽車型號為: 大眾-斯柯達
汽車產年份為: 2015-1-1
汽車車牌號為: -1
沃爾沃
汽車製造商為: 吉利
汽車型號為: Volvo-s40
汽車產年份為: 2015-1-2
汽車車牌號為: 666666
9. 在Java,C#或C++重做前一個練習。Scala相比之下精簡多少
程式程式碼:
- class JavaCar{
- private String manufactor;
- private String model;
- private String year;
- private int number;
- public JavaCar(){}
- public JavaCar(String manufactor,String model){
- this.manufactor=manufactor;
- this.model=model;
- this.year=null;
- this.number=-1;
- }
- public JavaCar(String manufactor,String model,String year){
- this.manufactor=manufactor;
- this.model=model;
- this.year=year;
- this.number=-1;
- }
- public JavaCar(String manufactor,String model,String year,int number){
- this.manufactor=manufactor;
- this.model=model;
- this.year=year;
- this.number=number;
- }
- public String getManufactor(){
- return this.manufactor;
- }
- public String getModel(){
- return this.model;
- }
- public String getYear(){
- return this.year;
- }
- public void setNumber(int number){
- this.number=number;
- }
- public int getNumber(){
- return number;
- }
- public static void main(String[] args) {
- }
- }
- public class CarTest{
- public static void main(String[] args) {
- JavaCar Chevrolet=new JavaCar("通用","雪佛蘭-愛唯歐");
- JavaCar Volkswagen=new JavaCar("一汽","大眾-斯柯達","2015-1-1");
- JavaCar Volvo=new JavaCar("吉利","Volvo-S40","2015-1-2",66666);
- String[] nameArr={"雪佛蘭","大眾","沃爾沃"};
- JavaCar[] carinfoArr={Chevrolet,Volkswagen,Volvo};
- CarTest cartest=new CarTest();
- cartest.OutInfo(nameArr,carinfoArr);
- }
- public void OutInfo(String[] nameArr,JavaCar[] carinfoArr){
- for(int i=0;i<nameArr.length;i++){
- System.out.println(nameArr[i]);
- System.out.println("汽車製造商: "+carinfoArr[i].getManufactor());
- System.out.println("汽車型號: "+carinfoArr[i].getModel());
- System.out.println("汽車年份: "+carinfoArr[i].getYear());
- System.out.print("車牌號: "+carinfoArr[i].getNumber());
- }
- }
- }
執行結果:
雪佛蘭
汽車製造商: 通用
汽車型號: 雪佛蘭-愛唯歐
汽車年份: null
車牌號: -1
大眾
汽車製造商: 一汽
汽車型號: 大眾-斯柯達
汽車年份: 2015-1-1
車牌號: -1
沃爾沃
汽車製造商: 吉利
汽車型號: Volvo-S40
汽車年份: 2015-1-2
車牌號: 66666
10. 考慮如下的類
class Employ(val name:String,var salary:Double){
def this(){ this ("John Q. Public",0.0) }
}
重寫該類,使用顯示的欄位定義,和一個預設主構造器。你更傾向於使用哪種形式,為什麼
程式程式碼:
- class Employee(var name:String,var salary:Double){
- def this(){
- this("Jom Q. Public",0)
- }
- }
- object Employee{
- def main(args: Array[String]): Unit = {
- val ITemployee=new Employee
- val Bankemployee=new Employee("Tom Hanks",100000)
- println("ITemployee Name: "+ITemployee.name+" Salary: "+ITemployee.salary)
- println("Bankemployee Name: "+Bankemployee.name+" Salary: "+Bankemployee.salary)
- }
- }
執行結果:
ITemployee Name: Jom Q. Public Salary: 0.0
Bankemployee Name: Tom Hanks Salary: 100000.0