tailieunhanh - Học Actionscript 3.0 - p 17
Polymorphism Polymorphism The last important concept of object-oriented programming that we want to discuss is polymorphism. Although we’ll expand the explanation as this section evolves, you can start by thinking of polymorphism as a design practice that allows you to use objects of different types in a uniform manner. For example, for our vehicle exercise, you might create classes for land-, water-, and air-based vehicles and write code to move each type of vehicle. In this scenario, it’s better to use one method name for moving all of these vehicle types (such as “move”), instead of separate method names (like “drive,” “pilot,”. | Polymorphism Polymorphism The last important concept of object-oriented programming that we want to discuss is polymorphism. Although we ll expand the explanation as this section evolves you can start by thinking of polymorphism as a design practice that allows you to use objects of different types in a uniform manner. For example for our vehicle exercise you might create classes for land- water- and air-based vehicles and write code to move each type of vehicle. In this scenario it s better to use one method name for moving all of these vehicle types such as move instead of separate method names like drive pilot and fly for moving a car boat and plane respectively . Doing so makes your code more flexible more reusable and easier to read and document. In ActionScript polymorphism is commonly used with inheritance and or interfaces. We ll work with interfaces in a moment but for now think of them as rulebooks for classes. An interface is nothing more than a list of public methods that must be present in any class that conforms to the interface. For example you might continue to develop our vehicle exercise and eventually end up with vehicles that contain public methods that activate start up go stop and deactivate shut down your vehicles. You can create an interface that includes these method names and requires classes to adhere to that interface. This makes certain all of those classes can be controlled consistently. An interface doesn t restrict your class to those methods either. Classes can have their own public methods that are not in the interface without consequence. As long as the interface methods are present everyone will be happy. We ll discuss the further role that interfaces play in polymorphism a little bit later. For now let s extend what you already know and show how to use polymorphism with inheritance. Polymorphism and inheritance Employing polymorphism with inheritance allows you to design subclasses that can use the same method names as their
đang nạp các trang xem trước