In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
This article focuses on "how to achieve scala object-oriented", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "how to implement scala object-oriented"!
Class-like
Class Hello {
Privateval tt= "Hello"
Private var tos= "World"
Def add () {
Println (tt+tos)
}
Def plus (m:Char) = tos+m
}
Class definition
Classes mainly include fields val and var methods and functions, but scala forbids naming fields and methods with the same name
There are two kinds of visibility of class members: private and public,private need to be declared, and pulic need not declare
Declare the object with new after the class declaration
Val one=new Hello ()
Object operation
Val a=one.value1
Val b=one.value2
One.value2=one.plus (H)
One.add ()
Calling a no-parameter method can be done without () one. Add
If the no-parameter method in the class does not take () and the actual call does not take ()
Def add {println (value1+value2)}
One . Add
Getter method
Setter method
Sclala provides getter and setter methods by default for the fields of each class.
For common fields, getter and setter are also public; for private fields, they are private.
Fields declared by var with getter and setter methods
The val field comes with the getter method read-only
For the field value1 period form is value1, there is no setter method
For the field value2, the getter and setter methods are value2 and value2_=.
In actual use, outside the class definition, the getter and setter methods are consistent, like one. Value2
The meaning of getter method and setter method is related to the private object data in the control class
It can be obtained by redefining getter and setter methods in the class, and private fields can be modified with restrictions.
Class HELLOWORLD {
Private var value1 = "HELLO"
Var value2 = "WORLD"
Def add () {println (value1+value2)}
Def plus (m: Char) = value2 + m
Def value1 = value1
Def value1_= (newvalue1: String) {
If (newvalue1.length > value1.length) value1 = newvalue1
}
}
Class H
Constructor
Each class has a main constructor that is intertwined with the class definition
The parameters of the main constructor are placed directly after the class name
Class HELLOWORLD (val value1:String,var value2:String)
The parameters of the main constructor are compiled into a field and passed in when the construction object is initialized
If a class does not display a definition Zhu constructor, then it automatically has a no-parameter master constructor.
If there is a statement that executes directly in the class, it will be executed every time the function is constructed, regardless of the constructor
Such as class HELLOWORLD (val value1:String,var value2:String) {
Println ("HELLOWORLD IS CREATED"
Val value3=value1+value2)}
There are generally four kinds of main constructors
Value:String
Generate a private field of the object, which does not have a method using value in the object
Privateval/var value:String
Private gettter and setter methods for private fields
Val 、 var value: String
Private fields, common setter, getter methods
@ BeanProperty val, var value: String
Class HelloWorld private (main constructor) {class member}
The primary constructor is private and objects can only be constructed through secondary constructors.
Auxiliary constructor
The scala class has as many auxiliary constructors as you want
The name of the auxiliary constructor is this defined in the class
The auxiliary constructor must start with a call to a Zhu constructor or other defined auxiliary constructor
Class HELLOWORLD {
Private var value1 = "HELLO"
Var value2 = "WORLD"
Def this (m:String) {
This () / / calls the main constructor
This.value1=m
}
Def this (mpurStringjingjnluString) {
This (m)
This.value2=n
}
}
Nested class
Scala allows syntax structures to be embedded in any syntax structure, so you can define classes in classes
Class HELLOWORLD {
CLASS HI {}}
It is different for an inner class under an external class ongoing instance
Such as val three=new HELLOWORLD and four = new HELLOWORLD
Three.HI and four.HI are different classes
An inner class can call an external class member, using the external class .this or a pointer to implement
Class HELLOWORLD {pointto= >
Var value2= ""
Class HI {val value3=HELLOWORLD.this.value2
Var value4=pointto.value2}
Scala object
Singleton object
The object syntax defines a single instance of a class
The constructor of the object is called when the object is first used
The syntax structure of object is roughly the same as that of class, except that object cannot provide constructor parameters.
Environments that typically use single-column objects
As a place to store tool functions or constants
Sharing a single immutable thing is
Coordinate a service with a single instance
Associated object
When a single-column object has a class of the same name, it is called an accompanying object.
Class HELLOWORLD {}
Object HELLOWORLD {}
Class and its accompanying objects can access private properties to each other, but they must exist in the same source file
The concomitant object of the class can be accessed, but not in the
Class HELLOWORLD {}
Object HELLOWORLD {def}
HELLOWORLD must call the NOW method in the companion object through HELLOWORLD.NOW, not directly with NOW.
Extended class object
Construct a class that extends the specified class and attributes as the object of the class, and has all the properties given by the object definition.
Apply method
When you need to construct concomitant objects with parameter requirements, you can define and use the appl method
Class HELLOWORLD (var mpurs Stringjingjingjingju Char) {}
Object HELLOWORLD {
Def apply (n:Char) = new HELLOWORLD (", n)
}
Val hi=HELLOWORLD ('j')
Expansion
Extens is a reserved word that implements inheritance in scala
Class week extends month {}
The week class inherits all non-private members of the month class
Week class is a subclass of month class, and month class is a superclass of week class.
Subclasses can override superclass members (with the same name and parameters)
Class week (val num:int) extends month (var no:int) {}
Object day extends week {}
Singleton objects can also inherit from a class, the same as the inheritance syntax of a class.
Rewrite
Method rewriting using override reserved word method in scala
Class week (overrideval lastday:string) extends month {}
The definition declaration after the override reserved word actually uses the reserved word like private declaration is a rewrite of the superclass, so it may also be written in the parameters of the class definition.
Class week extends month (overrideval lastday:string) {}
Overriding or modifying a subclass sclala checks its superclass, but changes to the superclass do not check the subclass
Class month {def secondday (m:String) = {}}
Class week extends month {def secondday= {}}
Rewrite rule
Rewrite def
Using val to use val to rewrite superclasses with methods without parameters
Use the method of def subclass and superclass method to repeat the name
Override both the getter and setter methods with var. Only override getter method to report an error
Rewrite val
The getter method overrides the getter method of the superclass with a private field of the val subclass with the same name as the superclass field
Rewrite var
Using var and when the var of the superclass is abstract can be rewritten, otherwise the var of the superclass will be integrated
Class month {
Val one=25// can be rewritten with val in subclasses
Var two=15// cannot be rewritten with var in a subclass because it is not abstract
Var three:int
Def firstday=, can be overridden with val in subclasses
Def now= / / can be rewritten with var in a subclass
Def now_= can be rewritten with def in a subclass
Def lastday (m:char) = {}
}
Def in the subclass can only override the def of the superclass. Val can override the val of the superclass or the def,var without parameters can only override the superclass.
Abstract var or superclass getter and setter
}
Abstract
Classes that cannot be instantiated are called abstract classes
One or more members of an abstract class are not fully defined. Members that are not fully defined are called abstract methods or abstract fields.
Mark abstract classes with abstract reserved fields
Abstract year {
Val name:Array [string] / / Abstract val with an abstract getter method
Var num:Int// abstract var with abstract getter and setter methods
Def sign / / there is no method body function body, it is an abstract method
}
As long as there is an abstract member in the class, you must use the abstract tag
Override abstract methods, abstract fields are not allowed to use override reserved words
Protection
When a class does not want to be inherited or extended, add the final reserved word before the class declaration
Final class year {}
When some members of a class do not want to be overridden, add the final reserved word before the member declaration
Class year {final def sign {}}
When some members of a superclass need to be inherited by a subclass and do not want to be visible outside the subclass, add the protect reserved word to the member variable
Protect [this] sets access to the current object, similar to private [this]
A member of a protected in a class is visible to its subclass and not to its superclass
Class year {protect def sign {}}
Construction
The subclass constructor runs after the superclass constructor runs
After the member called in the constructor of the superclass is overridden by the subclass, the return value may be incorrect
Class month {
Val num=31
Val days=new Array [Int] (num)}
Class week extends month {
Overrideval num=7}
Execute the month constructor num before constructing the week object is initialized to 31 month as the initialization array
Call num but the num subclass week overrides it, but because the week constructor has not been called yet, the value of num has not been initialized, so it is returned.
0 day is set to an array of length 0, the month constructor finishes running, the week constructor is executed, and num is initialized to 7
Solution method
Declare the val of the superclass as final
Declare the val of the superclass as lazy
Use pre-defined syntax in subclasses
Define in advance the fields that initialize the subclass before the constructor of the superclass runs
Put the statement block that needs to be defined in advance between the extends and the superclass, followed by the with reserved word
Class week extends {overideval num=7} with month {}
If you need to call a b member in an advance definition, unless the b member has already called the premise definition
Class week extends {
Overrideval num=7
Overrideval num2=num+1// allows num to be defined in advance
Overideval num4=num2+num3 / / not allowed. Num3 is not defined in advance.
} with month {}
Scala trait
Multiple inheritance
Scala does not support multiple inheritance, it is replaced by traits.
A subclass can only have one superclass, and a superclass can have multiple subclasses
Ji class week extends month,year is illegal
Why
If a subclass inherits from different superclasses and members of the same name in different superclasses, I don't know what to do with it.
Multiple inheritance causes the problem of diamond inheritance
Solving the problems that may be caused by multiple inheritance consumes far more resources than multiple inheritance.
Sclala uses attributes to achieve an effect similar to multiple inheritance.
A class can extend multiple attributes, and a trait can be extended by multiple classes.
Trait use
Attributes are the basic unit of code reuse in scala, encapsulating method and field definitions
Idiosyncratic use of reserved word trait concrete syntax is similar to class definition, except that you cannot have a constructor
Trait reset {
Def reset (mvuInt) = if (m > n) 1}
Once the trait is defined, it can be mixed into the category.
Class week extends reset wiTH B WITH C {}
Can the members of the traits be abstract?
All java interfaces can be used in scala as a feature.
Attribute members can be abstract, and there is no need to use abstract declarations
Similarly, abstract methods for rewriting attributes do not need to give override
However, abstract methods for multiple attributes to rewrite one trait require override
In addition to defining blending attributes in a class, you can also
Trait reseting extends reset {}
Mix characteristics in construction
Val five=new month with reseting
Trait structure
Trait construction is sequential and is constructed from left to right.
The constructors are constructed in the following order
Object hi {
Def main (args: array [string]): Unit= {}
Trait one {/ / Abstract attributes
Def a ()}
Trait two extends one {
Override def a () {println ("my")}
A
}
Trait tree extends one {
Override def a () {println ("Name")}
A
}
Class Name () {val masks 1}
Val one=new Name () with two with three / / NAME NAME
Println ()
Val two=new Name () with three with two//MY MY
}
If class An extends B1 with B2 with B3
Then get rid of the repetition and win on the right side of class B2.
At this point, I believe you have a deeper understanding of "how to implement scala object-oriented". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.