需要在scala中的构造函数定义语法中作出澄清(Need clarification in constructor definition syntax in scala)

编程入门 行业动态 更新时间:2024-10-05 07:25:48
需要在scala中的构造函数定义语法中作出澄清(Need clarification in constructor definition syntax in scala)

我是scala的新手,需要澄清以下涉及类的构造函数的代码片段。

class sample (a: Int, b: Int) { /* define some member functions here */ }

我能否将变量a和b隐藏到类sample ?

class sample (val a: Int, val b: Int) { /* define some member functions here */ }

在这种情况下, a和b公开访问? 在构造函数的参数列表中添加val关键字的确切作用是什么? 如果我使用def关键字而不是val ,它是否也具有相同的效果?

I'm new to scala and need clarification on the following code snippet involving constructors for classes.

class sample (a: Int, b: Int) { /* define some member functions here */ }

Can I take that the variables a and b are private to the class sample?

class sample (val a: Int, val b: Int) { /* define some member functions here */ }

And in this case, are a and b publicly accessible? What is the exact effect of adding the val keyword in the parameter list for the constructor? And if I use the def keyword instead of val, does it have the same effect as well?

最满意答案

class sample (a: Int, b: Int)

a和b在这种情况下是私人的。 用javap反汇编显示a和b不是类的一部分(Scala调用这些类字段):

public class Sample extends java.lang.Object implements scala.ScalaObject{ public Sample(int, int); }

a和b先于val。 使用javap反汇编显示a和b现在是Sample中的公共字段。

class sample (val a: Int, val b: Int)

public class Sample extends java.lang.Object implements scala.ScalaObject{ public int a(); public int b(); public Sample(int, int); }

在构造函数中使用def而不是val ,它不会编译。 def用于定义函数。 不知道你是否可以在构造函数中使用def作为参数。

另外请注意,私人和受保护的行为与您所期望的相同。 鉴于这种:

class Sample(private val a: Int, protected val b: Int, val c: Int)

使用javap反汇编到以下内容:

public class Sample extends java.lang.Object implements scala.ScalaObject{ public int b(); public int c(); public Sample(int, int, int); } class sample (a: Int, b: Int)

a and b are private in this case. Disassembling with javap shows a and b are not part of the class(Scala calls these class fields):

public class Sample extends java.lang.Object implements scala.ScalaObject{ public Sample(int, int); }

a and b preceded with val. Disassembling with javap shows a and b are now public fields in Sample.

class sample (val a: Int, val b: Int)

public class Sample extends java.lang.Object implements scala.ScalaObject{ public int a(); public int b(); public Sample(int, int); }

With def instead of val in the constructor it won't compile. def is for defining functions. Not sure if you can use def in constructor as a parameter.

Also, note that private and protected behave as you would expect. Given this:

class Sample(private val a: Int, protected val b: Int, val c: Int)

Disassembles to the following with javap:

public class Sample extends java.lang.Object implements scala.ScalaObject{ public int b(); public int c(); public Sample(int, int, int); }

更多推荐

本文发布于:2023-07-09 10:12:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1085559.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:语法   函数   定义   scala   syntax

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!