Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (494)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  I want to use Inheritance in a static way  (Read 686 times)
0 Members and 1 Guest are viewing this topic.
Offline supaFool

Junior Member


Projects: 2



« Posted 2013-08-11 17:55:18 »

I have a bunch of subclasses extending a Skill Type,
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
public abstract class Skill {

   // current level
  private static int fishLevel = 1;
   private static boolean isFishing = false;

   private static int miningLevel = 1;
   private static boolean isMining = false;

   private static int attackLevel = 1;
   private static int defLevel = 1;
   private static int strLevel = 1;
   private static int teleLevel = 1;

   public Skill(String name) {
   }

   /**
    * @return the fishLevel
    */

   public static int getFishLevel() {
      return fishLevel;
   }

   /**
    * @param fishLevel
    *            the fishLevel to set
    */

   public static void levelUpFishing() {
      fishLevel++;
   }

   /**
    * @return the isFishing
    */

   public static boolean isFishing() {
      return isFishing;
   }

   /**
    * @param isFishing
    *            the isFishing to set
    */

   public static void setIsFishing(boolean isFishing) {
      Skill.isFishing = isFishing;
   }}


I want to use Inheritance so i dont have to keep typing the same stuff again and again, like i have been doing :
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
/**
 * Uses the static methods
 *
 * @author Erick
 *
 */


public class Fishing extends Skill {

   public Fishing() {
   }

   public static void levelUp() {
      levelUpFishing();
   }
   
   public static String getId() {
      return "Fishing";
   }

}


I know I could use the static methods in the Skill Class, but i also need to set Icons and other things, that inheritance would help out with tremendously, is there a way to do this, I don't want to make instances of each skill.
Offline greenOwl

Junior Member


Medals: 1


from Germany


« Reply #1 - Posted 2013-08-11 18:01:29 »

If you want to do things like static inheritance you probably should re-think your OO-design.

Why does the class Skill has fields specific to "Fishing" although you have an explicit class for it as well?
Why not instantiate skills? I could imagine different characters having a different skill-level makeing and OO-approach a necessity.

elfeck
Offline supaFool

Junior Member


Projects: 2



« Reply #2 - Posted 2013-08-11 18:12:10 »


Why does the class Skill has fields specific to "Fishing" although you have an explicit class for it as well?

thats a good question, this was a example lol

I dont want to instantiate each skill because they simply don't need to be, I am just using it store the current level and name and other things, so the when one levels up fishing, i can simply Fishing.levelUp(); ,  and later in the game if i need to get the level i can just Fishing.getLevel(); , but i need to do this with all of the levels in a static way, so i cant make a interface, or abstract methods, just wondering if there was a way to do it.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline supaFool

Junior Member


Projects: 2



« Reply #3 - Posted 2013-08-11 18:17:25 »

BTW it is a game like runescape, so the skills are changing alot. not all entities has the same skills.
Offline ralphchapin

Junior Member


Medals: 3
Projects: 1



« Reply #4 - Posted 2013-08-11 18:20:32 »

I'd create a MetaSkill class and put the static stuff in there as non-static.  Create an instance of MetaSkill and use it instead of Skill for the formerly static methods.  MetaSkill can inherit from another class and implement interfaces.  It can also be inherited from.  Your Skill subclasses can all work with one MetaSkill instance, or you can create several MetaSkill instances with different data.  You could even have your different Skill subclasses reference different MetaSkill subclasses.  You might even find it handy to have MetaSkill itself be an interface.  (You'll probably find it handy to have a Skill method that returns the relevant MetaSkill instance.)

Anyway, you get the idea.  Do what works for you.  Trying to make statics work here just doesn't work; I've tried it.
Offline supaFool

Junior Member


Projects: 2



« Reply #5 - Posted 2013-08-11 18:26:50 »

I Rock!

Awesome, thanks for answering in a programmer fashion Smiley  I didn't think of this.
Offline supaFool

Junior Member


Projects: 2



« Reply #6 - Posted 2013-08-12 17:23:41 »

Well im spending to much time on this, so I'm going to just instantiate everything..lol

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
package com.supafool.apps.games.supascape.skill;

/**
 * Abstract class used to hold data for each skill
 *
 * @author Erick
 *
 */


public abstract class SkillAb {

   // current amount of xp
  protected double currentXp = 0.00;

   // xp needed to get next level
  protected double levelXp = 150.00;

   // Current level, not max level
  protected int currentLevel = 1;

   // Max Level is the max you can have, like 8/10, this is what levels up
  protected int maxLevel = 1;

   // Amount to raise the levelXp after skill has been leveled
  private final double levelOffSet = 1.35;

   // used to keep 2 skills from being called at the same time
  protected boolean isSkilling = false;

   public SkillAb() {
      initSkills();
   }

   public abstract void initSkills();

   public void gainXp(double amountToGain) {
      currentXp += amountToGain;

      // check is needs leveled
     if (currentXp > levelXp) {
         levelUp();

         // raise xp needed to level again
        levelXp = levelXp * (maxLevel * levelOffSet);

      }

   }

   public int getCurrentLevel() {
      return currentLevel;
   }

   public void levelUp() {
      maxLevel++;

      // Fill up current Level
     currentLevel = maxLevel;
   }

   public void setIsSkilling(boolean b) {
      isSkilling = b;
   }

   public boolean isSkilling() {
      // TODO Auto-generated method stub
     return isSkilling;
   }

   public abstract String getSkillName();

}
Online ags1

JGO Ninja


Medals: 62
Projects: 3
Exp: 5 years


Make code not war!


« Reply #7 - Posted 2013-08-12 21:21:12 »

It is better to put this information at instance level. Storing a lot of state in static variables is asking for trouble. Static variables should be for unchanging (or almost unchanging) values or stateless utility functions. If you make your skills static you also force yourself into having only one instance of the skill - what if want to give the same skill to monster, or if the player can have multiple characters, or if you dare to try add multiplayer...?

Offline ralphchapin

Junior Member


Medals: 3
Projects: 1



« Reply #8 - Posted 2013-08-13 00:18:13 »

Just get something running.  You can junk the statics later when (A) you can see that you have to and (B) you've got a better idea how to junk them.  Doing a lot of work that doesn't immediately get your game going is a drag.  Doing a lot of work that, in the end, doesn't do what you want is typical big business IT, but not something you want to do when you're not middle management.

There's a lot to be said for doing it right, but don't get ahead of yourself.
Offline sproingie

JGO Kernel


Medals: 202



« Reply #9 - Posted 2013-08-13 16:15:29 »

Actually understanding what you're doing and how things like OOP works is better than "just getting something running".  There's inelegant code, and some code's always going to feel that way, and there's totally missing basic concepts of design.  The first will just annoy you, the latter will ultimately keep you from getting what you want done.

But if it's your first few projects ... just get something running Smiley
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline jonjava
« Reply #10 - Posted 2013-08-13 17:00:58 »

I love an oxymoron as much as I love giraffes and this threads title is no exception.


Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Dwinin (21 views)
2014-09-12 09:08:26

Norakomi (55 views)
2014-09-10 13:57:51

TehJavaDev (66 views)
2014-09-10 06:39:09

Tekkerue (33 views)
2014-09-09 02:24:56

mitcheeb (54 views)
2014-09-08 06:06:29

BurntPizza (38 views)
2014-09-07 01:13:42

Longarmx (24 views)
2014-09-07 01:12:14

Longarmx (30 views)
2014-09-07 01:11:22

Longarmx (28 views)
2014-09-07 01:10:19

mitcheeb (37 views)
2014-09-04 23:08:59
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!