什么时候使用静态方法

问题:什么时候使用静态方法

I am wondering when to use static methods? Say if I have a class with a few getters and setters, a method or two, and I want those methods only to be invokable on an instance object of the class. Does this mean I should use a static method?

我想知道什么时候使用静态方法?就是说我有一个 带有一些getter和setter方法的类,我想这些只在一个类的实例对象中使用。那是不是意味着我应该用一个静态方法

例如:

Obj x = new Obj();
x.someMethod();
...or:Obj.someMethod(); // Is this the static way?

我现在非常疑惑

回答一

一条经验法则,问你自己“调用这个方法是否有意义的,即使还没有对象被构造出来” 如果是的话,他应该被定义为静态的。

就像在你一个汽车类里面,可能有一个方法

double convertMpgToKpl(double mpg)

…which would be static, because one might want to know what 35mpg converts to, even if nobody has ever built a Car. But this method (which sets the efficiency of one particular Car):

这个方法可以是静态的,因为即使没有建造过一辆汽车,我们也想知道35mpg 可以转换成多少。

但是下面这个方法(用来设置一辆特定汽车的功率)

void setMileage(double mpg)

这个就不能成为静态的了,因为在车辆没有被构造之前,不可能调用这个方法,

顺便说一下,这个转换不一定是对的,你有时可能有一个方法去调用两台汽车对象,这时候就还是静态的了,例如:

Car theMoreEfficientOf(Car c1, Car c2)

虽然它可以被转换成非静态的版本,一些人可能就会争论因为不存在一个特权选择哪台车比较重要,你不能强制选择一台车去调用这个方法。但是这种情况只是占所有静态方法中一个很小的部分

文章翻译自Stack Overflow:https://stackoverflow.com/questions/2671496/when-to-use-static-methods

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/391097.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

RESTful API浅谈

2019独角兽企业重金招聘Python工程师标准>>> 上半年时候,部门有组织的讨论了一下实践微服务的技术话题,主要内容是SOA服务和微服务各自的优势和难点,其中有提到关于RESTful API设计方法。 正好最近在深入的学习HTTP协议&#xff0…

spring自动注入--------

<?xml version"1.0" encoding"UTF-8"?> <beans xmlns"http://www.springframework.org/schema/beans"xmlns:p"http://www.springframework.org/schema/p"xmlns:c"http://www.springframework.org/schema/c"xmlns…

变量的作用域和生存期:_生存分析简介:

变量的作用域和生存期:In the previous article, I have described the Kaplan-Meier estimator. To give a quick recap, it is a non-parametric method to approximating the true survival function. This time, I will focus on another approach to visualizing a surviv…

数字孪生营销_如何通过数字营销增加您的自由职业收入

数字孪生营销There are a lot of ways we could go with this topic as it’s a huge one, but I just want to cover the nuggets here and make it simple as well as practical to understand and implement.我们可以采用很多方法来处理这个主题&#xff0c;因为它是一个很大…

您的网卡配置暂不支持1000M宽带说明

国内宽带网速越来越快&#xff0c;运营商更是在今年初纷纷推进千兆宽带业务。为了让用户更好地了解网络状况&#xff0c;360宽带测速器发布新版&#xff0c;优化了宽带测速范围&#xff0c;可有效支持最高1000&#xff2d;的带宽测量。此外&#xff0c;宽带测速器能检测用户网卡…

教辅的组成(网络流果题 洛谷P1231)

题目描述 蒟蒻HansBug在一本语文书里面发现了一本答案&#xff0c;然而他却明明记得这书应该还包含一份练习题。然而出现在他眼前的书多得数不胜数&#xff0c;其中有书&#xff0c;有答案&#xff0c;有练习册。已知一个完整的书册均应该包含且仅包含一本书、一本练习册和一份…

Java中怎么样检查一个字符串是不是数字呢

问题&#xff1a;Java中怎么样检查一个字符串是不是数字呢 在解析之前&#xff0c;怎么样检查一个字符串是不是数字呢 回答一 这些通常是由一个简单的用户自定义函数去解决的&#xff08;即&#xff0c;自带的 “isNumeric” 函数&#xff09; 例如 public static boolean…

小程序支付api密钥_如何避免在公共前端应用程序中公开您的API密钥

小程序支付api密钥问题 (The Problem) All you want to do is fetch some JSON from an API endpoint for the weather, some book reviews, or something similarly simple.您要做的就是从API端点获取一些有关天气的JSON&#xff0c;一些书评或类似的简单内容。 The fetch qu…

