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

Scala构造函数弃用

发布时间:2020-12-16 09:11:40 所属栏目:安全 来源:网络整理
导读:我在 Scala中有一个课程,目前以标准方式构建: class Test( int : Int ){ override def toString() = "Test: %d".format( int ) } 但是,我想通过一个伴侣对象来转移到间接的构造.由于我正在修改的库被其他人使用,我不想让构造函数立刻离开.相反,我想废弃它,
我在 Scala中有一个课程,目前以标准方式构建:

class Test( int : Int )
{
    override def toString() = "Test: %d".format( int ) 
}

但是,我想通过一个伴侣对象来转移到间接的构造.由于我正在修改的库被其他人使用,我不想让构造函数立刻离开.相反,我想废弃它,然后回来,一旦人们有机会改变他们的使用,让它变得私密.所以我修改了我的代码:

object Test
{
    def apply( int : Int ) = new Test( int )
}

@deprecated( "Don't construct directly - use companion constructor","09/04/13" )
class Test( int : Int )
{
    override def toString() = "Test: %d".format( int ) 
}

但是,这样做不再适用于整个班级.

scala> Test( 4 )
<console>:10: warning: class Test in package foo is deprecated: Don't construct directly - use companion constructor
       val res0 =
           ^
res0: com.foo.Test = Test: 4

有人知道Scala是否支持对构造函数的弃用,如果是这样做呢?

解决方法

This thread似乎描述了解决方案:

object Test
{
    def apply( int : Int ) = new Test( int )
}


class Test @deprecated( "Don't construct directly - use companion constructor","09/04/13" ) ( int : Int )
{
    override def toString() = "Test: %d".format( int ) 
}

现在不能尝试.

(编辑:李大同)

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

    推荐文章
      热点阅读