Java-自学笔记-6

Java-自学-6Static versus Non-StaticIn this blog, I would like to compare the differences between Static methods and Non-Static methods.Static methodsIf you want to invoke Static methods, using class name.换句话说吧,就是Static methods是一个静态值(好像说了句废话,但绝不是)。也就是说,它
摘要由CSDN通过智能技术生成

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会有不同的响应,我们就得搞出点新东西来。


                
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值