什么時候使用靜態方法

問題:什么時候使用靜態方法

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.pswp.cn/news/391097.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/391097.shtml
英文地址,請注明出處:http://en.pswp.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 全家桶的童鞋,可以參考學習,如…