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

What is the inheritance method of Scala object

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

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces "what is the inheritance method of Scala object". In the daily operation, I believe that many people have doubts about what the inheritance method of Scala object is. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "what is the inheritance method of Scala object?" Next, please follow the editor to study!

I. singleton object

There are no static properties or methods in Scala, which can be implemented through object if necessary. In addition, if you need to use a singleton, you can also use object to achieve it. Summary: object = static+singleton

Object ObjectTest {

Var name = ""

Var age = 0

Def main (args: Array [String]) {

Println ("I am an object")

}

}

2. Concomitant object

So in java, we have a lot of classes that require both static and instance methods. Such a situation can be achieved in Scala by the way of accompanying objects. The class and its companion must exist in the same file.

Class ObjectTest {

Var name = ""

Var age = 0

}

Object ObjectTest {

Var name = ""

Var age = 0

Def main (args: Array [String]) {

Val o = new ObjectTest

O.name = "bajie"

O.age = 100

ObjectTest.name = "wukong"

ObjectTest.age = 500,

Println (o.name, o.age)

Println (ObjectTest.name, ObjectTest.age)

}

}

III. Inheritance of object

Object can extend a class or one or more attributes (similar to interface in java, but can have the implementation of methods)

Trait Machine {

Def play ()

}

Object TV extends Machine {

Override def play () = {println ("gaoqing playing")}

}

IV. Apply

Here involves a specific application of concomitant objects, and its scope of application is very wide, ObjectTest ("bajie", 100) and ObjectTest.apply ("bajie", 100) are actually equivalent.

Class ObjectTest (val name: String, var age: Int) {

Override def toString () = name + "is" + age + "years old."

}

Object ObjectTest {

Def main (args: Array [String]) {

Println (ObjectTest (bajie, 100))

Println (ObjectTest.apply (bajie, 100))

}

Def apply (name:String, age: Int) = {

New ObjectTest (name, age)

}

}

5. Enumeration

There are no enumerated types in Scala, it provides a helper class to do the enumerating work. Scala.Enumeration.

Object EnuTest extends Enumeration {

Val Red, Yellow, Green = Value

Def main (args: Array [String]) {

Println (doWhat (Red))

Println (doWhat (Yellow))

Println (doWhat (Green))

}

Def doWhat (color: EnuTest.Value) = {

Color match {

Case Red = > "Stop"

Case Yellow = > "Wait"

Case Green = > "Go"

}

}

}

Note that Value is not a property, but a method, and each call generates a new ID. In this code, the id of the three enumerated values is 0Magne 1,2, and the type of the enumerated value is EnuTest.Value.

VI. Package object

How to understand this thing, first review object, we understand it as static+singleton, then package object should be a singleton object belonging to a certain package, and you can put some static methods needed by that package into it. In addition, there can be only one package object per package, which is located inside the package with the file name = package.scala.

In other respects, it is no different from ordinary object.

At this point, the study on "what is the inheritance method of Scala object" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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

Servers

Wechat

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

12
Report