Skip to content

Instantly share code, notes, and snippets.

@AllenSH12
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AllenSH12/0f6b91503c1102c40157 to your computer and use it in GitHub Desktop.
Save AllenSH12/0f6b91503c1102c40157 to your computer and use it in GitHub Desktop.
// 1
class Counter {
private var value: Int = 0
def increment() { value += 1 }
}
// 2
class BankAccount {
private var value: Double = 0.0
def deposit(d: Double) = { value = value + d }
def withdraw(d: Double) = { value = value - d }
def balance = value
}
// 3
class Time(val hours: Int, val minutes: Int) {
def before(other: Time): Boolean = other match {
case other if (hours == other.hours && minutes < other.minutes) => true
case other if (hours < other.hours) => true
case _ => false
}
}
// 4
class Time(val hours: Int, val minutes: Int) {
private val time_since_midnight = hours * 60 + minutes
def before(other: Time): Boolean = {
if (time_since_midnight < other.time_since_midnight) true else false
}
}
// 5
class Student {
@reflect.BeanProperty var name: String = _
@reflect.BeanProperty var id: Long = _
}
/*
$ javap Student
Compiled from "student.scala"
public class Student extends java.lang.Object{
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();
}
Yes, you can call the JavaBeans getters and setters in Scala.
Probably not, if both functions do the same thing it would seem better to have one uniform way to access a class
*/
// 6
class Person(a: Int) {
val age: Int = if (a < 0) 0 else a
}
// 7
class Person(var fullName: String) {
val firstName: String = fullName.split(" ").head
val lastName: String = fullName.split(" ").last
}
// val so it's consistent with the other properties of the class
// 8
class Car(var manufacturer: String, var model: String, var model_year: Int, var license_plate: String) {
def this(manufacturer: String, model: String) {
this(manufacturer, model, -1, "")
}
def this(manufacturer: String, model: String, model_year: Int) {
this(manufacturer, model, model_year, "")
}
def this(manufacturer: String, model: String, license_plate: String) {
this(manufacturer, model, -1, license_plate)
}
}
// 9
// Java
public static void main(String[] args) {
Car test = new Car("VW", "Jetta", "HA HA");
System.out.println(test.getModelYear());
System.out.println(test.getLicensePlate());
// -1
// HA HA
}
public class Car {
private String manufacturer;
private String model;
private int modelYear;
private String licensePlate;
public String getManufacturer() { return manufacturer; }
public String getModel() { return model; }
public int getModelYear() { return modelYear; }
public String getLicensePlate() { return licensePlate; }
public Car(String manufacturer, String model) {
this(manufacturer, model, -1, "");
}
public Car(String manufacturer, String model, int modelYear) {
this(manufacturer, model, modelYear, "");
}
public Car(String manufacturer, String model, String licensePlate) {
this(manufacturer, model, -1, licensePlate);
}
public Car(String manu, String mod, int year, String license) {
manufacturer = manu;
model = mod;
modelYear = year;
licensePlate = license;
}
}
// 10
class Employee(val name: String, var salary: Double) {
def this() { this("John Q. Public", 0.0) }
}
class Employee(n: String = "John Q. Public", s: Double = 0.0) {
val name = n
var salary = s
}
/*
the explicit form seems nicer, mostly because it skips the auxiliary constructor
it's method signature is pretty verbose though...
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment