Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In your example code you're trying to set variable value in class body - what's invalid since direct content of class body never gets executed (except field initializations). You have to put your code to some method (for example to constructor).</p> <pre><code>public class TowerArcher extends TowerBase{ public TowerArcher() { super(); //set values as in TowerBase constructor hp = 10; //archers don't wear armor, so they have low HP. } } </code></pre> <p><code>extends</code> simply uses form of specified parent class for fields of descendants and serves as base of methods which can, but don't have to be overridden. Fields set as <code>public</code> or <code>protected</code> (but not <code>private</code>) in super class (that's the official name of class which stands after <code>extends</code> keyword) are accessible in extended object. </p> <p>In your program, you're using <code>extends</code> correctly. New types of towers will bring new classes, which will extends <code>TowerBase</code>. </p> <p>Note that there can be only one class used as parent by <code>extends</code> keyword.</p> <p><code>implements</code> implements interfaces (interfaces, because you can implement more interfaces). In theory base, interfaces are similar to super classes, but they usually hold only one property and they manipulate with it. In your program, you can create <code>IFlameThrowing</code> or <code>IStoneThrowing</code> interfaces which would manage just one property of concrete tower (for example archers can't throw stones, but they can shoot fire arrows).</p> <p>You have many different towers, so create structure which will divide all the things mentioned to towers, and properties. Then implement it. If you want to allow player to add upgrades to his tower during game (for example to learn his archers how to throw stones), use decorator pattern. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload