类方法
  TEZNKK3IfmPf 2023年11月12日 34 0

1.1 形式如下:

访问修饰符  static  数据返回类型  方法名() { }   [推荐] 

static  访问修饰符  数据返回类型  方法名() { }

2. 类方法的调用

2.1 使用方式:

类名.类方法名

对象名.类方法名  [前提是满足访问修饰符的访问权限和范围]

类方法

2.2 快速入门  378

统计学费总和

代码在com.stulzl.staticmethod.包中

StaticMethod 
package com.stulzl.staticmethod;

//统计学费总和  378
public class StaticMethod {
    public static void main(String[] args) {
        Stu stu1 = new Stu("tom");
        stu1.payFee(100);
        //Stu.payFee(100);//效果和上者一样
        
        Stu stu2 = new Stu("mary");
        stu1.payFee(200);
        //Stu.payFee(200);//效果和上者一样
        
        //输出当前收到的总学费
        Stu.showFee();//300
    }
}
class Stu{
    private String name;
    //定义一个静态变量
    private static double fee = 0;
    //构造器
    public Stu(String name) {
        this.name = name;
    }

    //说明
    //1. 当方法使用了 static 修饰后,该方法就是静态方法
    //2. 静态方法就可以访问静态属性/变量
    public static void payFee(double fee){
        Stu.fee+=fee;//学费相加
    }
    public static void showFee(){
        System.out.println("总学费有:"+Stu.fee);
    }
}

3. 类方法经典的使用场景  379

类方法

//类方法经典的使用场景举例
//开发自己的工具类时,可以将方法做成静态的,方便调用
class MyTools{
    //求两个数的和
    public static double calSum(double n1,double n2){
        return n1+n2;
    }
    //还可以下厨很多这样的工具方法……
}

4. 类方法使用注意事项和细节讨论  380

1)类方法和普通方法都是随着类的加载而加载,将结构信息存储在方法区:类方法中无this的参数,普通方法中隐含着this的参数

2)类方法可以通过类名调用,也可以通过对象名调用。[举例]

3)普通方法和对象有关,需要通过对象名调用,比如对象名方法名(参数),不能通过类名调用。[举例]

4)类方法中不允许使用和对象有关的关键字,比如this和super。 普通方法(成员方法)可以。

[举例]

5)类方法(静态方法)中只能访问静态变量或静态方法。[如何理解]

6)普通成员方法,既可以访问非静态成员,也可以访问静态成员。

小结: 静态方法,只能访问静态的成员,非静态的方法,可以访问静态成员和非静态成员(必须遵守访问权限)

代码在com.stulzl.staticmethod_detail.包中

StaticMethod_Detail

package com.stulzl.staticmethod_detail;

//类方法使用细节  380
public class StaticMethod_Detail {
    public static void main(String[] args) {
        //普通方法和对象有关,需要通过对象名调用,比如对象名方法名(参数),不能通过类名调用。
        //非静态方法,不能通过类名调用
        //D.say();, 错误,需要先创建对象,再调用

        D.hi();//这是静态方法调用,正确可以的使用类名调用

    }
}
class D{
    private int n1 = 100;
    private static int n2 = 200;//静态的
    //普通方法,非静态方法
    public void say(){

    }
    //静态方法
    public static void hi(){
        //类方法中不允许使用和对象有关的关键字,比如this和super。
        //普通方法(成员方法)可以。
        //System.out.println(this.n1);//错误
    }
    //静态方法
    public static void hello(){
        //类方法(静态方法)中只能访问静态变量或静态方法
        System.out.println(n2);
        System.out.println(D.n2);
        //System.out.println(this.n2);//错误,类方法中不能使用this/super

        hi();
        //say();//静态方法不能访问非静态方法
    }
    //普通成员方法,既可以访问非静态成员,也可以访问静态成员。
    public void ok(){
        //非静态成员
        System.out.println(n1);
        say();
        //静态成员
        System.out.println(n2);
        hello();
    }
}

5. 类变量和类方法练习  381

5.1 判断输出 381

类方法

代码在com.stulzl.staticexercise.包中

StaticExercise01

package com.stulzl.staticexercise;

//类变量和类方法练习 381
public class StaticExercise01 {
    public static void main(String[] args) {
        new Test().count();//9
        new Test().count();//10
        System.out.println(Test.count);//11
    }
}
class Test{
    static int count = 9;
    public void count(){
        System.out.println("count="+count++);
    }
}

类方法

5.2看看输出什么? 381

代码在com.stulzl.staticexercise.包中

StaticExercise02

package com.stulzl.staticexercise;

public class StaticExercise02 {
    public static void main(String[] args) {
        System.out.println("Number of total is " +Person.getTotalPerson()); //0
        Person p1 = new Person();
        System.out.println( "Number of total is "+ Person.getTotalPerson()); //1
    }
}
class Person {
    private int id;
    private static int total = 0;

    public static int getTotalPerson() {
        //id++;//错误, 注销
        return total;
    }
    public Person() {//构造器
        total++; //total = 1
        id = total;//id = 1
    }
}

类方法

5.3 判断输出 381

代码在StaticExercise03包中

StaticExercise03

package com.stulzl.staticexercise;

public class StaticExercise03 {
    public static void main(String[] args) {
        Person1.setTotalPerson(3);
        new Person1(); //最后 total 的值就是 4
        Person1.m();//4
    }
}
class Person1 {
    private int id;
    private static int total2 = 0;
    public static void setTotalPerson(int total){
    // this.total = total;//错误,因为在 static 方法中,不可以使用 this 关键字
       Person1.total2 = total;
    }
    public Person1() {//构造器
        total2++;
        id = total2;
    }
    //编写一个方法输出total2
    public static void m(){
        System.out.println("total2="+total2);
    }
}

类方法

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月12日 0

暂无评论

TEZNKK3IfmPf