scala类中属性的可见性 [英] Visibility of properties in scala class

查看:548
本文介绍了scala类中属性的可见性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过以下方式在我的类的构造函数中定义了一个属性:

I defined a property in the constructor of my class the following way:

class Step(val message:String = "")

当我尝试从Java代码中获取消息值时获取可见性错误。为什么?

When I try access to message value from Java code y get a visbility error. Why?

推荐答案

我想在Java代码中你试图用步骤访问该字段.message
确实,这样一个领域,它是私人的。这就是您获得可见性错误的原因。在Scala中声明val字段时,编译器会生成字段和访问器方法。所以在java中你应该使用 step.message()

I guess that in the Java code you're trying to access the field with step.message. Indeed, there is such a field, and it is private. That is why you get the visibility error. When you declare 'val' fields in Scala, the compiler generates a field and accessor method. So in java you should use step.message()

这篇关于scala类中属性的可见性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