永无止境_永无止境地死:

永无止境Wir befinden uns mitten in der COVID-19-Pandemie und damit auch im Mittelpunkt einer medialen Geschichte, die durch eine noch nie dagewesene Komplexitt und Dynamik gekennzeichnet ist. Wie kann Informationsdesign helfen, diese Explosion von Nachrich…

HDU4612 Warm up —— 边双联通分量 + 重边 + 缩点 + 树上最长路

题目链接&#xff1a;http://acm.split.hdu.edu.cn/showproblem.php?pid4612 Warm up Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total Submission(s): 7206 Accepted Submission(s): 1681 Problem DescriptionN planets are …

Android sqlite load_extension漏洞解析

路人甲 2015/09/25 14:540x01 sqlite load_extensionSQLite从3.3.6版本&#xff08;http://www.sqlite.org/cgi/src/artifact/71405a8f9fedc0c2&#xff09;开始提供了支持扩展的能力&#xff0c;通过sqlite_load_extension API&#xff08;或者load_extensionSQL语句&#xf…

去除Java字符串中的空格

问题&#xff1a;去除Java字符串中的空格 俺有一个像这样的字符串 mysz "namejohn age13 year2001";我想要去除字符串里面的空格。我尝试使用 trim() &#xff0c;但是呢它只去除了字符串前后的空格。我也尝试用 ("\W", “”)&#xff0c;但是它把也给搞…

谷歌浏览器bug调试快捷键_Bug压榨初学者指南:如何使用调试器和其他工具查找和修复Bug

谷歌浏览器bug调试快捷键As web developers, it often feels like we spend more time fixing bugs and trying to solve problems than we do writing code. In this guide well look at some common debugging techniques, so lets get stuck in.作为Web开发人员&#xff0c;…

吴恩达神经网络1-2-2_图神经网络进行药物发现-第1部分

吴恩达神经网络1-2-2预测溶解度 (Predicting Solubility) 相关资料 (Related Material) Jupyter Notebook for the article Jupyter Notebook的文章 Drug Discovery with Graph Neural Networks — part 2 图神经网络进行药物发现-第2部分 Introduction to Cheminformatics 化学…

再利用Chakra引擎绕过CFG

xlab 2015/12/24 15:00Author:[email protected]0x00 前言本文源自一次与TK闲聊&#xff0c;期间得知成功绕过CFG的经过与细节(参考&#xff1a;[利用Chakra JIT绕过DEP和CFG])。随即出于对技术的兴趣&#xff0c;也抽出一些时间看了相关的东西&#xff0c;结果发现了另一处绕…

论文搜索源

中国科学院文献情报中心 见下图 中国计算机学会推荐国际学术会议和期刊目录 EI学术会议中心,        engieer village 转载于:https://www.cnblogs.com/cxy-941228/p/7693097.html

重学TCP协议(10)SYN flood 攻击

1.SYN flood 攻击 SYN Flood&#xff08;半开放攻击&#xff09;是一种拒绝服务&#xff08;DDoS&#xff09;攻击&#xff0c;其目的是通过消耗所有可用的服务器资源使服务器不可用于合法流量。通过重复发送初始连接请求&#xff08;SYN&#xff09;数据包&#xff0c;攻击者能…

大数据入门课程_我根据数千个数据点对互联网上的每门数据科学入门课程进行了排名...

大数据入门课程by David Venturi大卫文图里(David Venturi) A year ago, I dropped out of one of the best computer science programs in Canada. I started creating my own data science master’s program using online resources. I realized that I could learn everyt…

python 数据框缺失值_Python:处理数据框中的缺失值

python 数据框缺失值介绍 (Introduction) In the last article we went through on how to find the missing values. This link has the details on the how to find missing values in the data frame. https://medium.com/kallepalliravi/python-finding-missing-values-in-…

Spring Cloud 5分钟搭建教程(附上一个分布式日志系统项目作为参考) - 推荐

http://blog.csdn.net/lc0817/article/details/53266212/ https://github.com/leoChaoGlut/log-sys 上面是我基于Spring Cloud ,Spring Boot 和 Docker 搭建的一个分布式日志系统. 目前已在我司使用. 想要学习Spring Cloud, Spring Boot以及Spring 全家桶的童鞋,可以参考学习,如…