官术网_书友最值得收藏!

  • 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.

主站蜘蛛池模板: 嘉兴市| 龙游县| 三门峡市| 刚察县| 天水市| 永和县| 南雄市| 兰州市| 吐鲁番市| 喀喇沁旗| 化州市| 石阡县| 江达县| 玉门市| 铜梁县| 南安市| 汶川县| 兴文县| 盱眙县| 丽江市| 锡林郭勒盟| 岳西县| 新田县| 万源市| 平罗县| 旌德县| 清流县| 原平市| 辉县市| 汝阳县| 巴南区| 洪湖市| 平陆县| 昌吉市| 延川县| 莱西市| 仲巴县| 山阳县| 枞阳县| 怀柔区| 淮北市|