Java Basics (8)

Source: Internet
Author: User

The scope of the variable (valid scope of use)

1. There are 2 kinds of variables

1.1 member variables (attributes)

declaration in the inside of the class, outside of the method

1.2 Local variables

Declared within a method or for loop structure

2. Considerations when calling

2.1 member variables, with default values (uninitialized), are called directly through member variable names in the current class, and in other classes through the object name of the class. The name of the member variable is unique within the same class.

2.2 Local variables, no default values, must be assigned before use

Local variables, which can only be used in the current method, other methods and other classes cannot be called; names are unique in the same method, but multiple methods allow local variables with the same name

2.3 Local variables have precedence over member variables when local variables and member variables have the same name

public class Shangji1 {    int java;    int C;    int db;    Calculate total public    int Totalscore () {        return java+c+db;    }    Output total public    void Showscore () {        System.out.println ("Total score is:" +totalscore ());    }    Computes the average of the public    double Avge () {        int a=totalscore ();        return a/3.0;    }    public void Cuavge () {        System.out.println ("Average score is:" +avge ());}    }
public static void Main (String [] args) {        shangji1 s=new shangji1 ();        Scanner imput=new Scanner (system.in);        System.out.print ("Please enter Java's score:");        S.java=imput.nextint ();        System.out.print ("Please enter C's score:");        S.c=imput.nextint ();        System.out.print ("Please input DB score:");        S.db=imput.nextint ();        S.showscore ();        S.cuavge ();    }

Second, Java Document Comments (Javadoc) automatically generated

1, single-line comment://

2. Multi-line Comment:/* */

3. Document Comment:/** */

Steps:

After you select the item, right-click and select Export ..., 4 radio buttons, select the package, and finally click Finish to complete the build of the Help document HTML file, which is saved by default in the Doc folder under the current project.

Third, the definition and invocation of the non-parameter method in Java

1, the role of the method

Encapsulation (duplicated code)

Improve code reusability, maintainability

2, how to define the method of non-parameter _ methods named followed by camel nomenclature

Public return type method name () {

............... Method body

}

3, according to the return type, no parameter method, divided into 2 kinds

3.1 There is a return type (data type), in the method body, must appear

Return "expression, variable, value", return written in the last line of the method

3.3 There is no return type, when the method is defined, the return type is write void, there is no return statement,

Output results (SYSTEM.OUT.PRINTLN)

4. Method Invocation-

L Call methods of other classes: Object name. Method Name ();

L A call between methods in the same class: Method name ();

4.1 No return type

The name of the object. Method name ();//Output data

4.2 has return type

Data type variable name = Object name. Method Name ();

SYSTEM.OUT.PRINTLN (variable name);

or System.out.println (object name. Method name ());

Iv. Object-oriented 3 Big features: encapsulation, inheritance, polymorphism

public class Shangji3 {//Login menu public void ShowMenu () {System.out.println ("\n\t Welcome to use your own shopping management system \ n");        System.out.println ("\t\t1. Login");        System.out.println ("\t\t2. Exit");        SYSTEM.OUT.PRINTLN ("* * * * * * * * * * * * * * * * * * * * * * * *");    System.out.print ("Please select:");        }//main menu public void MainMenu () {System.out.print ("\ t go shopping management System main Menu");        SYSTEM.OUT.PRINTLN ("* * * * * * * * * * * * * * * * * * * * * * * *");        System.out.println ("\T\T1. Customer information management");        System.out.println ("\t\t2. True feedback");        SYSTEM.OUT.PRINTLN ("* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *);        Boolean con;        Scanner input=new Scanner (system.in);        System.out.print ("Please select, enter a number or press 0 to return to the previous menu:");        int No=input.nextint ();            do{Con=false;        Enter a number and select the menu.            Switch (NO) {case 1:custmenu ();        Break            Case 2:sendmenu ();        Break            Case 0:mainmenu (); BreAk Default:System.out.println ("Please enter the correct number!")            ");            Con=true;        Break    }}while (Con);        } public void Sendmenu () {System.out.print ("\ t go shopping management System > True feedback");        SYSTEM.OUT.PRINTLN ("* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *);        System.out.println ("\n\t\t1. Lucky Big Broadcast");        System.out.println ("\n\t\t2 Lucky Draw");        System.out.println ("\n\t\t3. Birthday Greetings");        SYSTEM.OUT.PRINTLN ("* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *);        Boolean com;        do{Com=false;        Enter a number, select Menu Scanner input=new Scanner (system.in);        System.out.print ("Please select, enter a number or press 0 to return to the previous menu:");        int no= input.nextint ();            Switch (NO) {case 1:system.out.println ("Execute lucky Big Broadcast");        Break            Case 2:system.out.println ("Performing Lucky Draw");        Break            Case 3:system.out.println ("Birthday greetings");        Break            Case 0:mainmenu (); BrEak            Default:System.out.println ("Please enter the correct number");            Com=true;            Break    }}while (COM); } public void Custmenu () {}}
public static void Main (String [] args) {        shangji3 f=new shangji3 ();        Display menu        f.showmenu ();        Implement Menu        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 ("Thank you for your use!") ");            D=false;            break;        }        } while (d);    

public static void Main (String [] args) {        shangji1 s=new shangji1 ();        Scanner imput=new Scanner (system.in);        System.out.print ("Please enter Java's score:");        S.java=imput.nextint ();        System.out.print ("Please enter C's score:");        S.c=imput.nextint ();        System.out.print ("Please input DB score:");        S.db=imput.nextint ();        S.showscore ();        S.cuavge ();    }

5. Packages (package) in Java

5.1 packages, corresponding to folders on disk

5.2 Create a new class that defaults to the default package

5.3 Declaring the Package keyword: packages

Package statement, pinned location

5.4 Importing a package keyword: import

Import package name. Class Name (* denotes all);

5.5 Effects:

L classify and resolve conflicts with the same name

L classify and store, easy to find and manage

5.6 Naming conventions--reference materials

6 . Methods with Parameters

6. 1 How to declare

Public return type method name (data type parameter name) {

.... Method body

}

6.2 Divided into 2 categories

6.2.1 No return type--void

6.2.2 has return type

L Basic Data types

L Custom Class-- The object of the return class ;

L Array--return array name ;

6.3 Types of parameters

L Basic Data type (10-31)

L Custom Class--passes the object of the class

L array--only the "array name" needs to be passed

6.4 Calls

6.4.1 called in the current class--Method name (value list);

6.4.2 called in other classes--object name. Method Name (value list);

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;    Importing package Import java.util.*;p Ublic 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 ("Please enter student's name:");            String Newname=input.next ();            D.addname (newName);//call method and pass argument        }        d.showname ();}    }

7. Summary of methods

Classification:

1, no return type, no parameters

2, no return type, parameters

3, there is a return type, no parameters

4. There are both return types and parameters

parameter or return type, which can be a common data type or a custom class or array

Java Basics (8)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.