关闭

快学scala习题(五)

148人阅读 评论(0) 收藏 举报
分类:

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:
       0: aload_0
       1: aload_1
       2: putfield      #14                 // Field name:Ljava/lang/String;
       5: return

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

  public long id();
    Code:
       0: aload_0
       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;
      11: aload_0
      12: invokespecial #41                 // Method java/lang/Object."<init>":
()V
      15: aload_0
      16: ldc           #43                 // String
      18: putfield      #14                 // Field name:Ljava/lang/String;
      21: aload_0
      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.}  
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:16961次
    • 积分:620
    • 等级:
    • 排名:千里之外
    • 原创:43篇
    • 转载:0篇
    • 译文:0篇
    • 评论:2条
    文章分类
    最新评论