Java self - object-oriented approach

Java class method

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

What to do in the class which is called method

Example 1: What is the method

For example, 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

What is the method

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

Example 2: Method Return Type

Some have a return type of 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 on 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: Parameter Method

Heroes in certain cases, can increase the speed
so that we pass addSpeed increase the speed of this method

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

int speed called the parameters of the method
to increase the speed galenic 100:

Hero garen =  new Hero();
garen.name = "盖伦";
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 naming of

The method of operation is the behavior of a class, it is generally begin with a verb , 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;
    }
     
}

Guess you like

Origin www.cnblogs.com/jeddzd/p/11371943.html