Knowledge Points:
1. Single-instance objects
Use the object syntax structure to achieve the purpose of static and static fields, as in the following example, an object defines a single instance of a class that contains the desired attribute, which is called when the object's constructor is first used.
Object account{ private var lastnumber = 0 = {Lastnumber + = 1; Lastnumber}}
An object can inherently have all the attributes of a class, except that it cannot provide constructor parameters.
2. Associated Objects
A class with a class and a "companion" object with the same name as the class that achieves the base friend instance method and static method. A class and its associated objects can access private attributes to each other, and they must exist in the same source file.
class account{ = account.newuniquenumber () private var balance = 0.0 def Deposit (amount:double) { + = amount }}object account{private var lastnumber = 0 = {Lastnumber + = 1; Lastnumber}}
3. An object extending a class or trait, an object can extend a class and one or more attributes, and the result is a class that extends the specified class and attributes, with all the attributes given in the object definition.
4.apply Method Object (parameter 1, Parameter 2, ..., parameter n) This form of use, the Apply method is called, the Apply method eliminates the keyword new to build the object.
5. Application objects each Scala program must start with the main method of an object
6.Scala does not have an enumeration type, the standard class library provides a enumeration helper class that can be used to output enumerations.
Exercise: (reference URL)
1. Write a Conversions object and add the Inchestocentimeters,gallonstoliters and Milestokilometers methods
Object conversions{ def inchestocentimeters () {} def gallonstoliters () {} def milestokilometers () {}}
2. The previous exercise was not very object-oriented. Provides a generic superclass unitconversion and defines the inchestocentimeters,gallonstoliters and Milestokilometers objects that extend the superclass
Abstract class unitconversion{ def inchestocentimeters () {} def gallonstoliters () {} extends unitconversion{ extends unitconversion{ extends unitconversion { override def milestokilometers () {}}
3. Define an Origin object that extends from Java.awt.Point. Why is this not really a good idea? (Take a closer look at the method of the Point Class)
The GetLocation method in point returns the Point object and requires the Origin class to return the Origin object.
Importextends point with app{ Super. GetLocation Origin.move ( 2,3) println (origin.tostring)}
4. Define a point class and a companion object so that we can construct a point instance directly with point (3,4) without new
class Point (x:int,y:int) { = "x=" +x+ "y=" +extends app{= { New point (x, y) }= point (3,4) println (p)}
5. Write a Scala app that uses app traits to print command line arguments in reverse order, separated by spaces. For example, Scala Reverse Hello world should print world Hello
Extends= print (arg + "")}
6. Write a poker 4-suit enumeration, and let its ToString method return?,?,?,?
extends enumeration with app{ = Value ("?" ) = Value ("?" ) = Value ("?" ) = Value ("?" ) println (CARD.M) println (card.t) println (Card.H) println (CARD.F) }
7. Implement a function to check if the color of a card is red
extends enumeration with app{ = Value ("?" ) = Value ("?" ) = Value ("?" ) = Value ("?" ) def color (c:card.value) { if(c = = CARD.M | | c = = card.t) print ("Black") Else print ("Red") } Color (Card.H)}
8. Write an enumeration that describes the 8 corners of an RGB cube. ID uses color values (for example: Red is 0xff0000)
extends enumeration with app{ = value (0xff0000, "Red") = value (0x000000, "Black") = Value (0x00ff00, "Green") = value (0x00ffff, "Cyan") = value (0xffff00, "Yellow") = value (0xFFFFFF, "white") = value (0x0000ff, "Blue") = value (0XFF00FF, " Magenta ")}
Learn scala-the sixth chapter on the subject