高级类特性----static关键字

来源:互联网 发布:常州海智软件 编辑:程序博客网 时间:2024/06/05 19:01
static 关键字


当我们编写一个类时,其实就是在描述其对象的属性和行为,而并没有产生实质上的对象,只有通过new关键字才会产生出对象,这时系统才会分配内存空间给对象,其方法才可以供外部调用
我们有时候希望无论是否产生了对象或无论产生了多少对象的情况下,某些特定的数据在内存空间里只有一份,例如所有的中国人都有个国家名称,每一个中国人都共享这个国家名称,不必在每一个中国人的实例对象中都单独分配一个用于代表国家名称的变量。

class Circle {
  private double radius;
  public Circle(double radius) {
    this.radius=radius;
  }
  public double findArea() {
    return Math.PI*radius*radius;
  }
}

创建两个Circle对象
  Circle c1=new Circle(2.0); //c1.radius=2.0
  Circle c2=new Circle(3.0); //c2.radius=3.0
Circle类中的变量radius是一个实例变量(instance variable),它属于类的每一个对象,不能被同一个类的不同对象所共享。


上例中c1的radius独立于c2的radius,存储在不同的空间。c1中的radius变化不会影响c2的radius,反之亦然。

如果想让一个类的所有实例共享数据,请用类变量

在Java类中声明变量方法内部类时,可使用关键字static做为修饰符。

static标记的变量或方法由整个类(所有实例)共享,如访问控制权限允许,可不必创建该类对象而直接用类名加‘.’调用

static成员也称类成员或静态成员,如:类变量、类方法、静态方法等。

 

类变量(class Variable)

类变量(类属性)由该类的所有实例共享

public class Person {
  private int id;
  public static inttotal = 0;
  public Person() {
    total++;
    id = total;
  }
}

类属性类似于全局变量


