TAILIEUCHUNG - Java Programming for absolute beginner- P11
Java Programming for absolute beginner- P11:Hello and welcome to Java Programming for the Absolute Beginner. You probably already have a good understanding of how to use your computer. These days it’s hard to find someone who doesn’t, given the importance of computers in today’s world. Learning to control your computer intimately is what will separate you from the pack! By reading this book, you learn how to accomplish just that through the magic of programming. | 2 25 03 8 51 AM Page 158 Ệ c 2 ặ 0 4- a c Ệ I a Ộ Ĩ J 5 toString method in the BigTruck class overrides this method to include whether or not a trailer is attached. First you used what you learned about the super keyword to call Automobile s version of the method and then you simply appended the trailer value. Polymorphism Polymorphism is another complicated word for a simple concept. It simply means that an instance of a subclass is also an instance of its superclass. A BigTruck instance is an Automobile too. All classes inherit from the Object class defined in the Java language so every instance of Automobile and BigTruck is also an instance of Object. For example if a method existed that accepted an Automobile object as a parameter public void doSomething Automobile auto . You can pass in a BigTruck object because it is an Automobile BigTruck truck new BigTruck doSomething truck Back to the Blackjack Game You ve learned a great deal about object-oriented programming in this chapter. Now you put this knowledge to good use by creating a text-based version of a blackjack game. In this section you subclass the CardDeck class to create RandomCardDeck a randomized deck of cards so the game isn t predictable. You also learn about the class which will help in creating the final game. Finally you write the actual Blackjack application. The RandomCardDeck Class You ve already written a class that represents a deck of cards the CardDeck class. Now you just need to override it so that the Cards stored in the deck are randomized. To do this you import the class. In the constructor you call the superclass s constructor to build a regular CardDeck and then you call the new shuffle method to rearrange the Cards randomly. One other thing you need to do is override the reset method so that when you reset the top card you also shuffle the deck. Here is the source listing for RandomCardDeck Simulates a .
đang nạp các trang xem trước