加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

scala – 避免生成默认的setter getter

发布时间:2020-12-16 19:16:00 所属栏目:安全 来源:网络整理
导读:我将自己的getter和setter添加到变量中: class Person{ private var age = 0 def currentAge = age def currentAge_=(age: Int) = this.age = age } 看编译版本给出: public class Person implements scala.ScalaObject { private int age; private int ag
我将自己的getter和setter添加到变量中:

class Person{
    private var age = 0
    def currentAge = age
    def currentAge_=(age: Int) = this.age = age 
}

看编译版本给出:

public class Person implements scala.ScalaObject {
    private int age;
    private int age();
    private void age_$eq(int);
    public int currentAge();
    public void currentAge_$eq(int);
    public Person();
}

我想避免自动生成默认的getter和setter.可能吗?

解决方法

private[this] var age = 0

这样年龄只对实例可见.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读