- Scala Programming Projects
- Mikael Valot Nicolas Jorand
- 124字
- 2021-07-23 16:25:14
Subclass assignment
Consider two classes, A and B, with B extends A.
When you declare a variable of type A, you can assign it to an instance of B, with val a: A = new B.
On the other hand, if you declare a variable of type B, you cannot assign it to an instance of A.
Here is an example that uses the same Shape and Rectangle definitions that were described earlier:
val shape: Shape = new Rectangle(x = 0, y = 3, width = 3, height = 2)
val rectangle: Rectangle = new Shape(x = 0, y = 3)
The first line compiles because Rectangle is a Shape.
The second line does not compile, because not all shapes are rectangles.
推薦閱讀
- 網絡教育學習指導
- Application Development with Qt Creator(Second Edition)
- RCNP實驗指南:構建高級的路由互聯網絡(BARI)
- 高校網絡道德教育研究
- 網絡的琴弦:玩轉IP看監控
- Building RESTful Web Services with Spring 5(Second Edition)
- 面向5G-Advanced的關鍵技術
- 計算機網絡技術及應用
- Master Apache JMeter:From Load Testing to DevOps
- 工業互聯網創新實踐
- Practical Web Penetration Testing
- INSTANT LinkedIn Customization How-to
- 云計算技術與標準化
- 物聯網M2M開發技術:基于無線CPU-Q26XX
- RestKit for iOS