20. Casting - CS2030S Programming Methodology II
We have seen in Unit 18 how we can write code that is reusable and general by making our code dependent on types at a higher-level of abstraction. Our main example is the following findLargest method, which takes in an array of objects that support the getArea method, and returns the largest area among these objects. The method served our purpose well, but it is NOT a very well-designed method. Just returning the value of the largest area is not as useful as returning the object with the largest area. Once the caller has a reference of the object, the caller can call getArea to find the value of the largest area. Let's write our findLargest method to find which object has the largest area instead. Let's see how findLargest can be used: The return type of findLargest (version 0.4) is now GetAreable. On Line 6 above, we assign the return object with a compile-time type of GetAreable to ga, which also has GetAreable as its compile-time type. Since the variable ga is of type GetAreable, ho
Unit 20: Run-Time Class Mismatch After taking this unit, students should: Understand the need for narrowing type conversion and type casting when writing code that depends on higher-level abstraction Understand the possibility of encountering run-time errors if typecasting is not done properly. We have seen in Unit 18 how we can write code that is reusable and general by making our code dependent on types at a higher-level of abstraction. Our main example is the following findLargest method, which takes in an array of objects that support the getArea method, and returns the largest area among
Explore this link on the map →saved by
related reading
- 21. Variance - CS2030S Programming Methodology IInus-cs2030s.github.io
- 14. Polymorphism - CS2030S Programming Methodology IInus-cs2030s.github.io
- 17. Abstract Class - CS2030S Programming Methodology IInus-cs2030s.github.io
- 2. Variable and Type - CS2030S Programming Methodology IInus-cs2030s.github.io
- 4. Encapsulation - CS2030S Programming Methodology IInus-cs2030s.github.io
- 19. Wrapper Class - CS2030S Programming Methodology IInus-cs2030s.github.io
- 22. Exception - CS2030S Programming Methodology IInus-cs2030s.github.io
- how I think when I think about programming - alice mazalicemaz.com
- 練習 - 探索資料類型的強制型轉和轉換 - Training | Microsoft Learnlearn.microsoft.com
- TypeScript: Documentation - The Basicstypescriptlang.org
- Reading 12: Defining ADTs with Interfaces, Generics, Enums, and Functionsweb.mit.edu
- Generic Data Types - The Rust Programming Languagedoc.rust-lang.org