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

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.

主站蜘蛛池模板: 留坝县| 鹤山市| 大理市| 横山县| 湘西| 蒙城县| 聊城市| 桑植县| 文昌市| 潍坊市| 永和县| 南阳市| 平舆县| 阳泉市| 博爱县| 宾阳县| 平乡县| 临安市| 专栏| 宜春市| 昔阳县| 甘洛县| 屯留县| 闽侯县| 普定县| 新平| 墨脱县| 桦川县| 夏邑县| 邵东县| 铜陵市| 修文县| 昌黎县| 大安市| 郓城县| 监利县| 嘉兴市| 青浦区| 长汀县| 高淳县| 三原县|