Day12 static,Arrays,Math

78 阅读1分钟

static在里面修饰成员变量,不属于对象而属于类,意思是多个对象共用此变量
static修饰成员方法,同理

Student stu=new Student();
stu.eat();//不推荐
Student.eat();//推荐

原因:使用对象调用会误以为是普通的成员方法
对于本类中的静态方法可以省略类名称
注意事项:
1.静态方法不能访问访问非静态内容,在内存中先有静态然后才有非静态
2.静态方法不能用this调用

静态代码块优先与构造方法的执行,并且静态代码块只执行一次

Arrays用法

Arrays.toString(array);//数组合并成字符串
Array.sort(array);//对数组升序排序

数学类用法

Math.abs(-2.5);//2.5,绝对值
Math.ceil(2.5);//3,上取整
Math.floor(2.5);//2,下取整
Math.round(2.5);//3,四舍五入