복붙노트

[SCALA] 왜 스칼라 특성은 클래스를 확장 할 수 있습니다?

SCALA

왜 스칼라 특성은 클래스를 확장 할 수 있습니다?

나는 (그들이 클래스를 확장하지 않는 자바뿐만 인터페이스를 다른 인터페이스를 확장) 스칼라에서 특성은 자바의 인터페이스와 유사 것을 알 수있다. 나는 특성이 클래스를 확장 특성 사용에 대한 SO에 예를 보았다.

이것의 목적은 무엇인가? 왜 특성은 클래스를 확장 할 수 있습니까?

해결법

  1. ==============================

    1.그들이 할 수있는 예, 클래스 풋에게 클래스가 그 특성을 확장 할 수 있는지에 대한 제한을 확장하는 특성 - 즉, 믹스에서 그 특성이 그 클래스를 확장해야하는 모든 클래스.

    그들이 할 수있는 예, 클래스 풋에게 클래스가 그 특성을 확장 할 수 있는지에 대한 제한을 확장하는 특성 - 즉, 믹스에서 그 특성이 그 클래스를 확장해야하는 모든 클래스.

    scala> class Foo
    defined class Foo
    
    scala> trait FooTrait extends Foo
    defined trait FooTrait
    
    scala> val good = new Foo with FooTrait
    good: Foo with FooTrait = $anon$1@773d3f62
    
    scala> class Bar
    defined class Bar
    
    scala> val bad = new Bar with FooTrait
    <console>:10: error: illegal inheritance; superclass Bar
     is not a subclass of the superclass Foo
     of the mixin trait FooTrait
           val bad = new Bar with FooTrait
                                  ^
    
  2. from https://stackoverflow.com/questions/12854941/why-can-a-scala-trait-extend-a-class by cc-by-sa and MIT license