From 2ccb704e310c7d8c8b76a8c4dc1e10c580e9c2d4 Mon Sep 17 00:00:00 2001 From: xuxin <840198532@qq.com> Date: Sat, 16 May 2026 15:41:47 +0800 Subject: [PATCH] =?UTF-8?q?day08-=E9=9D=99=E6=80=81static=E5=85=B3?= =?UTF-8?q?=E9=94=AE=E5=AD=97=E4=BF=AE=E9=A5=B0=E6=88=90=E5=91=98=E6=96=B9?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- day08/src/com/inmind/static02/Demo09.java | 26 ++++++++++++++++++++++ day08/src/com/inmind/static02/Student.java | 23 +++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 day08/src/com/inmind/static02/Demo09.java diff --git a/day08/src/com/inmind/static02/Demo09.java b/day08/src/com/inmind/static02/Demo09.java new file mode 100644 index 0000000..03bdd2e --- /dev/null +++ b/day08/src/com/inmind/static02/Demo09.java @@ -0,0 +1,26 @@ +package com.inmind.static02; +/* +static关键字的使用: +2.使用static修饰成员方法的使用方式:只跟类有关,与对象无关了,并且该类的每个对象都可以直接调用静态方法 + 区分 + a.自定义成员方法:(对象方法),它的定义是给每个对象来调用,必须通过创建对象来调用 + b 静态方法:(类方法),它的定义是给对应的类直接调用,跟每个对象无关的操作 + 静态方法的使用方式: + a.类名.静态方法名(参数列表)(推荐使用) + b.对象名.静态方法名(参数列表)(不推荐使用) + + 静态方法的作用:静态方法调用的比较方便,不需要创建对象,常用于对应的工具类的抽取,在一个项目中有可能有特定的功能代码,到处都要用,抽取成静态方法,直接通过类名.静态方法名,直接调用功能即可。 + + + */ +public class Demo09 { + public static void main(String[] args) { + Student s1 = new Student("张三", 18); + Student s2 = new Student("李四", 19); + Student.clazz = "1903室"; + //调用静态方法 + Student.show(); + s1.show(); + s2.show(); + } +} diff --git a/day08/src/com/inmind/static02/Student.java b/day08/src/com/inmind/static02/Student.java index 037bdb5..6cba23a 100644 --- a/day08/src/com/inmind/static02/Student.java +++ b/day08/src/com/inmind/static02/Student.java @@ -16,4 +16,27 @@ public class Student { this.name = name; this.age = age; } + + //静态方法,只跟类有关 + public static void show(){ + System.out.println("学生的教室为:"+clazz); + } + + //成员方法(对象方法),只跟对象有关 + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public int getAge() { + return age; + } + + public void setAge(int age) { + this.age = age; + } + }