Java复习_static静态方法(类方法)

package com.java.charactor;

//英雄
public class Hero {

    //姓名
    String name;
    //血量
    float hp;
    //护甲
    float armor;
    //移动速度
    int moveSpeed;
    //版权
    //类属性,静态属性
    static String copyright;

    //为防止继承类报错,一般习惯性的写上无参构造方法
    public Hero() {
    }

    //有参构造方法
    //默认的无参构造方法就失效了
    public Hero(String heroName){
        this.name = heroName;
    }

    public Hero(String name, float hp) {
        this.name = name;
        this.hp = hp;
    }//死亡
    public void die(){
        hp = 0;
    }

    //静态方法 / 类方法(通过类名就可调用)
    public static void battleWin(){
        System.out.println("battle win");
    }

    public String getName() {
        return name;
    }

    public static void main(String[] args) {
        Hero garen = new Hero("盖伦", 500);
        Hero teemo = new Hero("提莫", 383);

garen.die(); System.out.println("盖伦的hp:" + garen.hp); //战斗胜利 Hero.battleWin(); } }
原文地址:https://www.cnblogs.com/CPU-Easy/p/14245942.html