day08-静态static的注意事项和原理图解
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
package com.inmind.static02;
|
||||
/*
|
||||
学习静态的注意事项:
|
||||
静态方法调用的注意事项:
|
||||
1.静态方法可以直接访问类变量(静态变量)和静态方法。
|
||||
2.静态方法不能直接访问普通成员变量或成员方法。静态方法只能访问静态内容,
|
||||
反之,成员方法可以直接访问静态变量或静态方法。(先人不知道后人,后人是知道先人的)
|
||||
3.静态方法中,不能使用this关键字。
|
||||
|
||||
static 修饰的内容:
|
||||
是随着类的加载而加载的,且只加载一次。(只跟类有关)
|
||||
存储于一块固定的内存区域(静态区),所以,可以直接被类名调用。
|
||||
它优先于对象存在,所以,可以被所有对象共享。
|
||||
*/
|
||||
public class StaticDemo11 {
|
||||
public static void main(String[] args) {
|
||||
Student s1 = new Student("张三");
|
||||
Student s2 = new Student("李四");
|
||||
Student s3 = new Student("王五");
|
||||
Student.classRoom = "1903";
|
||||
s1.show();
|
||||
Student.showCount();
|
||||
}
|
||||
}
|
||||
@@ -14,11 +14,27 @@ public class Student {
|
||||
}
|
||||
|
||||
public void show() {//成员方法(对象方法)
|
||||
//在成员方法(非静态方法)中可以访问静态变量的classRoom
|
||||
System.out.println("姓名:"+name+"在"+classRoom+"教室上课");
|
||||
//在成员方法(非静态方法)中可以访问静态方法
|
||||
show1();
|
||||
showCount();
|
||||
}
|
||||
|
||||
//展示有多少个学生
|
||||
public static void showCount(){//静态方法
|
||||
//静态变量
|
||||
System.out.println("当前教室有"+studentCount+"个学生");
|
||||
//静态方法
|
||||
show1();
|
||||
//成员变量在静态方法中无法访问
|
||||
/*System.out.println(this.name);
|
||||
System.out.println(age);*/
|
||||
//成员方法在静态方法中无法访问
|
||||
//show();
|
||||
}
|
||||
|
||||
public static void show1(){
|
||||
System.out.println("静态方法");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user