static
static声明的变量和方法称为类属性和类方法
---直接调用,不需要初始化,一般用“类名.属性” “类名.方法名”
--static方法只能使用static属性;
package mypro01; /** * student类 * @author 4090039qrh * */ public class Student { //非static属性 String name; int age; //static属性 static double weight; static double height; //static方法只能调用static变量 public static void cal_weight() { // System.out.println(name); 不能调用非static属性 name System.out.println(weight); } //非static方法可以调用所有变量 public void cal_height() { System.out.println(name); System.out.println(height); } public static void main(String[] args) { //static属性和方法不需要创建对象,直接调用 Student.weight=100; Student.height=50; Student.cal_weight(); //非static属性和方法需要创建对象,才可以调用 Student s =new Student(); s.name="qiao"; s.age=18; s.cal_height(); }
posted on 2020-02-23 10:41 happygril3 阅读(109) 评论(0) 收藏 举报
浙公网安备 33010602011771号