Good programmers Big Data tutorial series like Scala

Good programmers Big Data tutorial series like Scala

  1. Class definition

Scala and Java basic access modifiers, like, respectively: private, protected, public.

If no access modifier character, by default, the access level Scala objects are public.

Private (Private) members

Modified with private keyword, with the members of this tag only contains the class or object member defined internal visible, the same rule also applies to inner classes.

class Outer{
class Inner{
private def f(){println("f")}
class InnerMost{
f() // 正确
}
}
(new Inner).f() //错误
}

(New Inner) .f () to access illegal because in Inner f is declared as private, but access is not within the category of Inner.

But access InnerMost where f is no problem, because the access is included in the class Inner.

Java, allows access to both, because it allows external access to the private members of the class within the class.

Protection (Protected) members

In the scala, the access protection (Protected) membership more stringent than some of java. Because it only allows subclasses in the definition of the protected members of the class members are accessed. In java, a protected keywords modified members, in addition to the definition of a subclass of the class can access the member, with a package of other classes can also be accessed.

package p{
class Super{
protected def f() {println("f")}
}
class Sub extends Super{
f()
}
class Other{
(new Super).f() //错误
}
}

Examples of classes defined:

// define Point class constructor takes two parameters
class Point (var x: Int, var y: Int) {

//无返回值的类方法

def move(dx: Int, dy: Int): Unit = {
x = x + dx
y = y + dy
}

//没有参数但是返回值为String类型的重写方法

override def toString: String =
s"($x, $y)"
}

// Create an instance of the class
Val Point1 new new Point = (2,. 3)
point1.x 2 //
the println (Point1) // Prints (2,. 3)

May be configured with default values:

Point class (var X: Int = 0, Y var: Int = 0) {
...
}
Val Origin Point // new new = X, Y have the default value 0
Val Point1 = new new Point (. 1). 1 // X = , Y = 0
the println (point1.x). 1 // Prints

Private member variables and Getter / Setter methods redefined:

private var _x = 0
private var _y = 0
private val bound = 100

def x = x
def x
= (newValue: Int): Unit = {
if (newValue < bound) _x = newValue else printWarning
}

def y = y
def y
= (newValue: Int): Unit = {
if (newValue < bound) _y = newValue else printWarning
}

private def printWarning = println("WARNING: Out of bounds")
}

val point1 = new Point
point1.x = 99
point1.y = 101 // prints the warning

Other details of the class definition:

// In Scala, a class is not declared as public.
// Scala source file can contain multiple classes, these classes are public with visibility.
{the Person class
// modified with val variable is read-only property, but there is no method with only getter setter methods
// (corresponding to the final modified variable with Java)
// field must be initialized
val id = "1234"

// var using modified variables default at the same time have a public getter and setter methods
var age: Int = 18

// private class field, there are private getter and setter methods, can only be used within the class
private var name: String = "bachelor"

// objects private fields, access to more stringent method of Person class only access to the current object's fields
private [this] val hobby = "tourism"
}

scala, when you have the following attributes to achieve four options:

var foo: Scala automated synthesis a getter and a setter

val foo: Scala automatic synthesis of a getter

Up to you to define the method foo and foo_ =

Up to you to define the method foo

  1. Constructor
    Note:

1. The main structure will perform all of the class definition statements

2. Main constructor parameter if placed directly after the class name

class ConstructorDemo ( val id: Int ) { … }

3. The main constructor become private, private keyword can be placed like this:

class ConstructorDemo private ( val id: Int ) { … }

In this case, the user must be constructed by the auxiliary Person object constructor

class ConstructorDemo {

private var var1 = ""

private was var2 = 0

// constructor auxiliary. 1
DEF the this (var1: String) {
the this () // constructor calls a main
this.var1 = var1
}

// constructor auxiliary 2
DEF the this (var1: String, var2: Int) {
the this (var1) // constructor call auxiliary. 1
this.var2 = var2
}

}

Guess you like

Origin blog.51cto.com/14573321/2444474