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
如果,您认为阅读这篇博客让您有些收获,不妨点击一下右下角的【推荐】。
如果,您希望更容易地发现我的新博客,不妨点击一下左下角的【关注我】。
如果,您对我的博客所讲述的内容有兴趣,请继续关注我的后续博客,我是【Sunddenly】。本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。