Learn VB .NET Through Game Programming [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

Learn VB .NET Through Game Programming [Electronic resources] - نسخه متنی

Matthew Tagliaferri

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
توضیحات
افزودن یادداشت جدید





Finally, Seeing the Polymorphism in Action

The chapter has taken the long way and demonstrated two implementations of a game class and a piece class. Listing 6-12 shows the code in the main form of the PCOpponent solution, which you can find in the source code that accompanies the book.


Listing 6.12: Calling an Implementation of an Interface Polymorphically



Private aGame As IPCOpponentGame
Private Sub mTic_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles mTic.Click
aGame = New TicTacToeGame(Me)
SetupGame()
End Sub
Private Sub mReversi_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles mReversi.Click
aGame = New ReversiGame(Me)
SetupGame()
End Sub
Private Sub SetupGame()
With aGame
AddHandler .PlayerWon, AddressOf PlayerWon
AddHandler .ComputerWon, AddressOf ComputerWon
AddHandler .NobodyWon, AddressOf NobodyWon
AddHandler .BadMove, AddressOf BadMove
AddHandler .CurrentScore, AddressOf UpdateScores
.StartGame()
End With
Me.Invalidate()
End Sub



In this code, the variable aGame is declared as being of the game interface type. If you’ll recall, this isn’t really saying “The variable aGame is an instance of class x.” Instead, it’s really saying “The variable aGame is an instance of some class that implements interface x.” Once declared in this way, the program can use this variable to store either an instance of the ReversiGame class or the TicTacToe game class, and it can access any of the members of these classes polymorphically (as long as these members are part of the interface).

/ 106