〔005〕Java basic object-oriented

▷Object-oriented

  • 对象:is a special data structure
  • 对象: is derived from class new. With the class, you can create objects, for example Random r = new Random(), where r is an object
  • Format: public class 类名 { 变量和一些类方法 }
  • Object-oriented programming is more in line with human thinking habits, and programming will be more intuitive
  • In fact, everything is an object. If you want to operate student data, go to the student object; if you want to operate random numbers, go to the random number object. For exampler.nextInt()
  • Each object can implement different functions, making the code clearer and more intuitive.
  • For example, in the game, to generate npc, you need to use the npc object to process it; to generate a weapon, you need to use the weapon object to process it
// 新建类,Npc.java
package tiny.geeker;

public class Npc {
    
    
    String name; // 生成npc的名字
    double blood; // 给npc加血量

    public void printName() {
    
    
        System.out.println("创建的NPC是:" + name);
    }

    public void printBlood() {
    
    
        System.out.println("创建的NPC是:" + name + ",它的血量为:" + blood);
    }
}

// 调用类
package tiny.geeker;

public class Test {
    
    
    public static void main(String[] args) {
    
    
        Npc npc = new Npc();

        npc.name = "德玛西亚";
        npc.blood = 999999;

        npc.printName();
        npc.printBlood();
    }
}

First time acquaintance

▷ Precautions

  • 第一:It is recommended that the class name be in English with the first letter capitalized, meet the camel case pattern and be meaningful, so that others can know at a glance what the general function of this class is, such as the aboveNpc
  • 第二: The variables defined in the class are called member variables or attributes of the object, such as the above name and blood; the methods defined in the class are called Member methods are also called object behaviors, such as printName and aboveprintBlood
  • 第三: Multiple classes can be written in a class file, but they can only be modified with one public
  • 第四:Class member variables themselves have default values ​​and generally do not need to be assigned initial values.
  • 第五:The data between objects will not be affected, but when multiple variables point to the same object, they will be affected.
  • 第六:If an object does not have a variable that references it, the object becomes garbage.
package tiny.geeker;

public class Test {
    
    
    public static void main(String[] args) {
    
    
        Npc npc = new Npc();
        // 类成员变量本身存在默认值,一般不需要赋初始值
        System.out.println(npc.name);
        System.out.println(npc.blood);

        // 对象和对象之间的数据不会影响
        Npc npc1 = new Npc();
        npc1.name = "德玛西亚";
        npc1.printName();

        Npc npc2 = new Npc();
        npc2.name = "飞兔小哥";
        npc2.printName();

        // 多个变量指向同一个对象时就会相会影响
        Npc npc3 = new Npc();
        Npc npc4 = npc3;
        npc3.name = "德玛西亚";
        npc3.printName();
        npc4.name = "飞兔小哥";
        npc3.printName();

        // 如果某个对象没有一个变量引用它,那么该对象就会成为垃圾对象
        Npc npc5 = new Npc();
        npc5 = null;
        npc5.printBlood();
    }
}

Precautions

▷ this keyword

  • this:In fact, it is a variable that points to the current object in the method.
  • You can see that the class name printed in the classthis and the class name printed when calling both point to the same address, so you can knowthis It points to the class itself
  • So in a class, you can use this to assign values ​​to variables in the class and call methods in the class itself

this keyword
Use this

▷Constructor

  • 构造器: When a class is instantiated using new, the constructor will be called, and you can often perform some initialization operations on the class
  • 构造器: division 无参构造器 sum 有参构造器
  • Format: public class Npc { public Npc() { ... } } The constructor is actually a method with the same name as the class and has no return value type
package tiny.geeker;

public class Npc {
    
    
    public Npc() {
    
    
        System.out.println("我是构造方法");
    }
}

Construction method

▷ Overloading constructor methods

  • In the overloading of the previous method, we know that when the method name is the same but the parameters are different, it is called method overloading. Then the overloading of the constructor is the same, but it is also called有参构造器

Overloaded constructor

▷ Entity class

  • This is a special class that is only used to save data. You can use it to create objects and save data about something.
  • The variables of the member must be私有, use private, and do not expose the variables to the outside world, but through get, , a>set method to operate variables, and must have无参数构造器
  • Entity classes are mainly used to separate data and data business processing from each other.
package tiny.geeker;

public class Npc {
    
    
    private String name;
    private double blood;

    public Npc() {
    
    
    }

    public Npc(String name, double blood) {
    
    
        this.name = name;
        this.blood = blood;
    }

    public String getName() {
    
    
        return name;
    }

    public void setName(String name) {
    
    
        this.name = name;
    }

    public double getBlood() {
    
    
        return blood;
    }

    public void setBlood(double blood) {
    
    
        this.blood = blood;
    }
}

Entity class

▷ Case: Hero Search

  • Now we create the Hero class and set up multiple heroes, which can be retrieved by the hero's id
  • It is necessary to define the Hero class for creating heroes and the classes for operating Heroes. The creation class is responsible for saving hero data, and the operation class is responsible for retrieving heroes.
package tiny.geeker;

public class HeroAction {
    
    
    private Hero[] heros;

    public HeroAction(Hero[] heros) {
    
    
        this.heros = heros;
    }

    public void printAllHeros() {
    
    
        System.out.println("-----所有的英雄信息如下-----");
        for (int i = 0; i < this.heros.length; i++) {
    
    
            Hero h = heros[i];
            System.out.println("id:" + h.getId() + " 名称:" + h.getName() + " 血量:" + h.getBlood());
        }
    }

    public void getHeroById(int id) {
    
    
        System.out.println("-----检索的英雄信息如下-----");
        for (int i = 0; i < this.heros.length; i++) {
    
    
            Hero h = heros[i];
            if(h.getId() == id) {
    
    
                System.out.println("id:" + h.getId() + " 名称:" + h.getName() + " 血量:" + h.getBlood());
                return;
            }
        }
        System.out.println("没有该英雄信息");
    }
}

Hero type

Guess you like

Origin blog.csdn.net/weixin_41635750/article/details/134415706