public class Caesar {
private String name;
public Caesar(String name) {
this.name = name;
}
public void say() {
for(String l : list) {
System.out.println(l);
}
}
private static List<String> list = new ArrayList<String>();
static {
list.add("veni");
list.add("vidi");
list.add("vici");
}
}
class Caesar(val name:String) {
def say():Unit = Caesar.list.foreach(println)
}
object Caesar {
//Analog zu Java
var list = List[String]()
list += "veni"
list += "vidi"
list += "vici"
//Oder besser gleich
//val list = List("veni","vidi","vici")
}
class Abc {
static int a;
static {
InputStream in = ...
....
a = in.read()
}
}
object Abc {
var a
// call static
myStaticKonstr
def myStaticKonstr = {
....
}
}
object Abc {
val a = {
InputStream in = ...
....
in.read()
}
}
val (x,y) = {
val loc = myFrame.getLocation
(loc.x, loc.y)
}
def this(a, b, c) = this(a, _, c)
public NumericArray(NumericArray n) ....
public NumericArray(int[] mag) ....
public NumericArray(int val) ....
public NumericArray(String s) ...
...
val big1 = new NumericArray("123")
val big2 = new NumericArray(222)
class Bla(a:String, b:String, c:String) { //<--Hauptkonstruktor
def this(a:String, b:String) = this(a,b,"cool")
def this(a:String) = this(a,"ist","cool")
def this() = this("Scala","ist","cool")
}
//Scala 2.8
class Bla(a:String = "Scala", b:String = "ist", c:String = "cool") {
}
class NumericArray(int[] mag) {
}
object NumericArray { //<--Begleitobjekt, companion object
def apply(n: NumericArray) = new NumericArray(n.mag)
def apply(value: Int) = new NumericArray(Array(value))
def apply(s: String) = new NumericArray(convertSomehowStringToIntArray(s))
}
//Aufrufe
//Hauptkonstruktor
val a = new NumericArray(Array(2,3,4,5))
//Factory-Methoden
val b = NumericArray(13)
val c = NumericArray("134957495749580569")
val d = NumericArray(c)