Scala Programming Problem Sets (01)

Source: Internet
Author: User

Scala Programming Problem Sets (01)



by Gao

Dongting International Intelligent Hardware testing base


Q-01: How to use Scala 's Singleton mechanism to express class-level data.

Answer:

      in object-oriented programming (oop,object-oriented programming) concept, attributes ( Attribute) and functions (function) are divided into two different levels (level). For example, Xiamen's Technology Valley (Xiamen) Company's big data platform, defined the hundreds of airlines class (class), including: Eastern Airlines (Chinaeasternairlines), Southern Airlines (Chinaeasternairlines), Eva Airlines (Evergreenairlines) and so on. Each of these aircraft is an instance (Instance) or object. As you can see, no matter the property or function is divided into two levels:

    • Instance-level (Instance level or object level ): This is generally known, for example, each aircraft is an instance or object, each has its own brand, capacity, age and other attribute data.

    • Class-level (class level): For example, Eva Airlines ' number of aircraft is an important attribute data, but not an aircraft (Instance) attribute data, but belongs to the entire Evergreenairlines class of attribute data. When we ask: The number of EVA Airlines aircraft, it is often equivalent to asking: how many objects (instances) are currently created by the Evergreenairlines class? Like this data, it is called the Class-level attribute data.


Now that you have the properties of Class-level , in order to access the data value (value) of this property, of course we will define some functions to access or manipulate it. This function is called a function or method of Class-level.


Instance-level the definition of properties and functions

Class EVA {

var manufacturer:string = ""

var capacity:int = 0

var mfr_date:string = ""


Def pr_capacity () {

println (capacity)

Class-level the definition of properties and functions

Object EVA {

var instance_amount:int = 0

def apply () {

Instance_amount = Instance_amount + 1

return new EVA

}

Def Pr_amt () {

println (Instance_amount)

MyApp

Object MYAPP {

def main (args:array[string]) {

Val a1 = EVA ()

Val A2 = EVA ()

Val a3 = EVA ()

Eva.pr_amt

}

where capacity is the Instance-level property, pr_capacity is the Instance-level function. While Instance_amt is the Class-level property, Pr_amt is the Class-level function. When executing this program, first load (load) code into memory to allow the VM to execute it.

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/55/46/wKiom1SJWqqwtmynAABaRwW2lHE002.jpg "title=" Aa01.png "alt=" Wkiom1sjwqqwtmynaabarww2lhe002.jpg "/>

When executing to the instruction:val a1 = EVA (), the equals sign is executed first(=)on the right.EVA (), takeObject EVAdefinition to create an object that expressesEVAclass itself, in other words,EVAThe class itself is an object, so create aVEAobject of the class, to storeEVAclass-related properties(Data)values. For example, define aInstance_amtattribute to record the currentEVAHow many objects are created for a class altogether(How many planes are there for EVA Airways).

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/55/46/wKiom1SJWs2Sd89-AAB8z2s0bkI963.jpg "title=" Aa02.png "alt=" Wkiom1sjws2sd89-aab8z2s0bki963.jpg "/>

then execute to the Apply () function and take the class Eva definition to create an object that expresses a long wing aircraft, recording the property values of the aircraft. In addition, Evaobj's Instance_amt value was increased by 1, and Eva Airways added an aircraft.

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/55/43/wKioL1SJW46QB1KcAACYoZ3XiRM435.jpg "title=" Aa03.png "alt=" Wkiol1sjw46qb1kcaacyoz3xirm435.jpg "/>

Continue to the following:

  Val A2 = EVA ()

Val a3 = EVA ()


create more of the EVA aircraft and update the INSTANCE_AMT values in the EVA Aircraft category . 650) this.width=650; "Src=" http://s3.51cto.com/wyfs02/M02/55/46/wKiom1SJWzLRIE78AADB9j122C0267.jpg " Title= "Aa04.png" alt= "Wkiom1sjwzlrie78aadb9j122c0267.jpg"/>

  At this point, there are 4 objects in memory, including1a<evaclass> (of its own)objects, and3a<evaExample>Object(as in theA1, A2and theA3). Above, by Eva Aviation's example, illustratesScalaHow language is expressedInstance-levelproperties and functions, and expressionsClass-levelproperties and functions.


~ End ~




Scala Programming Problem Sets (01)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.