From 35bb7c413118dcb959734e49c16b0f30eb790252 Mon Sep 17 00:00:00 2001 From: xuxin <840198532@qq.com> Date: Wed, 22 Jul 2026 16:33:08 +0800 Subject: [PATCH] =?UTF-8?q?day08-=E9=9D=99=E6=80=81static=E7=9A=84?= =?UTF-8?q?=E6=B3=A8=E6=84=8F=E4=BA=8B=E9=A1=B9=E5=92=8C=E5=8E=9F=E7=90=86?= =?UTF-8?q?=E5=9B=BE=E8=A7=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/com/inmind/static02/StaticDemo11.java | 24 +++++++++++++++++++ day08/src/com/inmind/static02/Student.java | 16 +++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 day08/src/com/inmind/static02/StaticDemo11.java diff --git a/day08/src/com/inmind/static02/StaticDemo11.java b/day08/src/com/inmind/static02/StaticDemo11.java new file mode 100644 index 0000000..9d89584 --- /dev/null +++ b/day08/src/com/inmind/static02/StaticDemo11.java @@ -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(); + } +} diff --git a/day08/src/com/inmind/static02/Student.java b/day08/src/com/inmind/static02/Student.java index d07c5ee..8b7e939 100644 --- a/day08/src/com/inmind/static02/Student.java +++ b/day08/src/com/inmind/static02/Student.java @@ -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("静态方法"); } }