java学习之类方法

目录

一、基本介绍

二、类方法的调用

三、类方法的应用实例

代码

 内存分析

运行结果

四、类方法的经典使用场景

五、类方法使用细节

第一条

第二条

第三条

第四条

第五条

第六条

六、练习

第一题

考察点

分析

结果

 第二题

 代码

考察点

结果

 第三题


类方法

一、基本介绍

类方法也叫静态方法

形式如下:访问修饰符 static 数据返回类型 方法名()

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

二、类方法的调用

1)类名.方法名

2)对象名.方法名

注意:满足访问修饰符权限和规则

三、类方法的应用实例

计算学生交的学费总额

代码

package com.hspedu.static_;

public class StaticMethod {
    public static void main(String[] args) {
    //创建2个学生对象,交学费
        Stu stu1 = new Stu("Tom");
        //stu1.payFee(2000);
        Stu.payFee(2000);//静态变量fee的值0——>2000

        Stu stu2 = new Stu("Jack");
        //stu2.payFee(3000);
        Stu.payFee(3000);//静态变量fee的值2000——>5000

        Stu.showFee();//输出fee,即5000
        
    }
}

class Stu{
    private String name;//普通成员
    //定义一个静态变量,来累积学生的学费
    private static double fee = 0;

    public Stu(String name) {
        this.name = name;
    }
    //当方法使用了static修饰后,该方法就是一个静态方法
    //静态方法可以访问静态属性/变量
    public static void payFee(double fee){
        //统计总学费,将传进来的参数相加,累积到静态变量中
        //Stu.fee = Stu.fee + fee;
        Stu.fee += fee;
        //静态方法只能访问静态属性,不能访问非静态属性
    }
    public static void showFee(){

        System.out.println("总学费有:" + Stu.fee);

    }
}

 内存分析

运行结果

四、类方法的经典使用场景

如果希望不创建实例,也可以调用某个方法(即当做工具来使用),这时,把方法做成静态方法非常合适,比如:Math类,Arrays类

package com.hspedu.static_;

public class StaticMethod {
    public static void main(String[] args) {
   
        //如果希望不创建实例,也可以调用某个方法(即当做工具来使用)
        //这时,把方法做成静态方法非常合适,比如:
        System.out.println(Math.sqrt(9));//开平方
        System.out.println(MyTools.calSum(3.4,7.8));
        System.out.println(MyTools.division(5,0));
    }
}
//开发自己的工具类时,可以将方法做成静态的,方便调用
class MyTools{
    //求和
    public static double calSum(double n1, double n2){
        return n1 + n2;
    }
    public static Double division(double n1, double n2){
        if(n2 == 0){
            return null;
        }
        return n1 / n2;
    }

}

五、类方法使用细节

第一条

类方法和普通方法都是随着类的加载而加载,将结构信息存储在方法区:

类方法中无this的参数

普通方法中隐含着this的参数

第二条

类方法可以通过类名调用,也可以通过对象名调用。

第三条

普通方法和对象有关,需要通过对象名调用(创建对象然后调用),比如对象名.方法名(参数),不能通过类名调用

第四条

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

第五条

类方法(静态方法)中 只能访问 静态变量或静态方法。

第六条

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

package com.hspedu.static_;

public class StaticMethodDetail {
    public static void main(String[] args) {
        //非静态方法只能通过对象名.方法名()访问
        D d = new D();
        d.say();
        //D.say();//错误

        d.hi();
        D.hi();
    }
}
class E {
    public int n2 = 20;

}
class D extends E{
    private int n1 = 10;
    private static int n3 = 30;
    public void say(){//非静态方法
        System.out.println(this.n1);
        System.out.println(super.n2);
    }
    public static void hi(){//静态方法
        //类方法中不能使用和对象相关的关键字,
        // 比如this和super
        //System.out.println(this.n1);
        //System.out.println(super.n2);

    }

    //静态方法只能访问静态成员
    public static void hello(){
        System.out.println(n3);
        System.out.println(D.n3);
        //System.out.println(this.n3);//错误,不能使用tihs和super
        //System.out.println(n1);//错误,不能访问非静态变量
        //say();//错误,不能访问非静态方法
        hi();
        D.hi();
    }
    //普通成员方法既能访问非静态成员,也能访问静态成员
    public void f1(){
        //非静态
        System.out.println(n1);
        say();

        //静态
        System.out.println(n3);
        hi();
        hello();
    }
}

总结:静态方法只能访问静态成员,非静态方法可以访问静态成员和非静态成员

六、练习

第一题

说出以下代码的输出结果

package com.hspedu.static_;

public class Test {
    static int count = 9;//类变量
    public void count() {
        System.out.println("count=" + (count++));
    }
    public static void main(String args[]) {
        new Test().count();
        new Test().count();
        System.out.println(Test.count);
    }
}

考察点

类变量是所在类的所有对象实例共享的,后++是先运算再自增

分析

结果

 第二题

 代码

package com.hspedu.static_;

public class TestPerson {
    public static void main(String[] args) {
        System.out.println("Number of total is " + Person.getTotalPerson());//0
        Person p1 = new Person();//创建新对象,调用构造器,total = 1; id = 1;
        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;
    }
}

考察点

静态方法只能访问静态成员,非静态方法可以访问静态成员和非静态成员

结果

 第三题

package com.hspedu.static_;

public class TestPerson02 {
    public static void main(String[] args) {
        Person02.setTotalPerson(3);
        new Person02();
    }

}

class Person02 {

    private int id;
    private static int total = 0;

    public static void setTotalPerson(int total){
        this.total = total;
        Person02.total = total;
    }

    public Person02(){
        total++;
        id=total;
    }
}


考察点同题2

解答

package com.hspedu.static_;

public class TestPerson02 {
    public static void main(String[] args) {
        Person02.setTotalPerson(3);//调用类方法,类变量total=3
        new Person02();//创建对象,调用构造器,执行total++,total=4,id=4
        Person02.showTotal();

    }

}

class Person02 {

    private int id;
    private static int total = 0;

    public static void setTotalPerson(int total){
        //this.total = total;//静态方法中不能适用this和super等对象有关的关键字
        Person02.total = total;
    }

    public Person02(){
        total++;
        id=total;
    }
    //创建静态方法,输出静态变量total的值
    public static void showTotal(){
        System.out.println("total的值=" + total);
    }
}


运行结果

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值