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)/*result2147483647*/class BankAccount(val balance:Double=0.0){ def deposit() {} def withdraw() {}}val obj = new BankAccount(100.00)println(obj.balance)/*result100.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))/*resultfalsetruetrue*/ |
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))/*resultfalsetruetrue*/ 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.BeanPropertyclass Student{ @BeanProperty var name: String = _ @BeanProperty var id: Long = _}val a = new Studenta.name = "Jonathan"a.id = 43344506Lprintf("%s‘s id is %d\n", a.name, a.id)a.setName("Frank")a.setId(43344599L)printf("%s‘s id is %d\n", a.getName(), a.getId())/*resultG:\share\scala>scalac e5-5.scalaG:\share\scala>javap Student.classCompiled 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.scalaJonathan‘s id is 43344506Frank‘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)/*resultThe 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)/*resultThe person‘s lastName is ChenThe 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)/*resultMaker:BMW, TypeName:A6, Year:-1, Id:Maker:BMW, TypeName:A6, Year:2015, Id:TheOneMaker: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