FAQ
public class ClassA {

   private ClassB propertyB;

   private ClassC propertyC;

   public ClassA(ClassB propertyB) {
     this.propertyB = propertyB;
     this.propertyC = new ClassC(propertyB);
   }

}
无法写成这样:
class ClassA(var propertyB: ClassB) {

   var ClassC propertyC = _

   def this( propertyB: ClassB) = {
     this.propertyC = new ClassC(propertyB)
     this
   }

}

--
您收到此邮件是因为您订阅了 Google 网上论坛的“Scala中文社区”论坛。
要退订此论坛并停止接收此论坛的电子邮件,请发送电子邮件到 scalacn+unsubscribe@googlegroups.com
要向此网上论坛发帖,请发送电子邮件至 scalacn@googlegroups.com
要查看更多选项,请访问 https://groups.google.com/groups/opt_out。

Search Discussions

  • David Day at Jan 15, 2014 at 12:01 am
    比如说如下代码:
    public class ClassA {

       private ClassB propertyB;

       private ClassC propertyC;

       public ClassA(ClassB propertyB) {
         this.propertyB = propertyB;
         this.propertyC = new ClassC(propertyB);
       }
    }
    无法写成这样:
    class ClassA(var propertyB: ClassB) {

       var propertyB: ClassC = _

       def this(propertyB: ClassB) = {
         this.propertyC = new ClassC(propertyB);
         this
       }

    }
    那应该怎样写?

    --
    您收到此邮件是因为您订阅了 Google 网上论坛的“Scala中文社区”论坛。
    要退订此论坛并停止接收此论坛的电子邮件,请发送电子邮件到 scalacn+unsubscribe@googlegroups.com
    要向此网上论坛发帖,请发送电子邮件至 scalacn@googlegroups.com
    要查看更多选项,请访问 https://groups.google.com/groups/opt_out。
  • Xuefeng Wu at Jan 15, 2014 at 12:10 am
    如果只是想做个赋值计算,完全可以定义一个类字段来做
    scala> new Account("ScalaCn")
    res2: Account = Account@1d2580e4

    scala> res2.personName
    res3: String = ScalaCn


    如果想做构造函数,参数要不一样

    scala> class Account(name:String, balance: Double) {
    def this(name:String) = this(name,0)
    }
    defined class Account

    scala> new Account("Scala")
    res0: Account = Account@52bfed39

    scala> new Account("ScalaCn",100)
    res1: Account = Account@7ef4de5d



    2014/1/14 David Day <davepkxxx@gmail.com>
    比如说如下代码:
    public class ClassA {

    private ClassB propertyB;

    private ClassC propertyC;

    public ClassA(ClassB propertyB) {
    this.propertyB = propertyB;
    this.propertyC = new ClassC(propertyB);
    }
    }
    无法写成这样:
    class ClassA(var propertyB: ClassB) {

    var propertyB: ClassC = _

    def this(propertyB: ClassB) = {
    this.propertyC = new ClassC(propertyB);
    this
    }

    }
    那应该怎样写?

    --
    您收到此邮件是因为您订阅了 Google 网上论坛的“Scala中文社区”论坛。
    要退订此论坛并停止接收此论坛的电子邮件,请发送电子邮件到 scalacn+unsubscribe@googlegroups.com
    要向此网上论坛发帖,请发送电子邮件至 scalacn@googlegroups.com
    要查看更多选项,请访问 https://groups.google.com/groups/opt_out。


    --

    ~Yours, Xuefeng Wu/吴雪峰 敬上

    --
    您收到此邮件是因为您订阅了 Google 网上论坛的“Scala中文社区”论坛。
    要退订此论坛并停止接收此论坛的电子邮件,请发送电子邮件到 scalacn+unsubscribe@googlegroups.com
    要向此网上论坛发帖,请发送电子邮件至 scalacn@googlegroups.com
    要查看更多选项,请访问 https://groups.google.com/groups/opt_out。
  • Xuefeng Wu at Jan 15, 2014 at 12:11 am
    或者是想在对象产生的时候做个初始化动作

    scala> class Account(name: String) {
    def init() {println(name)}
    init()
    }
    defined class Account

    scala> new Account("ScalaCn")
    ScalaCn
    res4: Account = Account@4da379fe



    2014/1/15 Xuefeng Wu <benewu@gmail.com>
    如果只是想做个赋值计算,完全可以定义一个类字段来做
    scala> new Account("ScalaCn")
    res2: Account = Account@1d2580e4

    scala> res2.personName
    res3: String = ScalaCn


    如果想做构造函数,参数要不一样

    scala> class Account(name:String, balance: Double) {
    def this(name:String) = this(name,0)
    }
    defined class Account

    scala> new Account("Scala")
    res0: Account = Account@52bfed39

    scala> new Account("ScalaCn",100)
    res1: Account = Account@7ef4de5d



    2014/1/14 David Day <davepkxxx@gmail.com>
    比如说如下代码:
    public class ClassA {

    private ClassB propertyB;

    private ClassC propertyC;

    public ClassA(ClassB propertyB) {
    this.propertyB = propertyB;
    this.propertyC = new ClassC(propertyB);
    }
    }
    无法写成这样:
    class ClassA(var propertyB: ClassB) {

    var propertyB: ClassC = _

    def this(propertyB: ClassB) = {
    this.propertyC = new ClassC(propertyB);
    this
    }

    }
    那应该怎样写?

    --
    您收到此邮件是因为您订阅了 Google 网上论坛的“Scala中文社区”论坛。
    要退订此论坛并停止接收此论坛的电子邮件,请发送电子邮件到 scalacn+unsubscribe@googlegroups.com
    要向此网上论坛发帖,请发送电子邮件至 scalacn@googlegroups.com
    要查看更多选项,请访问 https://groups.google.com/groups/opt_out。


    --

    ~Yours, Xuefeng Wu/吴雪峰 敬上

    --

    ~Yours, Xuefeng Wu/吴雪峰 敬上

    --
    您收到此邮件是因为您订阅了 Google 网上论坛的“Scala中文社区”论坛。
    要退订此论坛并停止接收此论坛的电子邮件,请发送电子邮件到 scalacn+unsubscribe@googlegroups.com
    要向此网上论坛发帖,请发送电子邮件至 scalacn@googlegroups.com
    要查看更多选项,请访问 https://groups.google.com/groups/opt_out。
  • David Day at Jan 15, 2014 at 3:10 pm
    了解了

    在 2014年1月15日星期三UTC+8上午8时11分04秒,Xuefeng Wu写道:
    或者是想在对象产生的时候做个初始化动作

    scala> class Account(name: String) {
    def init() {println(name)}
    init()
    }
    defined class Account

    scala> new Account("ScalaCn")
    ScalaCn
    res4: Account = Account@4da379fe



    2014/1/15 Xuefeng Wu <ben...@gmail.com <javascript:>>
    如果只是想做个赋值计算,完全可以定义一个类字段来做
    scala> new Account("ScalaCn")
    res2: Account = Account@1d2580e4

    scala> res2.personName
    res3: String = ScalaCn


    如果想做构造函数,参数要不一样

    scala> class Account(name:String, balance: Double) {
    def this(name:String) = this(name,0)
    }
    defined class Account

    scala> new Account("Scala")
    res0: Account = Account@52bfed39

    scala> new Account("ScalaCn",100)
    res1: Account = Account@7ef4de5d



    2014/1/14 David Day <dave...@gmail.com <javascript:>>
    比如说如下代码:
    public class ClassA {

    private ClassB propertyB;

    private ClassC propertyC;

    public ClassA(ClassB propertyB) {
    this.propertyB = propertyB;
    this.propertyC = new ClassC(propertyB);
    }
    }
    无法写成这样:
    class ClassA(var propertyB: ClassB) {

    var propertyB: ClassC = _

    def this(propertyB: ClassB) = {
    this.propertyC = new ClassC(propertyB);
    this
    }

    }
    那应该怎样写?

    --
    您收到此邮件是因为您订阅了 Google 网上论坛的“Scala中文社区”论坛。
    要退订此论坛并停止接收此论坛的电子邮件,请发送电子邮件到 scalacn+u...@googlegroups.com <javascript:>

    要向此网上论坛发帖,请发送电子邮件至 sca...@googlegroups.com <javascript:>。
    要查看更多选项,请访问 https://groups.google.com/groups/opt_out。


    --

    ~Yours, Xuefeng Wu/吴雪峰 敬上

    --

    ~Yours, Xuefeng Wu/吴雪峰 敬上
    --
    您收到此邮件是因为您订阅了 Google 网上论坛的“Scala中文社区”论坛。
    要退订此论坛并停止接收此论坛的电子邮件,请发送电子邮件到 scalacn+unsubscribe@googlegroups.com
    要向此网上论坛发帖,请发送电子邮件至 scalacn@googlegroups.com
    要查看更多选项,请访问 https://groups.google.com/groups/opt_out。

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupscalacn @
postedJan 15, '14 at 12:01a
activeJan 15, '14 at 3:10p
posts5
users2

2 users in discussion

David Day: 3 posts Xuefeng Wu: 2 posts

People

Translate

site design / logo © 2019 Grokbase