Scala Study --- override

以前没使用过Scala, 其实我Java也是半截水平\无奈, 学Java的时候刚从C++中挣脱出来,发现Java无比优雅,但很快又对Java种种不信任程序员的设计感到受限。

直到, ,

今天遇到了Scala\撒花

Scala的collection设计不能更赞!一段时间后打算专门写篇文章总结Scala,名字就叫“我为什么喜欢Scala!”。

废话就不多说了,今天研究了一下Scala的override用法与特点。

override --- one of the key words of Scala

>>General look of override

  1. 重写 :可以重写成员变量和成员方法(重写字段或者方法)
  2. 子类继承父类

>>Details

Scala中使用extends关键字进行扩展,同Java,例如:

class A extends B { }

如果没有extends子句,则默认继承自scala.AnyRef类

重写:

Scala中重写使用override关键字

定义参数化字段:

因为Scala可以在声明类的时候顺道声明一些变量,为了更方便的扩展这些声明,可以使用一些关键字,例如 private, override等,例如:

class X(override val a : Int, private val b : Int) extends P {}

这样,在类X中,a会覆盖父类的变量或方法, 而b 则为私有的。

调用超类的构造方法:

class X(s : String) extends P(s) { }

即,在extends后面的父类中,直接把参数传入即可,与C#的相似

时间: 2024-12-12 08:41:43

Scala Study --- override的相关文章

Beginning Scala study note(5) Pattern Matching

The basic functional cornerstones of Scala: immutable data types, passing of functions as parameters and pattern matching. 1. Basic Pattern Matching In Scala, your cases can include types, wildcards, sequences, regular expressions, and so forth. scal

Beginning Scala study note(7) Trait

A trait provides code reusability in Scala by encapsulating method and state and then offing possibility of mixing them into classes thus allowing code reuse. #define Trait Gliding scala> trait Gliding{ | def gliding(){ println("gliding")} |

Beginning Scala study note(3) Object Orientation in Scala

1. The three principles of OOP are encapsulation(封装性), inheritance(继承性) and polymorphism(多态性). example: class Shape{ def area: Double = 0.0 } # supertype # subtypes class Rectangle(val width: Double, val height: Double) extends Shape{ override def ar

Beginning Scala study note(8) Scala Type System

1. Unified Type System Scala has a unified type system, enclosed by the type Any at the top of the hierarchy and the type Nothing at the bottom of the hierarchy. All Scala types inherit from Any. # Using Any, Book extends AnyRef, and x is an Int that

Beginning Scala study note(9) Scala and Java Interoperability

1. Translating Java Classes to Scala Classes Example 1: # a class declaration in Java public class Book{} # Scala equivalent of a class declaration class Book Example 2: # a Java class with a Construtor public class Book{ private final int isbn; priv

Beginning Scala study note(6) Scala Collections

Scala's object-oriented collections support mutable and immutable type hierarchies. Also support functional higher-order operations such as map, filter, and reduce that let you use expression-oriented programming in collections. Higher-order operatio

Beginning Scala study note(4) Functional Programming in Scala

1. Functional programming treats computation as the evaluation of mathematical and avoids state and mutable data. Scala encourages an expression-oriented programming(EOP) 1) In expression-oriented programming every statement is an expression. A state

Beginning Scala study note(2) Basics of Scala

1. Variables (1) Three ways to define variables: 1) val refers to define an immutable variable; scala> val x = 10 x: Int = 10 scala> x*x res4: Int = 100 scala> res4 + 1 # use result as a value res6: Int = 101 scala> res4 + res6 res7: Int = 201

scala study

http://docs.scala-lang.org/tutorials/tour/tour-of-scala.html http://www.scala-lang.org/documentation/ http://docs.scala-lang.org/overviews/?_ga=2.8349685.636410797.1498641561-1908126350.1495619268 http://docs.scala-lang.org/overviews/core/futures.htm