Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Good programmer big data tutorials Scala series and so on

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

Good programmer big data tutorials Scala series and so on

Definition of class

The Scala access modifier is basically the same as Java's: private,protected,public.

If no access modifier is specified, by default, the access level of the Scala object is public.

Private (Private) member

Decorated with the private keyword, members with this tag are visible only within the class or object that contains the member definition, and the same rules apply to inner classes.

Class Outer {

Class Inner {

Private def f () {println ("f")}

Class InnerMost {

F () / / correct

}

}

(new Inner) .f () / / error

}

(new Inner) .f () access is illegal because f is declared as private in Inner, and access is not in class Inner.

However, there is no problem accessing f in InnerMost, because this access is contained in the Inner class.

These two kinds of access are allowed in Java because it allows external classes to access private members of inner classes.

Protect (Protected) members

In scala, access to Protected members is more stringent than java. Because it only allows protected members to be accessed in subclasses of the class in which the member is defined. In java, members modified with the protected keyword can be accessed not only by subclasses of the class that defines the member, but also by other classes in the same package.

Package p {

Class Super {

Protected def f () {println ("f")}

}

Class Sub extends Super {

F ()

}

Class Other {

(new Super) .f () / / error

}

}

Example of the definition of a class:

/ / define the Point class, and the constructor takes two parameters

Class Point (var x: Int, var y: Int) {

/ / Class methods with no return value

Def move (dx: Int, dy: Int): Unit = {

X = x + dx

Y = y + dy

}

/ / override methods that have no parameters but return values of type String

Override def toString: String =

S "($x, $y)"

}

/ / create an instance of the class

Val point1 = new Point (2,3)

Point1.x / / 2

Println (point1) / / prints (2,3)

The constructor can have default values:

Class Point (var x: Int = 0, var y: Int = 0) {

...

}

Val origin = new Point / / x, y all take the default value 0

Val point1 = new Point (1) / / Xue1jinyang0

Println (point1.x) / / prints 1

Private member variables and redefined Getter/Setter methods:

Private var _ x = 0

Private var _ y = 0

Privateval 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 in the class definition:

/ / in Scala, a class does not have to be declared public.

/ / the Scala source file can contain multiple classes, all of which have public visibility.

Class Person {

/ / variables decorated with val are read-only, with only getter methods but no setter methods

/ / (equivalent to variables decorated with final in Java)

/ / the field must be initialized

Val id = "1234"

/ / variables modified with var. By default, there are both public getter methods and setter methods.

Var age: Int = 18

/ / Class private fields with private getter and setter methods that can only be used inside the class

Private var name: String = "bachelor"

/ / the private field of the object. If the access permission is more strict, the method of the Person class can only access the field of the current object.

Private [this] val hobby = "Tourism"

}

In scala, you have the following four choices when implementing properties:

Var foo: Scala automatically synthesizes a getter and a setter

Val foo: Scala automatically synthesizes a getter

It is up to you to define the foo and foo_= methods

You define the foo method.

Constructor

Note:

1. The main constructor executes all statements in the class definition

two。 If the main constructor has parameters placed directly after the class name

Class ConstructorDemo (val id: Int) {… }

3. The main constructor becomes private, and you can place the private keyword like this:

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

At this point, the user must construct the Person object through an auxiliary constructor

Class ConstructorDemo {

Private var var1 = ""

Private var var2 = 0

/ / Auxiliary constructor 1

Def this (var1:String) {

This () / / calls the main constructor

This.var1 = var1

}

/ / Auxiliary Constructor 2

Def this (var1:String, var2:Int) {

This (var1) / / call auxiliary constructor 1

This.var2 = var2

}

}

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report