English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Klassen sind die Abstraktion von Objekten, und Objekte sind konkrete Beispiele für Klassen. Klassen sind abstrakt und belegen keine Speicherplatz, während Objekte konkret sind und Speicherplatz beanspruchen. Klassen sind die Blaupausen zur Erstellung von Objekten, sie sind ein Softwaremuster, das Methoden und Variablen definiert, die in spezifischen Typen von Objekten enthalten sind.
我们可以使用 new 关键字来创建类的对象,示例如下:
class Point(xc: Int, yc: Int) { var x: Int = xc var y: Int = yc def move(dx: Int, dy: Int) { x = x + dx y = y + dy println ("x 的坐标点: " + x); println ("y 的坐标点: " + y); } }
Scala 中的类不声明为 public,一个 Scala 源文件中可以有多个类。
以上示例的类定义了两个变量 x 和 y ,一个方法:move,方法没有返回值。
Scala 的类定义可以有参数,称为类参数,如上面的 xc, yc,类参数在整个类中都可以访问。
接着我们可以使用 new 来示例化类,并访问类中的方法和变量:
import java.io._ class Point(xc: Int, yc: Int) { var x: Int = xc var y: Int = yc def move(dx: Int, dy: Int) { x = x + dx y = y + dy println ("x 的坐标点: " + x); println ("y 的坐标点: " + y); } } object Test { def main(args: Array[String]) { val pt = new Point(10, 20); // 移动到一个新的位置 pt.move(10, 10); } }
Execute the above code, the output is:
$ scalac Test.scala $ scala Test x 的坐标点: 20 y 的坐标点: 30
Scala 继承一个基类跟 Java 很相似,但我们需要注意以下几点:
1、重写一个非抽象方法必须使用 override 修饰符。
2、只有主构造函数才可以往基类的构造函数里写参数。
3、在子类中重写超类的抽象方法时,你不需要使用 override 关键字。
接下来让我们来看个示例:
class Point(xc: Int, yc: Int) { var x: Int = xc var y: Int = yc def move(dx: Int, dy: Int) { x = x + dx y = y + dy println ("x 的坐标点: " + x); println ("y 的坐标点: " + y); } } class Location(override val xc: Int, override val yc: Int, val zc: Int) extends Point(xc, yc){ var z: Int = zc def move(dx: Int, dy: Int, dz: Int) { x = x + dx y = y + dy z = z + dz println("x-Koordinatenpunkt: "); + x); println("y-Koordinatenpunkt: "); + y); println ("z 的坐标点 : " + z); } }
Scala 使用 extends 关键字来继承一个类。示例中 Location 类继承了 Point 类。Point 称为父类(基类),Location 称为子类。
override val xc 为重写了父类的字段。
继承会继承父类的所有属性和方法,Scala 只允许继承一个父类。
示例如下:
import java.io._ class Point(val xc: Int, val yc: Int) { var x: Int = xc var y: Int = yc def move(dx: Int, dy: Int) { x = x + dx y = y + dy println("x-Koordinatenpunkt: "); + x); println("y-Koordinatenpunkt: "); + y); } } class Location(override val xc: Int, override val yc: Int, val zc: Int) extends Point(xc, yc){ var z: Int = zc def move(dx: Int, dy: Int, dz: Int) { x = x + dx y = y + dy z = z + dz println("x-Koordinatenpunkt: "); + x); println("y-Koordinatenpunkt: "); + y); println ("z 的坐标点 : " + z); } } object Test { def main(args: Array[String]) { val loc = new Location(10, 20, 15); // 移动到一个新的位置 loc.move(10, 10, 5); } }
Execute the above code, the output is:
$ scalac Test.scala $ scala Test x-Koordinatenpunkt: 20 y-Koordinatenpunkt: 30 z 的坐标点 : 20
Scala 重写一个非抽象方法,必须使用 override 修饰符。
class Person { var name = "" override def toString = getClass.getName + "[name=" + name + "]" } class Employee extends Person { var salary = 0.0 override def toString = super.toString + "[salary=" + salary + "]" } object Test extends App { val fred = new Employee fred.name = "Fred" fred.salary = 50000 println(fred) }
Execute the above code, the output is:
$ scalac Test.scala $ scala Test Employee[name=Fred][salary=50000.0]
In Scala gibt es kein 'static', aber es bietet uns auch eine Implementierungsmethode für das Singleton-Pattern, nämlich den Schlüsselwort object.
Bei der Verwendung von Singleton-Pattern in Scala muss neben der definierten Klasse auch ein gleichnamiges object-Objekt definiert werden, der Unterschied zu der Klasse ist, dass das object-Objekt keine Parameter haben kann.
Wenn ein Singleton-Objekt denselben Namen wie eine Klasse teilt, wird es als deren Begleiter-Objekt bezeichnet: companion object. Sie müssen Klasse und ihren Begleiter-Objekt in derselben Quelldatei definieren. Die Klasse wird als Begleiterklasse des Singleton-Objekts bezeichnet: companion class. Eine Klasse und ihr Begleiter-Objekt können private Mitglieder voneinander besuchen.
import java.io._ class Point(val xc: Int, val yc: Int) { var x: Int = xc var y: Int = yc def move(dx: Int, dy: Int) { x = x + dx y = y + dy } } object Test { def main(args: Array[String]) { val point = new Point(10, 20) printPoint def printPoint{ println("x-Koordinatenpunkt: "); + point.x); println("y-Koordinatenpunkt: "); + point.y); } } }
Execute the above code, the output is:
$ scalac Test.scala $ scala Test x-Koordinatenpunkt: 10 y-Koordinatenpunkt: 20
/* Dateiname: Marker.scala * Autor: Grundlagen-Tutorial-Website * url:de.oldtoolbag.com */ // Privater Konstruktor class Marker private(val color: String) { println("Create" + this) override def toString(): String = "Color Mark:"+ color } // Companion object, with the same name as the class, can access the private properties and methods of the class object Marker{ private val markers: Map[String, Marker] = Map( "red" -> new Marker("red"), "blue" -> new Marker("blue"), "green" -> new Marker("green") ) def apply(color: String) = { if(markers.contains(color)) markers(color) else null } def getMarker(color: String) = { if(markers.contains(color)) markers(color) else null } def main(args: Array[String]) { println(Marker("red")) // Singleton function call, omitting the '.' symbol println(Marker getMarker "blue") } }
Execute the above code, the output is:
$ scalac Marker.scala $ scala Marker Create Color Mark: red Create Color Mark: blue Create Color Mark: green Color Mark: red Color Mark: blue