书上有一段话:
A static field or method is not associated with each instance of the class. Instead, there is only a single instance of the field or method, and it is shared among all instances of the class and also the other classes in your program.
大意是说:
静态域或者方法并不属于类的某一个实例。相反,静态域或者方法的实例只有一个,在你的程序中,它被这个类和其他类的所有实例对象所共享。这句话中的最后一句,“被这个类和其他类的所有实例所共享”该如何理解呢?
静态域或者方法被自己所在的类的所有实例对象共享,这个倒是可以理解,可是被其他类的实例所共享,这个怎么说的过去呢?哪位大侠可以指点指点么?
A static field or method is not associated with each instance of the class. Instead, there is only a single instance of the field or method, and it is shared among all instances of the class and also the other classes in your program.
大意是说:
静态域或者方法并不属于类的某一个实例。相反,静态域或者方法的实例只有一个,在你的程序中,它被这个类和其他类的所有实例对象所共享。这句话中的最后一句,“被这个类和其他类的所有实例所共享”该如何理解呢?
静态域或者方法被自己所在的类的所有实例对象共享,这个倒是可以理解,可是被其他类的实例所共享,这个怎么说的过去呢?哪位大侠可以指点指点么?
比如Math类,使用时类名.方法名就可以了,就被外边的类共享了
但一般的调用方法还是: 类名.方法名
这位置是说其它类,而不是其它类的实例,在其它类中通过类名.方法名()访问静态方法,类名.属性名访问静态属性(当然,具体能否访问还取决于静态方法或属性的修饰符)