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

Downcasting

Now, we will talk about downcasting, which means going from, for example, a parent type to a child type. Enter the following between a set of curly braces below this line:

sampLabel.Text += ((Square)lst[0]).Perimeter();

Now, beneath the closed curly brace after the preceding line, enter the following:

else if(lst[0] is Rectangle)

Then, you can call the following code; so, copy the sampLabel.Text... line and paste it between a set of curly braces:

sampLabel.Text += ((Rectangle)lst[0]).Perimeter();

Be sure to change Square to Rectangle, so that it gets cast down to a rectangle, and then the Perimeter function on the rectangle will be called. When you hover your mouse over Perimeter in the preceding two lines, the popups show string Square.Perimeter() and string Square.Perimeter(), respectively. If you removed (Rectangle) from the preceding line and hovered your mouse over Perimeter, the popup will show string Quad.Perimeter(). Do you understand? This is why I have the cast: because it changes the way the functions are recognized.

This is downcasting from a parent to a child class. So, when we talk about bulk actions, you cannot cast to a parent class, perform a bulk action like a sort, or if you want to add refinements called child classes and child class objects, then you can downcast.

主站蜘蛛池模板: 靖州| 阜康市| 通渭县| 烟台市| 财经| 建瓯市| 翁源县| 台中县| 原阳县| 册亨县| 克拉玛依市| 自治县| 太原市| 定远县| 东港市| 齐齐哈尔市| 玉田县| 姜堰市| 长汀县| 扎赉特旗| 玉龙| 静宁县| 勐海县| 沈丘县| 惠安县| 鄄城县| 铜鼓县| 定西市| 建平县| 潼南县| 筠连县| 桐梓县| 都昌县| 吴忠市| 长岛县| 澄江县| 景德镇市| 榆中县| 陈巴尔虎旗| 辽源市| 洞口县|