Python learning day8 properties and methods of object-oriented programming class 2-

First, the properties of the class

1. syntax

Modifiers type attribute name = initial value

Description: modifier Private : This attribute can be used by methods of the class. Visible in the same class. Use objects: variables and methods. Note: You can not modify the class (outside class)

     Modifier public: This attribute can be accessed by a method other than the class. Visible to all classes. Using objects: classes, interfaces, variables, methods

          Type: Any basic type, such as int, boolean, or any class

Added: modifiers can be used to modify variables, you can specify the variable is public or private, private variables can not be the object properties. Way call

2. Classification of variables:

  1. The member variables: in vitro methods, in vivo variable classes, referred to as a member variable declarations

      Member variables include: 1 instance variable (not in a static modification)

                  2. class variable (with static modification)

  2. The local variables: internal variables declared in the method is referred local variables

      Local variables comprising: a parameter (defined by the method signature variable)

             2. The method of local variables (variables defined in the method)

             3. Local variables block (variable defined within the code block)

Example:

public  class the Person {
     public String name = "Zhao"; // public: the public, such a class can be used not only in the process of this class can be used from outside the class 
    Private  int Age; // Private: private, can only be used in this method in the class, can not use outside the class
     // more variable as an instance variable, the variable refers to use after class instantiated objects 
    public  static String Sex = "M"; // static: static , so that a variable called the class variable, no instantiated objects can be used. (May be called directly by the class name attribute) 
    public  void showAge () { 
        System.out.println (Age); 
    } 
}

  3. The difference between the member variables and local variables

      Member variables: 1 member variables defined in the class, can be accessed throughout the class.

           2. The member variables into class member variable and instance variables, instance variables only exist in the members of the object located in the heap memory (instance variables in the object can be used only after the class is instantiated).

           3. member variable has a default initial value.

           4. The member variables permission modifier may be arbitrarily selected from a (private public or protected) according to need.

      Local variables: 1. Local variables are defined in the local area, such as the method, like the code block (local variables can only be used within the scope of the method to which it belongs).

           2. The local variable on the stack memory. The scope of action of the end, variable space will be automatically released.

           3. Local variables are not initialized default values, each must be explicitly initialized.

           4. When you do not specify permissions local variable declaration modifier

Second, the class methods

 1. syntax:

Modifier return type method name (parameter list) { 
    method body statement; 
}    

Description: Returns the value is not 1, with the void

   2. a return value, the return value with a particular type, for example, int is defined on the return type of the method, with the latter method is the return value of the return

Example:

public  class the Person {
     Private  int Age;
     public  int getAge () { // declare a method getAge 
        return Age; 
        } 
    public  void the setAge ( int i) { // declare a method the setAge 
        Age = i; // parameter value assigned to class i member variables Age 
        } 
    }

 

Third, on the method

  1. What is the method (function)?

    Class is abstract or behavioral characteristics of the object, also called a function. Methods in Java can not exist, all the methods must be defined in the class.

  2. syntax:

Modifier return type method name (parameter type parameter 1, parameter type, parameter 2, ...) { 
    program code to 
    return a return value; 
}

Wherein: in the form of parameters: a variable for receiving incoming data in the external method is invoked. (Method parameter may be a plurality)

      Parameter Type: This is the data type of the formal parameter.

   Returns: method to return to the calling program data after it is finished.

   Return Value Type: data type methods to return results.

   Arguments: the actual data format of the parameters passed to the function when calling the method.

  3. The method of calling

    Only method is called, will be executed, the analysis method call is as follows:

  Note: 1. No specific return value, the return value type denoted by the keyword void, then the function's return statement can be omitted if the last line.

     2. defined method, the method should return the results to the caller, the caller referred to treatment.

     3. In the same class, all methods can call each other directly, not new to instantiate the object.

     The method can only call the method, the method can not be defined within the method.

  The method of overload (overload)

    Overloads (overload): refers to a class, there is a method to allow more than one of the same name, as long as they have different number of parameters or parameter types can.

E.g:

// Method plurality of the same name if you want to co-exist in a class, these types of data must be the same method on the number of parameters or different parameters 
public  int the Add ( int X, int Y) {
     return X + Y; 
} 
public  Double the Add ( int X, Double Y) { // parameters of different types of 
    return X + Y; 
} 
public  int the Add ( int X, int Y, int Z) { // number of different parameters 
    return X + Y + Z; 
} 
public  Double the Add ( Double Y, intX) { // the ratio of 2, a different order, but also different types of parameters 
    return X + Y; 
}

    Overload features: the return type independent, but only the parameter list , and the list of parameters must be different (different number of parameters or parameter types). (Example: public double add (int x, int y) and public int add (int x, int y) does not constitute a heavy load)

          When you call, depending on the method parameter list to distinguish

Example: Write a program that defines three overloaded methods and calls. Method named mOL. Requirements: The method receives a three parameter int, int two parameters, a parameter string, respectively, performs a square operation and outputs the result, and outputs the multiplication result, the output character string information, the main class and main () method three methods are invoked with the parameter difference.

public class Test{
    public static void main(String args[]){
        Test t = new Test();
        t.mOL(2);
        t.mOL(2,3);
        t.mOL("重载方法mOL");
    }

    public void mOL(int i){
        System.out.println(i * i * i);
    }

    public void mOL(int x, int y){
        System.out.println(x * y);
    }

    public void mOL(String s){
        System.out.println(s);
    }
}

Print Results:

 

Example: definition of three overloaded method max (), a first method of selecting the maximum value of the two values ​​int second method seeking to recover a large value of two double values, the third method of seeking three double the maximum values, and calls three methods, respectively.

public  class the Test {
     public  static  void main (String args []) { 
        the Test T = new new the Test (); 
        t.max ( . 1, 2 ); 
        t.max ( 1.3, 1.4 ); 
        t.max ( for 1.5, 16, 0.17 ); 
    } 

    public  void max ( int X, int Y) {
         IF (X> Y) { 
            System.out.println ( "maximum is:" + X); 
        } the else { 
            System.out.println ( "maximum is : "+  Y);    
        }
    }

    public void max(double x, double y){
        double res = 0;
        if(x > y){
            res = x;
        }else{
            res = y;
          }
        System.out.println("最大值是:" + res);
    }

    public void max(double a, double b, double c){
        double res = 0;

        if(a > b){
            if(a > c){
                res = a;
            }else{
                res = c;    
            }
        }else{
            if(b > c){
                res = b;
            }
        }
        System.out.println("最大值是:" + res);
    }
}

Print results

Guess you like

Origin www.cnblogs.com/su-peng/p/11788594.html