For various practical reasons it's usually better to not define fields in
Scala traits, but Scala does allow it

scala> trait Foo { var x = 42 }
defined trait Foo

scala> class Bar extends Foo
defined class Bar

scala> val b = new Bar
b: Bar = b...@8be1c9

scala> b.x
res2: Int = 42

scala> b.x = 13

scala> b.x
res3: Int = 13

The main difference between Scala traits and true blue multiple class
inheritance in C++ and Python is that Scala traits can only have a default
no-arg constructor.  If you need constructor arguments then you must use a
class.

On Fri, Aug 28, 2009 at 7:10 AM, Reinier Zwitserloot <[email protected]>wrote:

>
>
> automatically pick up on unless you override them. It's very similar
> to multiple inheritance, except that there's no sharing of state: A
> trait's fields aren't inherited by any classes that implement the
> trait. Just the methods.
>
>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "The 
Java Posse" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/javaposse?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to