java基礎(8)

來源:互聯網
上載者:User

標籤:傳遞   表達   產生   結構   單擊   登陸   退出   shang   姓名   

一、變數的範圍(有效使用範圍)

1、變數有2種

              1.1成員變數(屬性)

聲明在類的裡面,方法的外面

              1.2 局部變數

                                      聲明在方法裡面或for迴圈結構中

2、調用時的注意事項

2.1 成員變數,有預設值(不初始化),在當前類直接通過成員變數名調用;在其他類中,通過該類的對象名.屬性(成員變數)名調用;成員變數名稱在同一個類中是唯一的。

              2.2 局部變數,沒有預設值,必須先賦值再使用

局部變數,只能在當前的方法中使用,其他方法和其他類不能調用;在同一個方法中名稱是唯一的,但多個方法中,允許局部變數同名

              2.3 局部變數和成員變數同名時,局部變數的優先順序高於成員變數

public class shangji1 {    int java;    int c;    int db;    //計算總分    public int totalScore(){        return java+c+db;    }    //輸出總分    public void showScore(){        System.out.println("總分是:"+totalScore());    }    //計算平均分    public double avge(){        int a=totalScore();        return a/3.0;    }    public void cuAvge(){        System.out.println("平均分是:"+avge());    }}
public static void main(String [] args){        shangji1 s=new shangji1();        Scanner imput=new Scanner(System.in);        System.out.print("請輸入Java的成績:");        s.java=imput.nextInt();        System.out.print("請輸入c的成績:");        s.c=imput.nextInt();        System.out.print("請輸入db的成績:");        s.db=imput.nextInt();        s.showScore();        s.cuAvge();    }

 

二、java文檔注釋(javadoc)自動產生

      1、單行注釋://

      2、多行注釋:/* */

3、文檔注釋:/**    */

步驟:

           選擇項目後,按右鍵再選擇【export…】,4個選項按鈕中,選擇package,最後點擊【finish】完成協助文檔html檔案的產生,預設儲存在當前項目下的【doc】檔案夾中

 

三、java中無參方法的定義和調用

1、方法的作用

                  封裝(重複的代碼)

                  提高代碼的可重用性、可維護性

2、如何定義無參方法 _方法命名遵循駱駝命名法

                   public 傳回型別 方法名( ){

                                                      //……………方法體

                   }

3、根據傳回型別,無參的方法,分為2種

                  3.1 有傳回型別(資料類型),方法體中,必須出現

return 【運算式、變數、值】,return 寫在方法中最後一行

                  3.3 沒有傳回型別,在定義方法時,傳回型別寫void,沒有return語句,

輸出結果(System.out.println)

4、方法的調用—

l  調用其他類的方法:對象名.方法名();

l  同一個類中,方法之間的調用:方法名();

      4.1 無傳回型別

                                          對象名.方法名();//輸出資料

      4.2 有傳回型別

                                          資料類型 變數名=對象名.方法名();

                                          System.out.println(變數名);

