类变量和类方法
10.1.1 类变量-提出问题
有一群小孩在玩堆雪人,不时有新的小孩加入,请问如何知道现在共有多少人在玩?,编写程序解决。
10.1.2-3 类变量-解决问题
想法:设计一个 int count 表示总人数,我们在创建一个小孩时,就把 count 加 1,并且 count 是所有对象共享的 就 ok 了!,我们使用类变量来解决 ChildGame.java 改进。
代码:
package SeniorJava;
public class ChildGame
{
public static void main(String[] args) {
int count = 0;
Child child01 = new Child("小白");
child01.join();
child01.count++;
Child child02 = new Child("小红");
child02.join();
child02.count++;
Child child03 = new Child("小紫");
child03.join();
child03.count++;
//类变量可以用Child类直接访问!
System.out.println("共有"+Child.count+"名同学加入到游戏中来!");
//举一反三
System.out.println("child01.count="+child01.count);//3
System.out.println("child02.count="+child02.count);//3
System.out.println("child03.count="+child03.count);//3
}
}
class Child
{
private String name;
//定义一个变量count,是一个类变量(静态变量),是用static(静态)来修饰
//该变量最大的特点就是会被 Child 类的所有的对象实例共享
public static int count = 0;
public Child(String name) {
this.name = name;
}
public void join()
{
System.out.println(name+"加入了游戏!");
}
}
Child.count里相当于一个共同的空间,无论是child01.count还是什么,最后的输出都是同一个数。因为共享
10.1.4 类变量内存布局
10.1.5 什么是类变量
10.1.6 如何定义类变量
10.1.7 如何访问类变量
代码如下:
package SeniorJava;
public class VisitStatic
{
public static void main(String[] args) {
//通过:类名.类变量名
//说明:类变量是随着类的加载而创建,所以即使没有创建对象实例也可以访问
System.out.println(A.name);
A a = new A();
//通过:对象名.类变量名
System.out.println(a.name);
}
}
class A
{
public static String name = "皮卡丘奇遇冒险";
//如果私有的化只能在本类使用
//类变量的访问必须遵守相关访问权限
//private static String name = "皮卡丘奇遇冒险";
}
10.1.8 类变量使用注意事项和细节讨论
代码如下:
package SeniorJava;
public class StaticDetail {
public static void main(String[] args) {
B b = new B();
//System.out.println(B.n1);报错
System.out.println(B.n2);
//静态变量是类加载的时候,就创建了,所以我们没有创建对象实例
//也可以通过类名.类变量名来访问
System.out.println(C.address);
}
}
class B {
public int n1 = 100;
public static int n2 = 200;
}
class C {
public static String address = "北京";
}
10.1.9 类方法基本介绍
10.1.10 类方法的调用
前提是:满足访问修饰符的访问权限和范围
10.1.11 类方法应用案例
package SeniorJava;
public class StaticMethod
{
public static void main(String[] args)
{
//创建两个学生对象来交学费
Stu tom = new Stu("tom");
tom.payFee(100);//使用 对象名.类方法名 进行调用
//与 Stu.payFee(100); 是一样的执行方法 ,不过是 类名.类方法名(注意权限要公开)
Stu kary = new Stu("kary");
kary.payFee(300);
Stu mary = new Stu("mary");
mary.payFee(200);
//输出总学费
Stu.showFee();
}
}
class Stu
{
private String name;//普通成员
//定义一个静态变量,来累积学生学费
private static double fee = 0;
public Stu(String name) {
this.name = name;
}
//说明
//1. 当方法使用了 static 修饰后,该方法就是静态方法
//2. 静态方法就可以访问静态属性/变量
//3. 不能用this,不知是该访问静态变量还是哪个
public static void payFee(double fee)
{
Stu.fee += fee;
}
public static void showFee()
{
System.out.println("总学费:"+Stu.fee);
}
}
10.1.12 类方法经典的使用场景
10.1.13 类方法使用注意事项和细节讨论
package SeniorJava;
public class StaticMethodDetail {
public static void main(String[] args) {
D.hi();//ok
//非静态方法,不能通过类名调用
//D.say();, 错误,需要先创建对象,再调用
new D().say();//可以
}
}
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);不能使用
hi();//OK
//say();//错误。在静态方法中,只能访问静态方法,不能访问非静态方法
}
//普通成员方法,既可以访问非静态成员,也可以访问静态成员
//小结: 非静态方法可以访问静态成员和非静态成员【静只能访问静,非静可以全都访】
public void ok() {
//非静态成员
System.out.println(n1);
say();
//静态成员
System.out.println(n2);
hello();
}
}
10.1.14 课堂练习
10.1.15 小结
(1) 静态方法,只能访问静态成员
(2) 非静态方法,可以访问所有的成员
(3) 在编写代码时,仍然要遵守访问权限规则