简体   繁体   中英

Text adventure game guidance - Should every class in Java have a public static main method or not?

Currently in the process of creating an OOP version of a text adventure game for my students. I have the following classes:

  • Room
  • Character (which extends to sub classes - Boss, Player and NPC)
  • Item
  • Main
  • Puzzle (maybe a version of Hangman to guess the word)

Currently refreshing on Java as it has been a long time.

Firstly, should every class declared have a main method or should there be a main program class which has one main class.

Secondly, in terms of my classes, I am trying to come up with the variables and methods. So any guidance and tips you can offer would be great.

It's equally as enjoyable as painful at the moment!

The 'main' method is only useful for classes which are the entry point of applications. In essence, you should usually only have one class in an application which has a main method.

There are situations where more than one main method is worthwhile, but those would be situations where a single jar is packaged up with a bundle of aliases or wrapper scripts to provide a simplified command line for a suite of utilities based on common functionality.

As far as your classes are concerned, you should probably start by considering what you want your main program to do rather than what the variables/fields/methods should be. If you focus on the simplest thing possible, which can be expressed in one sentence, then you can build from there. Example - "I want my program to take an input letter from a player and reveal whether that letter is part of the solution". Focusing on implementing something like that will help deciding on structure. Expressing it in terms of input/output (as in 'input is 1 character' and 'output is true/false') can further drive good ideas for methods and fields.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM