类方法与类变量(静态方法与静态变量)

2021-11-09  本文已影响0人  哈迪斯Java
image.png

package oop.Demon11;

public class Students {
public static void main(String[] args) {
//创建两个学生对象
Stu tom = new Stu("tom");
tom.payFee(10000);
Stu jack = new Stu("jack");
jack.payFee(1200);
Stu.showFee();
System.out.println(myTools.method(23,12));
}
}
//开发自己的工具类时,可以将方法做成静态的,方便调用
class myTools{
public static double method(double n1,double n2){
return n1+n2;
}

}
class Stu{
private String name;
private static double fee = 0;

public Stu(String name) {
    this.name = name;
}
public static void payFee(double fee){
    Stu.fee+=fee;
}
public static void showFee(){
    System.out.println("总学费为:"+Stu.fee);
}

}

image.png
上一篇下一篇

猜你喜欢

热点阅读