Java classes and objects
Introducing the basic concepts of object-oriented
Suppose that we want to design a LOL this game, using the object-oriented thinking to design, how to do?
Step 1: Design This class hero
LOL There are a lot of heroes, such as the blind monk, team battles can be lost, Timo must die, Galen, piano female
all these heroes, there are some common state
, for example, they all have names, hp, armor, movement speed, etc.
so that we can design a thing called class that represents a heroic things
like: hero (hero)
state: name, blood, armor, speed
NOTE: This embodiment uses three kinds of data types are String (String), a float (float), int (integer).
Note: This class is not the primary method, do not attempt to run it. Not all classes are the main method.
public class Hero {
String name; //姓名
float hp; //血量
float armor; //护甲
int moveSpeed; //移动速度
}
Step 2: Create a specific hero
A class is like a template, based on such a template, you can create one specific hero
one particular hero, called one object
new Hero () is to create a hero java objects mean
public class Hero {
String name; //姓名
float hp; //血量
float armor; //护甲
int moveSpeed; //移动速度
public static void main(String[] args) {
Hero garen = new Hero();
garen.name = "盖伦";
garen.hp = 616.28f;
garen.armor = 27.536f;
garen.moveSpeed = 350;
Hero teemo = new Hero();
teemo.name = "提莫";
teemo.hp = 383f;
teemo.armor = 14f;
teemo.moveSpeed = 330;
}
}
Step 3: The first class letter capitalized
Good programming practice makes the code look more crisp, easy to read, easy to maintain
such type the first letter capitalized Hero
public class Hero {
}