1.class
Scala classes are a bit different from classes in C #, such as declaring a field that is not priavate decorated var age,scala compiler field helps us to produce a private field and 2 public method get and set, which is similar to C # 's simple properties; If you use the private adornment , its methods will also be private. This is the so-called unified access principle.
Too much detail, or the code in the comments.
[Java]View PlainCopy
- Class is the public level by default
- Class person{
- var age= //field must be initialized ()
- def age=age //This is the method, no parameters can be omitted ()
- Def incremen () {this.age+=1}
- }
- Class student{
- var age= ///The underlying compiler automatically adds a public method of get and set to the private age, which can be understood as pseudo type
- private[this] var gender="male" //private[this] only this class can use the
- Private var name="Clow" //declares private, the underlying compiler automatically adds the get and set proprietary methods for the private name
- //But you can define your own property methods
- def getname=THIS.name
- def setName (value:string) {This.name=value}
- }
- Use of constructors
- Class Teacher {
- var Age:int = _
- var name:string = _ //Can be reserved
- //Overloaded constructors are similar to the public Teacher () {} inside C #
- def this (Age:int, name:string) {
- This () //must be called once for the main constructor
- This.age=age
- This.name=name
- }
- }
Constructor details for class
[Java]View PlainCopy
- 1. The main constructor after the class name, the parameter is declared field, if the parameter is not used Var or Val declaration, it will be declared private field
- 2. When instantiating a class, the statements in the class are executed: println ("person")
- Class person (Name:string,var Age:int) {
- println ("person")
- Def show (): Unit = {
- println ("show..") +name)
- }
- var gender:string=_
- //Secondary constructor must call the main constructor, parameter cannot use Var
- def this (Name:string,age:int, gender:string) {
- This (name,age)
- This.gender=gender
- }
- }
2.scala has no static modifier, but the member under object is static, and if there is a class with the same name, it acts as its companion class. In object, you can generally do some initialization for the associated class, such as the Val Array=array (PS: It uses the Apply method) that we often use
[Java]View PlainCopy
- Object dog{
- private var age=0
- def age={
- age+=1
- Age
- }
- }
- Class dog{
- var age1=dog.age //dog.age is a private field of object Dog. It reminds me of the friend class of C + + .
- }
Use of 3.Apply
[Java]View PlainCopy
- Class applytest{
- Val name="Clow";
- def apply () {
- println ("Class applytest--apply () ...");
- }
- }
- The members under object are static by default
- Object applytest{
- def apply () = {
- println ("Object applytest--apply () ...");
- new Applytest ()
- }
- }
- Object Basic4 {
- def main (args:array[string]) {
- //class name () called the Apply method under the corresponding object
- var a1=applytest ()
- println (A1.name)
- //Object name (), call the Apply method of the corresponding class
- A1 () //Output: Class applytest--apply () ...
- }
- }
Apply how to use it to implement a singleton mode
[Java]View PlainCopy
- Class Applytest private{ //Add private hidden constructor
- Def SayHello () {
- println ("Hello Jop")
- }
- }
- Object applytest{
- var instant:applytest=Null
- def apply () ={
- if (instant==null) instant=new applytest
- Instant
- }
- }
- Object Applydemo {
- def main (args:array[string]) {
- Val T=applytest ()
- T.sayhello ()
- }
- }
The understanding of object and class in Scala---The Apply method is the initialization method