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 are the ways in which Scala expands the scope of internal classes?

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the relevant knowledge of "what are the ways in which Scala expands the scope of the internal class". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Inner class initial user, the scope of inner class belongs to: external class object, different types of internal class object in different external class object will report error type mismatch.

Example code:

Note:

An external class Person and an inner class Student are defined

Package com.hadoop.ljs.spark.studyimport scala.collection.mutable.ArrayBuffer/** * @ author: Created By lujisen * @ company ChinaUnicom Software JiNan * @ date: 2020-02-08 22:49 * @ version: v1.0 * @ description: com.hadoop.ljs.spark.study * / class Person {class Student (val name:String) {} val students=new ArrayBuffer [Student] def getStudent (name:String): Student= {return new Student (name)}}

Main function:

Here, the student inner class object in the person2 object and the inner class object in the person1 object reported an error type mismatch

Error reason: because by default, the scope of the inner class is: the external class object must remember that it is the external class object *

Package com.hadoop.ljs.spark.study/** * @ author: Created By lujisen * @ company ChinaUnicom Software JiNan * @ date: 2020-02-08 22:19 * @ version: v1.0 * @ description: com.hadoop.ljs.spark.study * / object PersonTest {def main (args: Array [String]): Unit = {val person1=new Person () val studentA=person1.getStudent ("ljs1")

Person1.students+=studentA

Val person2=new Person () val studentB=person2.getStudent ("ljs2") / * here the student inner class object in the person2 object and the inner class object in the person1 object report an error type mismatch * here because by default, the scope of the inner class is: the external class object must remember that it is the external class object * / person1.students+=studentB}}

Screenshot of error:

There are two ways to expand the scope of inner class objects:

The first way: by deriving class objects

Code example:

Define Student directly in the accompanying object object Person, and the program runs normally.

Package com.hadoop.ljs.spark.studyimport com.hadoop.ljs.spark.study.Person2.Studentimport scala.collection.mutable.ArrayBuffer/** * @ author: Created By lujisen * @ company ChinaUnicom Software JiNan * @ date: 2020-02-08 22:43 * @ version: v1.0 * @ description: com.hadoop.ljs.spark.study * / object Person2 {class Student (val name:String) {} class Person2 {val students=new ArrayBuffer [Person2.Student] def GetStudent (name:String): Student= {return new Student (name)}}

The main function class is no longer wrong:

Package com.hadoop.ljs.spark.study/** * @ author: Created By lujisen * @ company ChinaUnicom Software JiNan * @ date: 2020-02-08 22:37 * @ version: v1.0 * @ description: com.hadoop.ljs.spark.study * / object PersonTest2 {def main (args: Array [String]): Unit = {val person1=new Person2 () val studentA=person1.getStudent ("ljs1") person1.students+=studentA

Val person2=new Person2 () val studentB=person2.getStudent ("ljs2") person1.students+=studentB}}

The second way to expand the scope of internal classes: type projection

Person class code:

Note:

Line 14 of the code, Person#Student is used here with the sign "#" through type mapping: as long as you are the inner class object of the external class, I recognize it as the same type * /

Package com.hadoop.ljs.spark.study

Import scala.collection.mutable.ArrayBuffer/** * @ author: Created By lujisen * @ company ChinaUnicom Software JiNan * @ date: 2020-02-08 22:55 * @ version: v1.0 * @ description: com.hadoop.ljs.spark.study * / class Person3 {class Student (val name:String) {} / * Note here the Person#Student sign is "#" through type mapping: as long as you are inside the external class. I recognize all the objects of the same type as * / val students=new ArrayBuffer [Person3#Student] def getStudent (name:String): Student= {return new Student (name)}}.

This is the end of the content of "what are the ways for Scala to expand the scope of internal classes". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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

Internet Technology

Wechat

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

12
Report