Java game development: knowledge about java object-oriented (4)

This is the last tutorial: Go To It!
The new year has passed. We have finished the tutorial for several days. Today we begin to continue our study (happy New Year ~)

Implementation of game character class

For a game character, we can think of the following fields:

  1. Max Hp
  2. Current blood volume
  3. name
  4. Equipment array (it can also be a list, but maybe we don't know what a list is yet...)
  5. Current map
  6. Current coordinate array
  7. Grade
  8. Death

Let's think about those fields. We can encapsulate them into attributes

It seems that the maximum HP is not necessary, because the maximum HP will only change its size due to the numerical transformation of equipment and level. We can set it as a private (private) field, but it is not necessary to encapsulate it as an attribute

The current blood volume is OK ~ because we have to check whether his blood volume is lower than 0 every time he is injured. If it is lower than 0, the character will die. Each time we return blood, we also need to see whether the blood volume exceeds the maximum blood volume. If it exceeds the limit, it is necessary to set it as an attribute

Name, equipment, map coordinate level can be treated as a field without encapsulation (if you like)

**However, in order to ensure the integrity of the data, you can consider privatizing all the above fields**

We can set these methods

  1. Skills (no problem with multiple) - character's actions and superpowers
  2. Upgrade (of course, you can encapsulate the level into an attribute, and pass 1 to this attribute to upgrade. It depends on your habit). The increase of level often leads to the increase of the upper limit of character's HP
  3. Move - change of map or coordinates
  4. . . . . A lot of people need to watch your game

Let's knock on the code to see if it can be implemented?

public class chara {
    private int maxHp;
    private int _nowHp;
    public String name;
    public String[] arrayEquip;
    public String nowArea;
    public int[] nowPosition;
    public int level;
    public boolean isDead = false;

    public int get_nowHp() {

        return _nowHp;
    public void set_nowHp(int hpValue){
        // params: hpValue: the value that affects blood volume
        int temp = _nowHp - hpValue;
        if (temp > 0) _nowHp = temp; // Just give it to her when your blood volume is normal
        else if(temp > maxHp) _nowHp = maxHp; // If the HP is greater than the maximum HP, it means that your HP recovery is full, so you need to limit the upper limit
        else if(temp < 0) isDead = true; // Character death~
    public int skillA(){
        return 12;// 12 is injury

this detailed explanation

We talked about this in the last tutorial, but it's just a cursory skim. Now let's talk about the function of this in detail

public class student {
    private String _name; // full name
    private String _id; // Test number
    private String _classroom; // examination room
    public student(){
        // Construction method without parameters

    public student(String _id,String _name,String _classroom){
        // Parametric construction method of student




In the code, we created a student class and printed it in its construction method_ Because of the scope of the variable id, the JVM will print the literal value of the parameter id first instead of the literal value of the student class field
Let's remember that this is very important and you need to think about it

In methods (including constructors) with formal parameters or local variables with the same name as class fields, the fields of the class will be hidden.

When we need to use the value of the class field in the method with the same name as the class field, we can add this in front of the class field. We know that this is to create a variable within the class that references its own instance object, but unlike the external object, this keyword can easily access the private part of the class
We upgraded the previous code and got

public class student {
    private String _name; // full name
    private String _id; // Test number
    private String _classroom; // examination room
    public student(){
        // Construction method without parameters

    public student(String _id,String _name,String _classroom){
        // Parametric construction method of student

        this._id = _id;
        this._name = _name;



These are almost the basic knowledge of classes. In the later study, we need to continue to understand other functions of classes. The following is the supplementary part of class foundation:

Class supplement

Naming conventions for identifiers

Naming conventions for classes

  • Use the name Car - Car class that indirectly represents the content of the class
  • Remember to capitalize the first letter of the word, followed by lowercase Person - character class
  • If it is arranged by multiple words, the first letter of each word is capitalized. NetworkManager - network management class

Naming conventions for methods (except construction methods)

  • A word is directly lowercase move
  • If there are multiple words, the first word is all lowercase, and the first letter of the following word is uppercase moveMap

If you are still interested, please visit here: Java_ Naming conventions

Tags: Java Programming OOP

Posted by Gump on Sun, 17 Apr 2022 06:03:07 +0930