static关键字使用
- 1.static:静态的
- 2.static可以用来修饰:属性、方法、代码块、内部类
- 3.使用static修饰属性:静态变量(类变量)
3.1属性,按是否使用static修饰,又分为:静态属性、非静态属性(实例变量)
实例变量:创建了类的多个对象,每个对象都独立的拥有一套类中的非静态属性。当修改其中一个对象中的非静态属性时,不会导致其他对象中同样的属性值的修改。
静态变量:创建了类的多个对象,多个对象共享同一个静态变量。当通过某一个对象修改静态变量时,会导致其他对象调用此静态变量时,是修改过的。
3.2static修饰属性的其他说明:
①静态变量随着类的加载而加载。可以通过“类.静态变量”的方式进行调用
②静态变量的加载要早于对象的创建。
③由于类只会加载一次,则静态变量在内存中也只会存在一份:存在方法区的静态域中。
④ 类变量 实例变量
类 1 0
对象 1 1
3.3静态属性举例:System.out; Math.PI;
-
4.使用static修饰方法:
①随着类的加载而加载,可以通过“类.静态方法”的方式进行调用
② 静态方法 非静态方法
类 1 0
对象 1 1
③静态方法中,只能调用静态的方法和属性
非静态方法中,既可以调用非静态的方法和属性,也可以调用静态的方法和属性 -
5.static注意点:
5.1在静态方法中,不能调用this关键字、super关键字
5.2关于静态属性和静态方法的使用,从生命周期理解。 -
6.开发中,如何确定属性或是否要声明为static的?
>属性可以被多个对象所共享的,不会随着对象的不同而不同。
>操作静态属性的方法,通常设置为static。工具类中的方法,习惯上声明为static。比如:Math、Arrays
public class StaticTest {
public static void main(String[] args) {
Chinese.nation = "中国";
//Chinese.name = "fsd";//编译不通过
System.out.println(Chinese.nation);
Chinese c1 = new Chinese();
c1.name = "姚明";
c1.age = 40;
c1.nation = "chn";
Chinese c2 = new Chinese();
c2.name = "马龙";
c2.age = 30;
c2.nation = "CHINA";
System.out.println(c1.nation);
System.out.println(c2.nation);
c1.eat();
Chinese.show();
}
}
class Chinese{
String name;
int age;
static String nation;
public void eat(){
System.out.println("中国人吃中餐");
walk();
info();
this.info();
}
public static void show(){
System.out.println("我是一个中国人");
// eat();
// name = "sf";
walk();//可以调用静态的结构
}
public void info(){
System.out.println("name: " + name + ",age:" + age);
}
public static void walk(){
}
}
实例一
public class CircleTest {
public static void main(String[] args) {
Circle c1 = new Circle();
Circle c2 = new Circle();
Circle c3 = new Circle(3.4);
System.out.println("c1的id:" + c1.getId());
System.out.println("c2的id:" + c2.getId());
System.out.println("c2的id:" + c3.getId());
System.out.println("创建的园的个数为:" + Circle.getTotal());
}
}
class Circle{
private double radius;
private int id;
public Circle(){
id = init++;
total++;
}
public Circle(double radius){
this();
//或者
// id = init++;
// total++;
this.radius = radius;
}
private static int total;//记录创建的园的个数
private static int init = 1001;
// private int init = 1001;
public double findArea(){
return 3.14 * radius * radius;
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
public int getId() {
return id;
}
public static int getTotal() {
return total;
}
public static void setTotal(int total) {
Circle.total = total;
}
}
练习
- 编写一个类实现银行账户的概念,包含的属性有“账号”、“密码”、“存款余额”、“利率”、“最小余额”,
- 定义封装这些属性的方法。账号要自动生成。
- 编写主类,使用银行账户类,输入、输出3个储户的上述信息。
- 考虑:那些属性可以设置成static
public class Account {
private int id;
private String pwd = "111111";
private double balance;
private static double interestRate;
private static double minMoney = 1.0;
private static int init = 1001;//用于自动生成id
public Account(){
id = init++;
}
public Account(String pwd, double balance){
id = init++;
this.pwd = pwd;
this.balance = balance;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public static double getInterestRate() {
return interestRate;
}
public static void setInterestRate(double interestRate) {
Account.interestRate = interestRate;
}
public static double getMinMoney() {
return minMoney;
}
public static void setMinMoney(double minMoney) {
Account.minMoney = minMoney;
}
@Override
public String toString() {
return "Account{" +
"id=" + id +
", pwd='" + pwd + '\'' +
", balance=" + balance +
'}';
}
}
public class AccountTest {
public static void main(String[] args) {
Account acct1 = new Account();
Account acct2 = new Account("123456", 2000);
Account.setInterestRate(0.012);
Account.setMinMoney(100);
System.out.println(acct1);
System.out.println(acct2);
System.out.println(acct1.getInterestRate());
System.out.println(acct1.getMinMoney());
}
}