close

This chapter introduces the basic concept of object-oriented. LOL hero games are popular all over the world. I believe you have played it. Suppose we want to design this game and use object-oriented thinking. What should we do?

1. Classes and Objects

Design Hero Class

There are various heroes in LOL. These heroes have their own characteristics. The heroes everyone likes are different. Although these heroes have different lines, different names, and different skills, they have common attributes, such as name, magic resistance, attack range, equipment, etc. So, we can call these heroes with common attributes collectively classes, just like apples, grapes, bananas, they all belong to fruits, whether it is Ash or Anne in LOL hero, they belong to one of the heroes.

In this example, the three data types used are String (string), double (double), and int (integer). Next, we will design the hero class. The attributes included in the class are name, defensive ability, and armor, moving speed. The complete code shown as follows:

public class Hero {

String name;

double defensive;

double armor;

int moveSpeed;

Create Specific Heroes

The designed class is like a template. According to such a template, you can create individual hero. Each hero created corresponds to each specific object. You add the new Hero () code in the class, which mean to create a hero object. In java, the object should be created in the main function. The expression of the main function is “public static void main (String [] args)”. In the main function, you can create the specific hero you like. Here I will give an example of two heroes of Annie and Ashe. The specific process of object creation is as follows:

public static void main(String[] args) {

Hero annie=new Hero();

annie.name=”annie”;

annie.defensive=65.98;

annie.addDefense(50.2);

Hero Ashe=new Hero();

Ashe.armor=67.98;

Ashe.moveSpeed=4;

Do you want to see the effect of the code you wrote? You need to add a code to make the final result appear in the console. This code is System.out.println. Here I give an example to finally output the name and defense ability of the hero Annie. The code is as follows:

System.out.println(“name:”+annie.name+’\n’+”defensive capability:”+annie.defensive);

The final complete code shows:

After running the complete program, click the run button in the toolbar

The final display result will be displayed in the console

some suggestions

Good programming habits will make the code look easier to read and maintain. The first letter of a class is capitalized, such as the Hero category. When two words are concatenated, such as moveSpeed, the first letter of the second word should be capitalized.

2. Attributes

A hero has characteristics such as name, attack ability, and defense ability. These characteristics are called attributes of a class. The type of attributes can be basic variable types, such as in (integer), float (floating point number), double (double precision number), It can also be a class type, such as String. The class type must be capitalized.

The name of the attribute is generally lowercase, the specific example is as follows:

public class Hero {

String name;

double defensive;

double armor;

int moveSpeed;

In the above picture, the word following string, double, int is the name of the attribute.

3. Method

the definition of method, the specific example is as follows:

public class Hero {

String name;

double defensive;

double armor;

int moveSpeed;

void legendary(){

System.out.println(“legendary!”);            }

In LOL, when your hero kills eight other heroes, “legendary” will be displayed in the interface of the game, so we can define a method in our Hero class: void legendary (). Therefore, the method can be understood as what the object can do and what characteristics it has.

In this example, the defined method has no return type, so it is defined as void, which means that the method does not return any value. If there is a return type, change void to the corresponding return type, such as int, float, double, etc.

  • Method Parameters

Heroes can increase their defensive capabilities in some specific situations, so we can increase the hero’s defensive capabilities through the addDefense method, the specific example is as follows:

void addDefense( double defense)

{

defensive=defensive+defense;

}

In this example, double defense is called the parameter of the method. Assuming an increase of 50.2 defense ability to Annie, the code can be expressed as: annie.defensive=65.98;annie.addDefense(50.2); The final rendering effect is

Hero annie=new Hero();

annie.name=”annie”;

annie.defensive=65.98;

annie.addDefense(50.2);

  • Method Naming

The method is the action behavior of the class, so simple and direct words should be used to reflect the behavior of the object. If there are multiple words, the first letter of each subsequent word should be capitalized, such as addDefense.

Dedicated to offer the complete code shown today:

public class Hero {

     String name;

     double defensive;

     double armor;

     int moveSpeed;

     void legendary(){

                 System.out.println(“legendary!”);

     }

     void addDefense( double defense)

     {

                 defensive=defensive+defense;

     }

     public static void main(String[] args) {

                 Hero annie=new Hero();

                 annie.name=”annie”;

                 annie.defensive=65.98;

                 annie.addDefense(50.2);

                 Hero Ashe=new Hero();

                 Ashe.armor=67.98;

                 Ashe.moveSpeed=4;

                 System.out.println(“name:”+annie.name+’\n’+”defensive capability:”+annie.defensive);

     }

            }

The basic steps of object-oriented programming have been introduced. Now you can practice what you have learned today. The following are recommended exercises.

4. Design Items into Types

Category name: Fruit

Fruit has the following attributes:

color type is class type String

name type is class type String

Create three specific fruits

   Name     color

   Apple     red

   Banana   yellow

   Grape    purple

5. Add the Attributes to the Class of the Phone

A mobile phone can display software applications, system settings, battery remaining, wallpaper, etc. You can add the above attributes to the class of the phone, such as phone apps, battery remaining, wallpaper type, etc.

6.  Design Several New Methods for Hero:

1. death (), no parameters, no return type.

2. Get the current blood volume getSpeed(), which has parameters and a return value of int type.

Today’s tutorial is over, I hope people who watch this tutorial can do it by themselves and type the code line by line. I hope you can learn the java language patiently

Tags : basic concept of object-orientedJavaLOL hero

Leave a Response