类:就像一个模板,在类中可以创建一个个的具体的英雄,创建的英雄就叫对象
public class Hero {
String name; //姓名 //类的属性/成员变量:状态
float hp; //血量
float armor; //护甲
int moveSpeed; //移动速度
//坑队友
void keng(){ //方法:在能类里做什么(函数)
System.out.println("坑队友!");
}
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;
}
}
final : 可修饰变量、类和方法
①当一个变量被final修饰的时候,该变量只有一次赋值的机会
②当类被修饰成final的时候,表示该类不能够被继承
③类中的方法被修饰成final,那么该方法在该类中,不能够被重写
一维数组:int[] a;
a = new int[5]; 或 int
[] b =
new
int
[
5
];
或int
[] a =
new
int
[]{
100
,
102
,
444
,
836
,
3236
};
或int
[] b = {
100
,
102
,
444
,
836
,
3236
};
二维数组:
int b[][] = new int[][]{ {1,2,3},{4,5,6},{7,8,9} };
int[][] a = new int[2][3];
增强型for循环遍历:
for (int each:数组)
int values [] = new int[]{18,62,68,82,65,9};
for (int each : values) {
System.out.println(each);
}
构造方法: 方法名和类名一样(包括大小写)、没有返回类型
如果不写,就会默认提供一个无参的构造方法
public class Hero {
String name; float hp; float armor; int moveSpeed;
//带一个参数的构造方法
public Hero(String heroname){
name = heroname;
}
//带两个参数的构造方法
public Hero(String heroname,float herohp){
name = heroname;
hp = herohp;
}
public static void main(String[] args) {
Hero garen = new Hero("盖伦");
Hero teemo = new Hero("提莫",383);
}
}
类属性/静态属性:属性被static修饰
当一个属性被声明成类属性,那么所有的对象,都共享一个值,可理解为全局变量
类方法/静态方法:不需要对象的存在,直接就访问
对象方法/实例方法/非静态方法:必须建立在有一个对象的前提的基础上
package charactor;
public class Hero {
public String name;
protected float hp;
public void die(){ //实例方法,对象方法,非静态方法
hp = 0; //必须有对象才能够调用
}
public static void battleWin(){ //类方法,静态方法
System.out.println("battle win"); //通过类就可以直接调用
}
public static void main(String[] args) {
Hero garen = new Hero();
garen.name = "盖伦";
//必须有一个对象才能调用
garen.die();
Hero teemo = new Hero();
teemo.name = "提莫";
//无需对象,直接通过类调用
Hero.battleWin();
}
}
接口:声明一个方法 physicAttack 物理攻击,但是没有方法体,是一个“空”方法
public interface AD {
//物理伤害
public void physicAttack();
}
实现接口:就必须提供接口中声明的方法physicAttack()
public class ADHero extends Hero implements AD{
@Override
public void physicAttack() {
System.out.println("进行物理攻击");
}
}
参考的网站:how2java