类属性应用举例

 1 class Person { 2   private int id; 3   public static int total = 0; 4   public Person() { 5      total++; 6        id = total; 7     } 8     public static void main(String args[]){ 9       Person Tom=new Person()10     Tom.id=0;11     total=100; // 不用创建对象就可以访问静态成员12     }13  }14 15 public class OtherClass {16   public static void main(String args[]) {17      Person.total = 100;  // 不用创建对象就可以访问静态成员18        //访问方式:类名.类属性类名.类方法19        System.out.println(Person.total);20        Person c = new Person(); 21        System.out.println(c.total);    //输出10122     }23 }

 

类方法(class Method)

没有对象的实例时,可以用类名.方法名()的形式访问由static标记的类方法

 1  class Person { 2        private int id; 3        private static int total = 0; 4        public static int getTotalPerson() {  5     return total; 6        } 7        public Person() { 8              total++; 9      id = total;10        }11 }12 public class TestPerson {13         public static void main(String[] args) {14      System.out.println("Number of total is " +Person.getTotalPerson());15 16     //没有创建对象也可以访问静态方法17      Person p1 = new Person();18          System.out.println( "Number of total is "+ Person.getTotalPerson());19         }20 }

 

在static方法内部只能访问类的static属性,不能访问类的非static属性

 1 class Person { 2     private int id; 3     private static int total = 0; 4     public static int getTotalPerson() {  5       id++;    //非法 6       return total; 7     } 8     public Person() { 9       total++;10       id = total;11     }12 }

 

因为不需要实例就可以访问static方法,因此static方法内部不能有this(也不能有super)

 1 class Person { 2   private int id; 3    private static int total = 0; 4    public static void setTotalPerson(int total){ 5        this.total=total;    //非法,在static方法中不能有this,也不能有super 6    } 7    public Person() { 8        total++; 9        id = total;10    }11 }12 public class TestPerson {13     public static void main(String[] args) {14       Person.setTotalPerson();15     }16 }

 

在静态方法里只能直接调用同类中其它的静态成员(包括变量和方法),而不能直接访问类中的非静态成员。这是因为,对于非静态的方法和变量,需要先创建类的实例对象后才可使用,而静态方法在使用前不用创建任何对象

静态方法不能以任何方式引用this和super关键字。与上面的道理一样,因为静态方法在使用前不用创建任何实例对象,当静态方法被调用时,this所引用的对象根本就没有产生。

main() 方法是静态的,因此JVM在执行main方法时不创建main方法所在的类的实例对象,因而在main()方法中,我们不能直接访问该类中的非静态成员,必须创建该类的一个实例对象后,才能通过这个对象去访问类中的非静态成员,这种情况,我们在以后的例子中会多次碰到。

 

类属性、类方法的设计思想

类属性作为该类各个对象之间共享的变量。在设计类时,分析哪些类属性不因对象的不同而改变,将这些属性设置为类属性。相应的方法设置为类方法

如果方法与调用者无关,则这样的方法通常被声明为类方法,由于不需要创建对象就可以调用类方法,从而简化了方法的调用


练习

1、编写一个类,实现银行账户的概念,包含的属性有“帐号”、“密码”、“存款余额”、“利率”、“最小余额”,定义封装这些属性的方法。账号要自动生成。
编写主类,使用银行账户类,输入、输出3个储户的上述信息。
考虑:哪些属性可以设计成static属性。

 1 /** 2  * 编写一个类,实现银行账户的概念,包含的属性有“帐号”、“密码”、“存款余额”、“利率”、“最小余额”,定义封装这些 3  * 属性的方法。账号要自动生成。 4  */ 5 public class Account { 6  7     //初始化的 id 8     private static int initId = 1000; 9     10     //账号11     private String id;12     13     //密码14     private String password;15     16     //余额17     private int balance;18     19     //利率20     private static double rate;21     22     //最小余额23     private static int minBalance;24 25     public Account(String password, int balance) {26         this.id = "" + (initId++); 27         this.password = password;28         this.balance = balance;29     }30 31     public String getId() {32         return id;33     }34 35     public void setId(String id) {36         this.id = id;37     }38 39     public String getPassword() {40         return password;41     }42 43     public void setPassword(String password) {44         this.password = password;45     }46 47     public int getBalance() {48         return balance;49     }50 51     public void setBalance(int balance) {52         this.balance = balance;53     }54 55     public static double getRate() {56         return rate;57     }58 59     public static void setRate(double rate) {60         Account.rate = rate;61     }62 63     public static int getMinBalance() {64         return minBalance;65     }66 67     public static void setMinBalance(int minBalance) {68         Account.minBalance = minBalance;69     }70 71     @Override72     public String toString() {73         return "Account [id=" + id + ", password=" + password + ", balance="74                 + balance + ", rate=" + rate + ", minBalance=" + minBalance + "]";75     }76 }

 

 1 /** 2  * 编写主类,使用银行账户类,输入、输出3个储户的上述信息。 3  */ 4 public class TestBank { 5     public static void main(String[] args) { 6          7         //统一设置 rate 和 minBalance 8         Account.setMinBalance(100); 9         Account.setRate(0.01);10         11         Account acc1 = new Account("1234", 100);12         Account acc2 = new Account("1235", 200);13         Account acc3 = new Account("1236", 300);14         15         System.out.println(acc1);16         System.out.println(acc2);17         System.out.println(acc3);18     }19 }

 

静态初始化

一个类中可以使用不包含在任何方法体中的静态代码块(static block ),当类被载入时,静态代码块被执行,且只被执行一次,静态块经常用来进行类属性的初始化
static块通常用于初始化static (类)属性
class Person {
  public static int total;
  static {
    total = 100;//为total赋初值
  }
  …… //其它属性或方法声明
}

静态初始化举例
class Person {
  public static int total;
  static {
    total = 100;
    System.out.println("in static block!");
  }
}

public class Test {
  public static void main(String[] args) {
    System.out.println("total = "+ Person.total);
    System.out.println("total = "+ Person.total);
  }
}

输出:
in static block
total=100
total=100

 


 

 1 public class Chinese { 2      3     String name; 4     int age; 5      6     static String country; 7      8     //静态代码块: 使用 static 修饰的代码块 9     //在类被加载时执行一次. 且执行一次. 可以在静态代码块中对静态成员变量进行初始化. 10     static{11         System.out.println("静态代码块");12     }13     14     //非静态代码块: 先于构造器执行, 没创建一个对象都会执行一次. 15     {16         System.out.println("非静态代码块");17     }18     19     //对非静态成员进行初始化. 20     public Chinese(String name, int age) {21         super();22         this.name = name;23         this.age = age;24         25         country = "";26     }27     28     public Chinese() {29         System.out.println("构造器");30     }31 32     String getInfo(){33         return "name: " + name;34     }35     36     static void test(){37         System.out.println("test...");38         System.out.println("country: " + country); 39         40 //        System.out.println(name);41     }42 }

 

 1 /** 2  * static: 静态的.  3  * 1. 若需要一个类的多个对象共享一个变量, 则该变量需要使用 static 修饰.  4  * 2. 因为 static 修饰的变量为类的所有的实例所共享, 所以 static 成员不属于某个类的实例, 而属于整个类. 5  *    所以在访问权限允许的情况下, 可以使用 "类名." 直接访问静态成员(成员包括属性和方法) 6  * 3. 注意: 在静态方法里只能直接调用同类中其它的静态成员(包括变量和方法),而不能直接访问类中的非静态成员。 7  *         这是因为,对于非静态的方法和变量,需要先创建类的实例对象后才可使用,而静态方法在使用前不用创建任何对象。 8  * 4. 同 3 的道理: 静态方法不能以任何方式引用this和super关键字 9  * 5. 非静态方法中可以直接来访问类的静态成员.10  * 6. main() 方法是静态的,因此JVM在执行main方法时不创建main方法所在的类的实例对象11  * 12  * 7. 静态初始化指对类的静态属性进行初始化. 13  * 7.1 不应该在构造器中对静态成员进行初始化: 因为静态成员不因类的实例而改变. 14  * 7.215  * //非静态代码块: 先于构造器执行, 每创建一个对象都会执行一次. 16  *    {17  *        System.out.println("非静态代码块");18  *    }19  * 7.3 20  * //静态代码块: 使用 static 修饰的代码块21  *    //在类被加载时执行一次. 且执行一次. 可以在静态代码块中对静态成员变量进行初始化. 22  *  //先于非静态代码块和构造器执行. 23  *    static{24  *        System.out.println("静态代码块");25  *    }35  */36 public class TestStatic {37     public static void main(String[] args) {45         57         Chinese.country = "中国";58         Chinese.test();59         60         Chinese p1 = new Chinese();61         p1.name = "Tom";62         p1.age = 12;63         p1.country = "China";64         65         Chinese p2 = new Chinese();66         p2.name = "Jerry";67         p2.age = 13;68 //        p2.country = "中国";69         70         System.out.println(p1.name);71         System.out.println(p1.country);72 73         System.out.println(p2.name); 74         System.out.println(p2.country); 75         76         System.out.println(p1.getInfo());77     }78 }

 

单例模式

设计模式是在大量的实践中总结和理论化之后优选的代码结构、编程风格、以及解决问题的思考方式。设计模式就想是经典的棋谱,不同的棋局,我们用不同的棋谱,免得我们自己再去思考和摸索。

所谓类的单态设计模式,就是采取一定的方法保证在整个的软件系统中,对某个类只能存在一个对象实例,并且该类只提供一个取得其对象实例的方法

如果我们要让类在一个虚拟机中只能产生一个对象,我们首先必须将类的构造方法的访问权限设置为private,这样,就不能用new 操作符在类的外部产生类的对象了,但在类内部仍可以产生该类的对象。因为在类的外部开始还无法得到类的对象,只能调用该类的某个静态方法以返回类内部创建的对象,静态方法只能访问类中的静态成员变量,所以,指向类内部产生的该类对象的变量也必须定义成静态的。

 

单例 Singleton 设计模板

class Single {
  private static Single onlyone = new Single();//私有的,只能在类的内部访问
  private String name;
  public static Single getSingle() { //getSingle()为static,不用创建对象即可访问
    return onlyone;
  }
  private Single() {} //private的构造器,不能在类的外部创建该类的对象
}

public class TestSingle{
  public static void main(String args[]) {
    Single s1 = Single.getSingle(); //访问静态方法
    Single s2 = Single.getSingle();
    if (s1==s2){
      System.out.println("s1 is equals to s2!");
    }
  }
}


 /**

* 设计为单子模式
*/
public class SingleInstance {

  //构造器私有化
  private SingleInstance() {}

  //在类的内部创建实例
  private static SingleInstance instance = new SingleInstance();

  //提供 get 方法
  public static SingleInstance getInstance() {
    return instance;
  }
}

/*

* 所谓类的单态设计模式,就是采取一定的方法保证在整个的软件系统中,对某个类只能存在一个对象实例
*   不能在类的外部通过 new 关键字的方式创建新的实例: 构造器私有化.
*   在类的内部创建实例
*   为了让类的外部能够访问到类的内部创建的实例, 该实例必须使用 static 修饰.
*   不能允许在类的外部修改内部创建的实例的引用. SingleInstance.instance = null;
* 需要把该属性用 private 修饰
*   为了让外部进行读, 添加对应的 get 方法.
*/

// SingleInstance s1 = new SingleInstance();
// SingleInstance s2 = new SingleInstance();

SingleInstance s1 = SingleInstance.getInstance();
// SingleInstance.getInstance() = null;

SingleInstance s2 = SingleInstance.getInstance();

System.out.println(s1 == s2);

 

理解main方法的语法

由于Java虚拟机需要调用类的main()方法,所以该方法的访问权限必须是public,又因为java虚拟机在执行main()方法时不必创建对象,所以该方法必须是static的,该方法接收一个String类型的数组参数,该数组中保存执行java命令时传递给所运行的类的参数。

命令行参数用法举例
public class CommandPara {
  public static void main(String[] args) {
    for ( int i = 0; i < args.length; i++ ) {
      System.out.println("args[" + i + "] = " + args[i]);
    }
  }
}

//运行程序CommandPara.java
java CommandPara lisa "bily" "Mr Brown"
//输出结果:

args[0] = lisa
args[1] = bily
args[2] = Mr Brown

 


 

if(args != null){

  if(args.length > 1){
    for(String arg: args){
      System.out.println(arg);
    }
  }
}

Run As》Run Configuration...》Arguments 输入a b c d e 》Run

这就是main方法在运行时传参

 

原创粉丝点击