Object-oriented textbook series (three) - Java class method

Object-oriented textbook series (three) - Java class method

More, click here to learn, sign up for

In LOL, one hero can do many things, such as super god, super ghost, pit teammates

What can be done inside the class method called
Step 1: What is the method of
Step 2: The method return type
Step 3: Method of parameter
naming Step 4:

Example 1: What is the method
such as residual blood fleeing his teammates, the way you used to others blocked, resulting in him being killed. This is the pit teammates
each hero. . . . You can pit
so as Hero class, a design method: keng

public class Hero {
    String name; //姓名
    float hp; //血量
    float armor; //护甲
    int moveSpeed; //移动速度
    //坑队友
    void keng(){
        System.out.println("坑队友!");
    }
}

Example 2: Method return type
and some types of return method is
such a method:

float getArmor(){
  return armor;
}

This method is used to obtain a hero how much armor, return type is float
some method does not return value, this time to put the return type to void, indicates that the method does not return any values
such methods "pit teammate"

void keng(){
  System.out.println("坑队友!");
}
public class Hero {
    String name; //姓名
    float hp; //血量
    float armor; //护甲
    int moveSpeed; //移动速度
    //获取护甲值
    float getArmor(){
        return armor;
    }
    //坑队友
    void keng(){
        System.out.println("坑队友!");
    }
}

Example 3: Method of parameter
hero in certain circumstances, may increase the speed
so that we can increase the speed by this method addSpeed

void addSpeed(int speed){
  //在原来的基础上增加移动速度
  moveSpeed = moveSpeed + speed;
}

int speed parameters known method
to increase the speed galenic 100:
Hero Hero Garen new new = ();
garen.name = "Garen";
garen.moveSpeed = 350;
garen.addSpeed (100);

public class Hero {
    String name; //姓名
    float hp; //血量
    float armor; //护甲
    int moveSpeed; //移动速度
    //坑队友
    void keng(){
        System.out.println("坑队友!");
    }
    //获取护甲值
    float getArmor(){
        return armor;
    }
    //增加移动速度
    void addSpeed(int speed){
        //在原来的基础上增加移动速度
        moveSpeed = moveSpeed + speed;
    }
    public static void main(String[] args) {
         Hero garen =  new Hero();
         garen.name = "盖伦";
         garen.moveSpeed = 350;
         garen.addSpeed(100);
    }
}

Example 4: The naming
method of operation is the behavior of a class, it is generally begin with verbs, such Keng ...
If there is more than one word, the first letter of each word capitalized behind
such addSpeed

public class Hero {
    String name; //姓名
    float hp; //血量
    float armor; //护甲
    int moveSpeed; //移动速度
    //坑队友
    void keng(){
        System.out.println("坑队友!");
    }
    //获取护甲值
    float getArmor(){
        return armor;
    }
    //增加移动速度
    void addSpeed(int speed){
        //在原来的基础上增加移动速度
        moveSpeed = moveSpeed + speed;
    }
}

More, CLICK HERE

Published 32 original articles · won praise 182 · views 10000 +

Guess you like

Origin blog.csdn.net/weixin_44092440/article/details/102962083
Recommended