Last active
March 29, 2016 23:02
-
-
Save seralf/355ec63a1445f2bd9b91f5e97d539362 to your computer and use it in GitHub Desktop.
examples with simple Scala "bean" classes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package examples.scala.php | |
object MainProve extends App { | |
Predef println "Hello world!" | |
val p1 = new Person1("John", "Doe") | |
println(p1) | |
val p2 = new Person2("John", "Doe") | |
p2.age = 42 | |
println(p2) | |
val p3 = new Person3() | |
p3.firstName = "John" | |
p3.lastName = "Doe" | |
println(p3) | |
try { | |
p3.firstName = "NO" | |
} catch { | |
case e: Throwable => println("WARNING" + e.getMessage) | |
} | |
} | |
case class Person1(firstName: String, lastName: String) | |
case class Person2(firstName: String, lastName: String) { | |
private var my_age: Int = 0 | |
def age = this.my_age | |
def age_=(a: Int) { | |
require(a >= 0) | |
this.my_age = a | |
} | |
override def toString() = s"Person[${firstName}, ${lastName}, ${age}]" | |
} | |
/* | |
* Java Like example. | |
*/ | |
class Person3 { | |
private var first_name: String = null | |
private var last_name: String = null | |
private var my_age: Int = 0 | |
def firstName = first_name | |
def firstName_=(name: String) = { | |
require(name != null && name.length() >= 3) | |
this.first_name = name | |
} | |
def lastName = last_name | |
def lastName_=(name: String) = { | |
require(name != null && name.length() >= 3) | |
this.last_name = name | |
} | |
def age = this.my_age | |
def age_=(a: Int) { | |
require(a >= 0) | |
this.my_age = a | |
} | |
override def toString() = s"Person[${firstName}, ${lastName}, ${my_age}]" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment