Android journey 1@关于编码风格和命名规范
生活随笔
收集整理的這篇文章主要介紹了
Android journey 1@关于编码风格和命名规范
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
/*
* 1.關于編程風格:每一位程序猿可能都有自己獨特的編程風格,但是有些規則是大家都必須遵守的,特別
* 是在工作的過程中,良好的代碼風格能大大提高代碼本身的可閱讀性和維護性,也更有利于別人修改你
* 的代碼。風格問題涉及到的都是細節相關的問題,讀者朋友們或許都聽過“細節決定成敗”這句話,這句話
* 在這里同樣生效,不過是換了種意思,就是“細節體現你的專業程度”,所以作為一名程序猿,我們應該從
* 編寫代碼的訓練中養成良好的編碼習慣,如命名規范,排版規范,寫好注釋等。下面具體說明下:
*/
/*
* 1)整個Project相關內容的目錄規范:(通常可以包含以下幾個文件夾)
* #.Requirement Doc: project的需求文檔說明
* #.Design:有關設計方面的說明
* #.Planning&Log:計劃、日志、會議等
* #.Test:測試及其報告
* #.Study:學習資料及demo
* #.Deployment:發布,部署
* #.src: 源代碼
* #.help:幫助文檔
* #. ...(根據自己需求添加合適文檔)
* 注意事項:在IDE建立project的時候,包即目錄(java),包名全小寫,且一般不超過3層,命名為:【公司】.【項目】.【模塊】
*/
/*
* 2)命名規范:
* #、Pascal命名法:每個單詞首字母大寫,其余小寫,如“ActivityDemo”,主要應用于:文件名,普通類名,構造函數等
* #、Camel命名法:第一個單詞全小寫,其余單詞按pascal命名,如“myName”,主要應用于:方法名(動賓短語),普通函數名等
* #、匈牙利命名法(使用前綴,后綴命名):如對于整型變量,可以“int intMyAge;”
* #、下劃線命名法:主要在常量(全大寫)中使用,分隔兩個不同單詞
* 其他常見命名:私有成員變量:“mMyName”;靜態變量:“sMyName”;方法參數:“pMyName”;
* 當然命名規則的使用主要看已有項目的要求,若新啟動一個項目,編碼前團隊必須先統一命名規范
*/
/*
* 3)其他需要注意的地方:
* 1、一個方法不超過35行代碼
* 2、不要去修改機器自動生成的代碼
* 3、final String取代String,即不要直接寫字符串
* 4、循環語句中不用return
* 5、屬性用set(),get()方法操控
* 6、不要再系統生成函數中寫復雜代碼,復雜代碼通常用函數調用解決
* 7、雖然class能搞定一切,但使用接口暴露信息會更好
* 8、switch語句中無論如何都要有default
* 9、同類的import包放在一起
* ...
* */ //下面看一下Android源碼中的代碼:/** Copyright (C) 2006 The Android Open Source Project** Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at** http://www.apache.org/licenses/LICENSE-2.0** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.* See the License for the specific language governing permissions and* limitations under the License.*/package android.app;import com.android.internal.app.ActionBarImpl; import com.android.internal.policy.PolicyManager;import android.os.Build; import android.os.Bundle; import android.os.UserHandle; import android.text.Selection; import android.text.SpannableStringBuilder; import android.text.TextUtils; import android.text.method.TextKeyListener; import android.util.AttributeSet; import android.util.EventLog;/**可在注釋中嵌入HTML標簽,在生成javadoc的時候控制各種屬性等* An activity is a single, focused thing that the user can do. Almost all* activities interact with the user, so the Activity class takes care of* creating a window for you in which you can place your UI with* {@link #setContentView}. While activities are often presented to the user* as full-screen windows, they can also be used in other ways: as floating* windows (via a theme with {@link android.R.attr#windowIsFloating} set)* or embedded inside of another activity (using {@link ActivityGroup}).** There are two methods almost all subclasses of Activity will implement:* * <ul>* <li> {@link #onCreate} is where you initialize your activity. Most* importantly, here you will usually call {@link #setContentView(int)}* with a layout resource defining your UI, and using {@link #findViewById}* to retrieve the widgets in that UI that you need to interact with* programmatically.* * <li> {@link #onPause} is where you deal with the user leaving your* activity. Most importantly, any changes made by the user should at this* point be committed (usually to the* {@link android.content.ContentProvider} holding the data).* </ul>...*/ public class Activity extends ContextThemeWrapperimplements LayoutInflater.Factory2,Window.Callback, KeyEvent.Callback,OnCreateContextMenuListener, ComponentCallbacks2 {private static final String TAG = "Activity";private static final boolean DEBUG_LIFECYCLE = false;/** Standard activity result: operation canceled. *///常量命名public static final int RESULT_CANCELED = 0;/** Standard activity result: operation succeeded. */public static final int RESULT_OK = -1;/** Start of user-defined activity results. */public static final int RESULT_FIRST_USER = 1;static final String FRAGMENTS_TAG = "android:fragments";private static final String WINDOW_HIERARCHY_TAG = "android:viewHierarchyState";private static final String SAVED_DIALOG_IDS_KEY = "android:savedDialogIds";private static final String SAVED_DIALOGS_TAG = "android:savedDialogs";private static final String SAVED_DIALOG_KEY_PREFIX = "android:dialog_";private static final String SAVED_DIALOG_ARGS_KEY_PREFIX = "android:dialog_args_";private static class ManagedDialog {Dialog mDialog;Bundle mArgs;}private SparseArray<ManagedDialog> mManagedDialogs;// set by the thread after the constructor and before onCreate(Bundle savedInstanceState) is called.//私有變量命名private Instrumentation mInstrumentation;private IBinder mToken;private int mIdent;static final class NonConfigurationInstances {Object activity;HashMap<String, Object> children;ArrayList<Fragment> fragments;HashMap<String, LoaderManagerImpl> loaders;}/* package */ NonConfigurationInstances mLastNonConfigurationInstances;private Window mWindow;/** * Change the intent returned by {@link #getIntent}. This holds a * reference to the given intent; it does not copy it. Often used in * conjunction with {@link #onNewIntent}. * * @param newIntent The new Intent object to return from getIntent * * @see #getIntent* @see #onNewIntent*自定義javadoc標簽*@author ..*@date ..*@modify ..(who,when..)*/ public void setIntent(Intent newIntent) {mIntent = newIntent;}
* */ //下面看一下Android源碼中的代碼:/** Copyright (C) 2006 The Android Open Source Project** Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at** http://www.apache.org/licenses/LICENSE-2.0** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.* See the License for the specific language governing permissions and* limitations under the License.*/package android.app;import com.android.internal.app.ActionBarImpl; import com.android.internal.policy.PolicyManager;import android.os.Build; import android.os.Bundle; import android.os.UserHandle; import android.text.Selection; import android.text.SpannableStringBuilder; import android.text.TextUtils; import android.text.method.TextKeyListener; import android.util.AttributeSet; import android.util.EventLog;/**可在注釋中嵌入HTML標簽,在生成javadoc的時候控制各種屬性等* An activity is a single, focused thing that the user can do. Almost all* activities interact with the user, so the Activity class takes care of* creating a window for you in which you can place your UI with* {@link #setContentView}. While activities are often presented to the user* as full-screen windows, they can also be used in other ways: as floating* windows (via a theme with {@link android.R.attr#windowIsFloating} set)* or embedded inside of another activity (using {@link ActivityGroup}).** There are two methods almost all subclasses of Activity will implement:* * <ul>* <li> {@link #onCreate} is where you initialize your activity. Most* importantly, here you will usually call {@link #setContentView(int)}* with a layout resource defining your UI, and using {@link #findViewById}* to retrieve the widgets in that UI that you need to interact with* programmatically.* * <li> {@link #onPause} is where you deal with the user leaving your* activity. Most importantly, any changes made by the user should at this* point be committed (usually to the* {@link android.content.ContentProvider} holding the data).* </ul>...*/ public class Activity extends ContextThemeWrapperimplements LayoutInflater.Factory2,Window.Callback, KeyEvent.Callback,OnCreateContextMenuListener, ComponentCallbacks2 {private static final String TAG = "Activity";private static final boolean DEBUG_LIFECYCLE = false;/** Standard activity result: operation canceled. *///常量命名public static final int RESULT_CANCELED = 0;/** Standard activity result: operation succeeded. */public static final int RESULT_OK = -1;/** Start of user-defined activity results. */public static final int RESULT_FIRST_USER = 1;static final String FRAGMENTS_TAG = "android:fragments";private static final String WINDOW_HIERARCHY_TAG = "android:viewHierarchyState";private static final String SAVED_DIALOG_IDS_KEY = "android:savedDialogIds";private static final String SAVED_DIALOGS_TAG = "android:savedDialogs";private static final String SAVED_DIALOG_KEY_PREFIX = "android:dialog_";private static final String SAVED_DIALOG_ARGS_KEY_PREFIX = "android:dialog_args_";private static class ManagedDialog {Dialog mDialog;Bundle mArgs;}private SparseArray<ManagedDialog> mManagedDialogs;// set by the thread after the constructor and before onCreate(Bundle savedInstanceState) is called.//私有變量命名private Instrumentation mInstrumentation;private IBinder mToken;private int mIdent;static final class NonConfigurationInstances {Object activity;HashMap<String, Object> children;ArrayList<Fragment> fragments;HashMap<String, LoaderManagerImpl> loaders;}/* package */ NonConfigurationInstances mLastNonConfigurationInstances;private Window mWindow;/** * Change the intent returned by {@link #getIntent}. This holds a * reference to the given intent; it does not copy it. Often used in * conjunction with {@link #onNewIntent}. * * @param newIntent The new Intent object to return from getIntent * * @see #getIntent* @see #onNewIntent*自定義javadoc標簽*@author ..*@date ..*@modify ..(who,when..)*/ public void setIntent(Intent newIntent) {mIntent = newIntent;}
轉載于:https://www.cnblogs.com/allenpengyu/p/3495156.html
總結
以上是生活随笔為你收集整理的Android journey 1@关于编码风格和命名规范的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 技巧:在Silverlight 2应用程
- 下一篇: android sina oauth2.