Java-自学-6
Static versus Non-Static
In this blog, I would like to compare the differences between Static methods and Non-Static methods.
Static methods
If you want to invoke Static methods, using class name.
换句话说吧,就是Static methods是一个静态值(好像说了句废话,但绝不是)也就是说,只能通过class name去调用它。
按照老师上课举得例子我觉得相当形象:
/** Static methods */
public static void makeNoise(){
System.out.println("Bark!")
}
/** if we want to invoke this method, we should do as follow */
Dog.makeNoise();
OK,上面的代码可以明显看出静态方法的特点,通过类进行直接调用。可能有人看了会一头雾水,那Non-Static能有什么区别呢?
Non-Static methods
那么非静态方法究竟是怎么样的呢?
我们来想一下,假如全世界都是Static methods,会有什么问题吗?
这个世界上有很多狗,大小、体重、种类均不同,那么它们的叫声相比也不尽相同。但是如果用Static methods,我们会看到全世界的狗,只会" Bark! "。有点可怕吧。因此对应不同的输入变量,如果期待这个method会有不同的响应,我们就得搞出点新东西来。