Java arrays, methods, classes and objects

Java arrays -

                  Elements in an array are accessed by subscripting

data type[ ] array name;    // or: data type array name[ ];
arrayName = newDataType   [ArrayLength];

      You can assign an array to another array in Java:

int [] a1 = {1,2,3};
int [] a2;
a2 = a1;

 

Java method -

 

access modifier return value type method name (parameter list) {
    method body
}

                  1. Access modifier: It represents the scope of permissions that the method is allowed to be accessed, which can be public, protected, private or even omitted. Public means that the method can be called by any other code. The use of several other modifiers will be described in the following chapters. explained in detail.

                  2. Return value type: The type of the return value of the method. If the method does not return any value, the return value type is specified as void (representing no type); if the method has a return value, the type of the return value needs to be specified, and in the method body Use the return statement to return a value.

                  3. Method name: It is the name of the method and must use a legal identifier.

                  4. Parameter list: It is a list of parameters passed to the method. There can be multiple parameters. Multiple parameters are separated by commas. Each parameter consists of parameter type and parameter name, separated by spaces. When the method is called, pass values ​​to the parameters. This value is called an argument or variable. The parameter list refers to the parameter type, order, and number of parameters of the method. Arguments are optional and methods can contain no arguments.

                  5. Method body: The method body contains specific statements that define the function of the method.

       According to whether the method takes parameters and returns a value, methods can be divided into four categories:

                                                                  No parameter, no return method
                                                                  , no parameter and return value, method
                                                                  with parameter, no return value, method
                                                                  with parameter and return value

 

The calcSum method is used to calculate the total score for the two courses:

public class PrintScore {
    public static void main(String[] args) {

        // Create an object named a 
        PrintScore a = new PrintScore();

        int rSum;   // Set an int variable to receive the return value of the method

        // Call the method, pass in the grades of the two courses 
        rSum = a.calcSum(78,99 );
        System.out.println( "Total score: "+ rSum);
    }

    /*
     * Function: Calculate the total score of the test scores of the two courses and output the total score
     * Define a method with two parameters to pass in the grades of the two courses
     */
    public int calcSum(int a, int b){
            int sum= a + b;
            return sum;
    }
}
       /* The method name is calcSum, its modifier is public, it has two parameters int a and int b,
        * These two parameters must be passed in when the method is called. It has an int return value, so in the method, there is a statement return sum;
        * Used to return the value of sum, where sum type is also int type. When the method is called, we use an int variable rSum to receive the return value. The method calcSum realizes the calculation and return of the total grades of the two courses.
        */

         Notice:

            When calling a method with parameters, you must ensure that the number, type, and order of the actual parameters correspond one-to-one with the formal parameters

            When calling a method, the actual parameters do not need to specify a data type

            When there are multiple method parameters, the multiple parameters are separated by commas

 

- Method overloading:

                          In a class, there are a series of methods with the same method name, but different parameter lists, and different method parameter lists (the number of parameters and parameter types are different)

               When doing method overloading, the following rules need to be followed:

                     1. When using method overloading, method overloading must be implemented through different parameter lists in the method. For example, the number of parameters of the method is different or the parameter type of the method is different.
                     2. Overloading cannot be implemented by access rights, return value type and thrown exception
                     3. Different exceptions are allowed to be thrown in overloaded methods
                     4. There can be different return value types, as long as the method parameter list is different
                     5. Can have different access modifiers

 

- Parameters and return values:

         Notice:

                    If the return type of the method is void, the return return value cannot be used in the method.

                    The return value of a method can only have at most one value, and cannot return multiple values.

                    The type of the return value of the method must be compatible, that is, if the return value type is int, it cannot return a String type value

                    When calling a method with a return value, since the method returns a result after execution, when calling a method with a return value, the return value is generally received and processed.

 

Java classes and objects -

                kind

public  class class name {
 // Define the type attribute 1 of the attribute part (member variable) 
attribute 1;
property2 of type property2;
...
// Define method part 
method 1;
Method 2;
...
}

        There are three main steps to define a class:

                  1. Define the class name, which is used to distinguish different classes. In the following code, public class is followed by the class name. class is the keyword we declare the class, the class name is followed by curly brackets, and the curly brackets are some information about our class. public, we will explain it in detail later, just remember it first.

                  2. Write the properties of the class. What an object has, we represent it through properties. The definition of the attribute is written in the curly brackets after the class name. When defining the attribute, the type of the attribute must be specified. One or more properties can be written in a class. Of course, it is also possible to not define properties.

                  3. Write the method of the class. Methods are also written in curly brackets. We can define a method or multiple methods, of course we can define no method.

 

      A class can contain variables of the following types:

  •            Local variables: Variables defined in a method, constructor or block of statements are called local variables. Variable declaration and initialization are in the method, after the method ends, the variable will be automatically destroyed.
  •            Member variables: Member variables are variables defined in the class, outside the method body. This variable is instantiated when the object is created. Member variables can be accessed by class methods, constructors, and class-specific blocks.
  •            Class variables: also called static variables, class variables are also declared in the class, outside the method body, but must be declared as static type.

 

          Construction method

  •  Every class has a constructor.
  •  If no constructor is explicitly defined for the class, the Java compiler will provide a default constructor for the class.
  •  When creating an object, at least one constructor must be called. The name of the constructor must have the same name as the class, and a class can have multiple constructors.

 

        The class is instantiated into an object through the new keyword, and new is followed by the constructor,

        So you can know that the new + constructor can create a new object:

package com.shiyanlou;

public  class People {
 // What does the property (member variable) have 
    double height;      // height 
    int age;            // age 
    int sex;        // gender, 0 is male, non-0 is female

    // Constructor, initializes all properties 
    public People( double h, int a, int s){
        height = h;
        age = a;
        sex = s;
    }
}

// Create an object and call our own parameterized constructor 
People XiaoMing = new People(168, 21, 1);

1. The name of the constructor is the same as the class name, and there is no return value.

2. If we do not write a constructor when defining a class, the system will generate a no-argument constructor for us by default, but this constructor will do nothing.

3. When there is a specified construction method, the system will no longer add a parameterless construction method for us.

4. Overloading of constructors: For multiple methods with the same method name but different parameters, the corresponding method will be automatically selected according to different parameters when called.

 

 

        object

                        When defining a class, no memory space is opened for the class, but once an object is created, the system will open up a space for the object in memory to store the attribute values ​​and methods of the object.

public class NewObject {
    public static void main(String[] args) {
        People LiLei = new People(); // Create a People object LiLei 

        LiLei.height =170 ;
        LiLei.age = 20 ;
        LiLei.sex = 1;

        LiLei.printBaseMes();
    }
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325210197&siteId=291194637