day08-静态static的原理图解

This commit is contained in:
2026-05-16 16:16:56 +08:00
parent 2ccb704e31
commit 42c7e02de6
2 changed files with 19 additions and 5 deletions

View File

@@ -10,8 +10,17 @@ static关键字的使用
b.对象名.静态方法名(参数列表)(不推荐使用)
静态方法的作用:静态方法调用的比较方便,不需要创建对象,常用于对应的工具类的抽取,在一个项目中有可能有特定的功能代码,到处都要用,抽取成静态方法,直接通过类名.静态方法名,直接调用功能即可。
--------------------------------------------------------------------------------------
学习静态的注意事项:
静态方法调用的注意事项:
1.静态方法可以直接访问类变量(静态变量)和静态方法。
2.静态方法不能直接访问普通成员变量或成员方法。静态方法只能访问静态内容,反之,成员方法可以直接访问静态变量或静态方法。(先人(静态内容)不知道后人(对象),后人(对象)是知道先人(静态内容)的)
3.静态方法中不能使用this关键字。
static 修饰的内容:
是随着类的加载而加载的,且只加载一次。(只跟类有关)
存储于一块固定的内存区域(静态区),所以,可以直接被类名调用。
它优先于对象存在,所以,可以被所有对象共享。
*/
public class Demo09 {
public static void main(String[] args) {

View File

@@ -1,17 +1,13 @@
package com.inmind.static02;
public class Student {
//属性(成员变量)
String name;
int age;
//静态变量
static String clazz;
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
@@ -20,10 +16,19 @@ public class Student {
//静态方法,只跟类有关
public static void show(){
System.out.println("学生的教室为:"+clazz);
//注意:静态方法中不能访问非静态的内容
/*System.out.println("学生的姓名为:"+name);
System.out.println("学生的年龄为:"+age);
getName();*/
//注意在静态方法无法使用this关键字
// System.out.println(this);
}
//成员方法(对象方法),只跟对象有关
public String getName() {
//注意:在非静态方法(成员方法)中可以访问静态内容
System.out.println(clazz);
show();
return name;
}