day08--静态static的原理图解

This commit is contained in:
xx
2026-09-22 15:55:39 +08:00
parent 17d3630c89
commit 5d82b02076
2 changed files with 28 additions and 8 deletions
@@ -27,7 +27,5 @@ public class Demo09 {
//校长问有多少同学了?? //校长问有多少同学了??
Student.showStudentCount(); Student.showStudentCount();
s1.study(); s1.study();
} }
} }
+28 -6
View File
@@ -1,5 +1,19 @@
package com.inmind.static02; package com.inmind.static02;
/*
学习静态的注意事项:
静态方法调用的注意事项:
1.静态方法可以直接访问类变量(静态变量)和静态方法。
2.静态方法不能直接访问普通成员变量或成员方法。静态方法只能访问静态内容,反之,成员方法可以直接访问静态变量或静态方法。(先人(静态)不知道后人(对象),后人(张三)是知道先人(刘备)的)
3.静态方法中,不能使用this关键字。
static 修饰的内容:
是随着类的加载而加载的,且只加载一次。(只跟类有关)
存储于一块固定的内存区域(静态区),所以,可以直接被类名调用。
它优先于对象存在,所以,可以被所有对象共享。
*/
public class Student { public class Student {
String name; String name;
int age; int age;
@@ -16,13 +30,21 @@ public class Student {
studentCount++; studentCount++;
} }
//静态方法,不再是一个成员方法了 //静态方法,不再是一个成员方法了(不能访问属性和行为)
public static void showStudentCount(){ public static void showStudentCount(){
// System.out.println(this);
System.out.println(studentCount);
/*study(); 静态方法是不能访问非静态内容的!!!!
System.out.println(name);
System.out.println(age);*/
}
//成员方法(非静态内容,是可以访问静态内容)
public void study(){
System.out.println(this.name+"的班级有"+studentCount+"人");
System.out.println(this);
showStudentCount();
System.out.println(clazz);
System.out.println(studentCount); System.out.println(studentCount);
} }
//成员方法
public void study(){
}
} }