tailieunhanh - Essential ActionScript 3.0 PHẦN 3

Example 8-3. The Food class (continued) public function getName ( ):String { return name; } public function setName (newName:String):void { name = newName; } } } Example 8-4 shows the code for the Apple class, which represents a specific type of food that pets eat. Example 8-4. The Apple class package zoo { public class Apple extends Food { private static var DEFAULT_CALORIES:int = 100; private var wormInApple:Boolean; public function Apple (initialCalories:int = 0) { if (initialCalories = .5; setName("Apple"); } public function hasWorm ( ):Boolean { return wormInApple; } } } Finally, Example 8-5 shows the code for the Sushi class, which represents a. | Example 8-3. The Food class continued public function getName String return name public function setName newName String void name newName _ _ Example 8-4 shows the code for the Apple class which represents a specific type of food that pets eat. Example 8-4. The Apple class package zoo public class Apple extends Food private static var DEFAULT_CALORIES int 100 private var wormInApple Boolean public function Apple initialCalories int 0 if initialCalories 0 initialCalories super initialCalories wormInApple .5 setName Apple public function hasWorm Boolean return wormInApple _ _ Finally Example 8-5 shows the code for the Sushi class which represents a specific type of food that pets eat. Example 8-5. The Sushi class package zoo public class Sushi extends Food private static var DEFAULT_CALORIES int 500 public function Sushi initialCalories int 0 if initialCalories 0 initialCalories super initialCalories Datatypes in the Virtual Zoo 157 Example 8-5. The Sushi class continued setName Sushi _ _ More Datatype Study Coming Up In this chapter we learned how to use datatypes to help identify and resolve potential problems in a program. In the next chapter we ll conclude our general exploration of datatypes by studying interfaces. Like classes interfaces are used to create custom datatypes. 158 Chapter 8 Datatypes and Type Checking CHAPTER 9 Interfaces An interface is an ActionScript language construct that defines a new datatype much as a class defines a datatype. However whereas a class both defines a datatype and provides the implementation for it an interface defines a datatype in abstract terms only and provides no implementation for that datatype. That is a class doesn t just declare a bunch of methods and variables it also supplies concrete behavior the method bodies and variable values that make the class actually do something. An interface instead of providing its own implementation is adopted by one or more .

TỪ KHÓA LIÊN QUAN