1 关键字 static
类中的实例变量,非 static 的成员变量
public class Circle_Test {
public static void main(String[] args) {
Circle c1 = new Circle();
c1.radius = 3;
System.out.println("半径为:" + c1.radius + "\t面积为:" + c1.findArea());
// 半径为:3.0 面积为:28.274333882308138
Circle c2 = new Circle();
c2.radius = 4;
System.out.println("半径为:" + c2.radius + "\t面积为:" + c2.findArea());
// 半径为:4.0 面积为:50.26548245743669
}
}
class Circle {
public double radius; // 圆的半径
public double findArea(){
return Math.PI * radius * radius;
}
}
在上述代码中,Circle 类中的变量 radius 是一个实例变量,它属于类的每一个对象,c1 中的 radius 变化不会影响到 c2 中的 radius ;
如果想让一个成员变量,被类中所有实例共用,就用 static 进行修饰即可,称为类变量(或类属性)
1.1 static 关键字
使用范围:
在 Java 类中,可用 static 修饰属性、方法、代码块、内部类;
被修饰后的成员具有以下特征:
- 成员随着类的加载而加载;
- 优先于对象存在;
- 被修饰的成员,被所有对象共享;
- 访问权限允许的情况下,可不创建对象,直接使用类进行调用;
1.2 静态变量
1.2.1 语法格式
使用 static 修饰的成员变量就是静态变量(或类变量、类属性)
修饰符 class 类名 {
修饰符 static 数据类型 变量名;
}
1.2.2 静态变量的特点
- 静态变量的默认值规则和实例变量一样;
- 静态变量值是所有对象共享;
- 静态变量在本类中,可以在任意方法、代码块、构造器中直接使用;
- 如果权限修饰符允许,在其他类中可以通过 " 类名.静态变量 " ,直接访问,也可以通过 " 对象.静态变量 " 的方式访问;(在这里更推荐 " 类名.静态变量 " 的方式)
- 静态变量的 get/set 方法也是静态的,当局部变量与静态变量重名时,使用 " 类名.静态变量 " 进行区分;
public class Test_01 {
public static void main(String[] args) {
//Country.setNation("中国"); // 类直接调用setNation方法,设置共用的属性
Country.nation = "中国"; // 类直接调用属性,设置共用的属性
Country c1 = new Country("张三",21); // 新建两个实例
Country c2 = new Country("李四",12);
System.out.println(c1.toString()); // Country{id=1001, name='张三', age=21, nation=中国}
System.out.println(c2.toString()); // Country{id=1002, name='李四', age=12, nation=中国}
}
}
class Country{
// 实例变量
private int id;
private String name; // 姓名
private int age; // 年龄
// 类变量
private static int init = 1001; // 私有静态变量,自动递增
static String nation; // 国籍
public Country(){
this.id = init;
init++;
}
public Country(String name, int age){
this.id = init;
init++;
this.name = name;
this.age = age;
}
public static void setNation(String nation) {
Country.nation = nation;
}
@Override
public String toString() {
return "Country{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", nation=" + nation +
'}';
}
}
1.3 静态方法
1.3.1 语法格式
用 static 修饰的成员方法,就是静态方法;
修饰符 class 类{
修饰符 static 返回值类型(形参列表) {
方法体
}
}
1.3.2 静态方法的特点
- 静态方法在本类中的任意方法、代码块、构造器中都可以直接被调用;
- 在权限允许的情况下,静态方法在其他类当中也可以通过 " 类名.静态方法 " 的方式调用。也可以通过 " 对象.静态方法 " 的方式调用(更推荐 " 类名.静态方法 " )
- 在 static 方法内部,只能访问类的 static 修饰的属性和方法,不能访问类的非static 的结构;
- 静态方法可以被子类继承,但不能被子类重写;
- 静态方法的调用只看编译时的类型;
- 由于不需要实例就可以访问 static 方法,因此 static 方法内部不能有 this ,也不能有 super ,如果有重名情况,只需要 " 类名. " 进行区别就可;
public class Father {
public static void Print(){
System.out.println("Father.Print");
fun(); // 调用本类中的静态方法 fun();
}
public static void fun(){
System.out.println("Father.fun");
}
public static void main(String[] args) {
Print();
// Father.Print
// Father.fun
}
}
--------------------------------------------
public class Son extends Father{
public static void fun(){
System.out.println("Son.fun");
}
}
--------------------------------------------
public class TestFather {
public static void main(String[] args) {
Father.Print(); // 调用父类中静态方法
// Father.Print
// Father.fun
Son.Print(); // 继承父类中的静态方法
// Father.Print
// Father.fun
System.out.println(" -------------------------- ");
Father f1 = new Son(); // 多态
f1.Print(); // 执行 Father 中的 Print();
// Father.Print
// Father.fun
f1.fun(); // Father.fun
}
}
练习:
编写一个类实现银行账户的概念,包含的属性有 " 账号 "、" 密码 "、" 存款余额 "、" 利率 "、" 最下余额 ",定义封装这些属性的方法,账号要自动生成;
编写主类,使用银行账户类,输入、输出3个储户的上述信息;
考虑:哪些属性可以设计成 static 属性;
public class Bank {
private int id; // 账号
private static double initialBalance; // 利率 -静态变量-
private static double minBalance; // 最小余额 -静态变量-
private static int init = 1001;
private String password; // 密码
private double balance; // 存款余额
public Bank() {
this.initialBalance = 0.0123;
this.minBalance = 0.0;
this.password = "000000";
this.id = init;
init++;
}
public Bank(String password, double balance) {
this.password = password;
this.balance = balance;
this.initialBalance = 0.0123;
this.minBalance = 0.0;
this.id = init;
init++;
}
public static void setInitialBalance(double initialBalance) {
Bank.initialBalance = initialBalance;
}
public static void setMinBalance(double minBalance) {
Bank.minBalance = minBalance;
}
@Override
public String toString() {
return "Bank{" +
"id = " + id + '\t' +
"password = " + password + '\t' +
"balance = " + balance + '\t' +
"minBalance = " + minBalance + '\t' +
"initialBalance = " + initialBalance + '\t' +
'}';
}
}
-----------------------------------------------------
public class TestBank {
public static void main(String[] args) {
Bank b1 = new Bank("123456",2000);
Bank b2 = new Bank("234567",5000);
Bank b3 = new Bank("345678",3000);
System.out.println(b1);
System.out.println(b2);
System.out.println(b3);
/*
Bank{id = 1001 password = 123456 balance = 2000.0 minBalance = 0.0 initialBalance = 0.0123 }
Bank{id = 1002 password = 234567 balance = 5000.0 minBalance = 0.0 initialBalance = 0.0123 }
Bank{id = 1003 password = 345678 balance = 3000.0 minBalance = 0.0 initialBalance = 0.0123 }
*/
System.out.println(" -------------------------- ");
Bank.setInitialBalance(0.04);
Bank.setMinBalance(200);
System.out.println(b1);
System.out.println(b2);
System.out.println(b3);
/*
Bank{id = 1001 password = 123456 balance = 2000.0 minBalance = 200.0 initialBalance = 0.04 }
Bank{id = 1002 password = 234567 balance = 5000.0 minBalance = 200.0 initialBalance = 0.04 }
Bank{id = 1003 password = 345678 balance = 3000.0 minBalance = 200.0 initialBalance = 0.04 }
*/
}
}

932

被折叠的 条评论
为什么被折叠?



