首页 > 代码库 > Core java I 读书笔记

Core java I 读书笔记

Static关键字:

  我们都知道 static修饰符,表示 相应的 变量或方法,是这个类的实例所共有的,并不需要创建这个类的实例就可以访问。

  eg. public static final double PI = 3.14...  就可以直接通过Math.PI 获取。

  前边的都好理解,static method总有些不明白,下边着重学习一下static mathod:

  Static methods are methods that do not operate on objects.  比如 Math.pow(a,b),是在Math类上调用,not Object。

  You can think of static methods as methods that don’t have a this parameter. In a nonstatic method, the this parameter refers to the implicit parameter of the method 。
  a static method can access a static feld. 但是不能访问 实例的 fields 。

    Use static methods in two situations:

      ? When a method doesn’t need to access the object state because all needed parameters are supplied as explicit parameters (example: Math.pow).
      ? When a method only needs to access static felds of the class (example: Employee.getNextId).

  工厂方法:

  

Core java I 读书笔记