- 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.
推薦閱讀
- 計算機網絡與通信(第2版)
- 物聯網識別技術
- Web Application Development with R Using Shiny
- 局域網組建、管理與維護項目教程(Windows Server 2003)
- Spring 5.0 Projects
- WordPress Web Application Development
- Master Apache JMeter:From Load Testing to DevOps
- AIoT應用開發與實踐
- 轉化:提升網站流量和轉化率的技巧
- bash網絡安全運維
- 智能家庭網絡:技術、標準與應用實踐
- CCNP TSHOOT(642-832)學習指南
- 一本書讀懂24種互聯網思維
- EtherCAT工業以太網應用技術
- MPLS網絡設計權威指南