2016-07-08 35 views
-2
// in general, it could be potion, armor, weapon, etc. 
Item class { 
    // states 
    id 
    name 
    // behavior 
    set, get of states here.. 

    use() 
    // still thinking what is the other general behavior 
} 

// in general, it could be Gun,Sword,Wand, Bow (range,melee,magic) 
Weapon class extends Item{ 
    // gun - fire(); type: range (in the movie, the gun can be use as melee weapon too xD) 
    // sword - slash(); type: melee (in the movie, the sword can be thrown :) or use as magic wand?) 
    // wand - ?? 
    // mace - bash(); 
    // bow - shoot() ?? 
    // so what is the general behavior of this weapon? 
} 

Bag class {} // parent is Item, collection of items here 

// Human, Orc, etc 
Character class { 
    // states 
    Weapon 

    // behavior 
    attack(); 
    // still thinking what is the other behavior and states 
} 

// Swordsman, Magician, Archer, etc. 
Hero class extends Character{ 
} 

上面的代碼是角色扮演,我開發遊戲我的OOP類,我在一般狀態很難思維和行爲的每個類。Java的面向對象 - 角色扮演類一般

問題

  • 如何創建這些類(我在OOP的初學者)。我聽說過一些封裝,polymorphish等
  • 我不知道如何正確使用接口,抽象等
  • 一般狀態和類行爲
  • 思考
  • 正如你可以在武器看類。槍,劍,弓可以用作近戰,射程,甚至魔法。我應該把這些WeaponType稱爲什麼? (範圍,肉搏,法術)
+1

注意它是'Foo類'而不是'Foo類'也許你可以從閱讀一些教程開始,比如https://docs.oracle.com/javase/tutorial/java/index.html,這個問題太寬泛了作爲 –

+0

確定你有正確的標籤?這當然不是Java源代碼?! – GhostCat

+0

只是一個錯字,對不起。 – ronscript

回答

2

讓我們一行

如何創建這些類(我在OOP的初學者)回答您的疑問線。我聽說過一些 封裝,polymorphish等

你不能開發一個遊戲,而不具有通了解OOPS.Spend的一段時間理解抽象/封裝/ Polymerphsim /繼承。

我不知道,如果你知道這些意味着你將能夠決定何時以及如何在所有的你,如果使用他們如何正確使用接口,抽象等

也許會需要他們。

思考類的一般狀態和行爲正如你可以在武器類中看到 。槍,劍,弓可以用作近戰,射程, 甚至魔法。我應該把這些WeaponType稱爲什麼? (範圍,肉搏,法術)

在這種情況下,你可以有一個接口稱爲武器與他們的類型作爲抽象methods.You可以繼承他們,併爲每個武器等類似的攻擊力,力量的武器屬性更改等等。當然,您還需要通過OOPS概念。 如果你是一個快速學習者Start by watching this Video.

+0

感謝您的回答! – ronscript

0

對於像遊戲這樣的複雜設計,我建議你學習更好的接口和抽象類。然後我建議你畫一個遊戲模型的UML圖。 武器可以是一個Fire()方法的接口,所有實現該接口的武器都將擁有自己的fire()方法。

對於一個好的設計,我也建議非常好地學習設計模式。如果你對你的遊戲做了很好的設計,未來將會非常容易地用新功能擴展你的遊戲!