                                          或        System.out.println( 對象名.方法名() );

四、物件導向的3大特徵:封裝、繼承、多態

public class shangji3 {    //登陸菜單    public void showMenu(){        System.out.println("\n\t歡迎使用我行我素購物管理系統\n");        System.out.println("\t\t1.登陸");        System.out.println("\t\t2.退出");        System.out.println("* * * * * * * * * * * * * * * * * * * *");        System.out.print("請選擇:");    }    //主菜單    public void mainMenu(){        System.out.print("\t我行我素購物管理系統主菜單");        System.out.println("* * * * * * * * * * * * * * * * * * * *");        System.out.println("\t\t1.客戶資訊管理");        System.out.println("\t\t2.真情回饋");        System.out.println("* * * * * * * * * * * * * * * *  * * * * * * * * * *");        boolean con;        Scanner input=new Scanner(System.in);        System.out.print("請選擇,輸入數字或按0返回上一級菜單:");        int no=input.nextInt();        do{            con=false;            //輸入數字,選擇菜單。        switch (no){        case 1:            custMenu();            break;        case 2:            sendMenu();            break;        case 0:            mainMenu();            break;        default:            System.out.println("請輸入正確的數字!");            con=true;            break;        }        }while(con);    }    public void sendMenu(){        System.out.print("\t我行我素購物管理系統 > 真情回饋");        System.out.println("* * * * * * * * * * * * * * * *  * * * * * * * * * *");        System.out.println("\n\t\t1.幸運大放送");        System.out.println("\n\t\t2.幸運抽獎");        System.out.println("\n\t\t3.生日問候");        System.out.println("* * * * * * * * * * * * * * * *  * * * * * * * * * *");        boolean com;        do{            com=false;        //輸入數字,選擇菜單        Scanner input=new Scanner(System.in);        System.out.print("請選擇,輸入數字或按0返回上一級菜單:");        int no= input.nextInt();        switch (no){        case 1:            System.out.println("執行幸運大放送");            break;        case 2:            System.out.println("執行幸運抽獎");            break;        case 3:            System.out.println("生日問候");            break;        case 0:            mainMenu();            break;        default:            System.out.println("請輸入正確的數字");            com=true;            break;            }        }while(com);    }    public void custMenu(){            }}
public static void main(String [] args){        shangji3 f=new shangji3();        //顯示菜單        f.showMenu();        //實現菜單        boolean d=true;        do{        Scanner input =new Scanner(System.in);        int choice=input.nextInt();        switch (choice){        case 1:            f.mainMenu();            break;        case 2:            System.out.println("謝謝您的使用!");            d=false;            break;        }        }while(d);    }

 

public static void main(String [] args){        shangji1 s=new shangji1();        Scanner imput=new Scanner(System.in);        System.out.print("請輸入Java的成績:");        s.java=imput.nextInt();        System.out.print("請輸入c的成績:");        s.c=imput.nextInt();        System.out.print("請輸入db的成績:");        s.db=imput.nextInt();        s.showScore();        s.cuAvge();    }

 

5、Java中的包(package)

                  5.1 包,對應到磁碟中的檔案夾

                  5.2 建立一個class,預設儲存在預設包中

                 5.3 聲明包的關鍵字:package

                                                                  package語句,置頂位置

                  5.4 匯入包的關鍵字:import

                                                                  import 包名.類名(*表示所有);

                  5.5 作用:

l  分類存放,解決同名衝突

l  分類存放,方便尋找和管理

5.6 命名規範——參考教材

             

6、有參數的方法

 

                  6.1 如何聲明

                             

                              public 傳回型別 方法名(資料類型 參數名){

                                          ….方法體

                              }

                  6.2 分為2類

                              6.2.1 無傳回型別——void

                              6.2.2 有傳回型別

l  基礎資料型別 (Elementary Data Type)

l  自訂類——return 類的對象;

l  數組——return 數組名;

6.3參數的類型

l  基礎資料型別 (Elementary Data Type)(10-31)

l  自訂類——傳遞的是類的對象

l  數組——只需要傳遞 【數組名】

6.4調用

6.4.1在當前類中調用——方法名(值列表);

6.4.2在其他類中調用——對象名.方法名(值列表);

package Pack1;public class No3 {    String names[]=new String[30];    public void addName(String name){        for (int i=0;i<5;i++){            names[i]=name;            if(i==5){                System.out.println(names[i]+"");            }        }    }    public void showName(){        }}

 

package Pack2;import Pack1.No3;    //匯入包import java.util.*;public class No1 {    public static void main(String [] args){        No3 d=new No3();        Scanner input=new Scanner(System.in);        for(int i=0;i<5;i++){            System.out.print("請輸入學生姓名:");            String newName=input.next();            d.addName(newName);//調用方法並傳實參        }        d.showName();    }}

 

7、方法總結

分類:

1、            無傳回型別,無參數

2、            無傳回型別,有參數

3、            有傳回型別,無參數

4、            既有傳回型別,又有參數

參數或傳回型別,可以是常用的資料類型,也可以是自訂的類或數組

java基礎(8)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.