java-based object-oriented package and fifth chapter

  . A method:

     public static void main(String[] args) {

     

     }

    General definition of criteria:

      Parameter: Usually the amount of uncertainty or varying amounts defined parameter // circle radius position, length and width of the rectangular array transfer is uncertain

      Return Value Type: the type of the operation result as the return value type // circumference of a circle, the area of ​​a circle, an element returns an array

 

      public static int getArea(int width,int length){

      return width * length

      }

      public static String getEle(String[] arr){

      return  arr[arr.length/2];

      }

    

    1. 

      public static void main(String[] args) {

       int i=3;

        method1 (i); // corresponds method1 (3); basic types only traditional values, parameter changes do not affect the argument

        System.out.println(i);

      }

      

       public void method1(int i){

           i+=3;

      }

    

    2.

    public static void main(String[] args) {

     int[] arr={1,2,3,4}

       method2(arr);

    }

   

   

    public void method2 (int [] arr2) {// int [] arr2 = arr; copy key, the address value to the arr arr2, arr, and point to the same array space arr2

       // method to arr2 the elements do affect the operation of arr

    }

 

    3.

      public static void main(String[] args) {

        ArrayList<String> al=new ArrayList<String>();

        al.add("3");

        al.add("4");

        al.add("5");

 

        method3(al);

      }

      public void method3 (ArrayList <String> al2) {// ArrayList <String> al2 = al; al al2 and uses a collection of

         Al impact on the elements in the collection will be made operational methods //

      }

   b object-oriented: Encapsulation

     class Goods{

     private String goodsName; // private use on the member of the class attribute can not be outside the direct access object properties.

                              // In order to ensure the correctness of the assignment properties, such as: the price can not assign a negative value

     private double goodsPrice;

 

        public Goods () {// empty argument constructor, if you do not write any constructors, there is a default

 

        }

 

        public Goods(String goodsName,double goodsPrice){

        this.goodsName=goodsName;

        this.goodsPrice=goodsPrice;

        }

 

 

        public String getGoodsName(){

        return goodsName;

        }

        public void setGoodsName(String goodsName){

        // goodsName = goodsName; // Local Variables Local variables =

        this.goodsName = goodsName; // this order to distinguish between members of the same name and local variables

        }                            

 

        public double getGoodsPrice(){

        return goodsPrice;

        }

        public void setGoodsName(double goodsPrice){

        // goodsName = goodsName; // Local Variables Local variables =

 

        if (goodsPrice <0) {// robustness assurance procedures

                System.out.println ( "dear user, commodity prices can not be negative ah!");

        return;

        }

        this.goodsPrice = goodsPrice; // this order to distinguish between members of the same name and local variables

        }

 

     }

 

     public static void main(String[] args) {

     // 1. create a new object

     Goods g = new Goods ( "Australian crayfish", 300); // constructor will find the corresponding parameter of the assignment as a member variable

     String goodsName=g.getGoodsName();

     String goodsPrice=g.getGoodsPrice();

       

        g.setGoodsName ( "Australia emperor shrimp"); // this case Representative setGoodsName () method is the object reference points g (AU emperor shrimp)

                                     // corresponding to this = g;

        String goodsName=g.getGoodsName();

     }

 

8. String Class operation (will check API)

  a.String class

    Conversion:

      1. The character array <-> String

        String(char[] char)

        char[] toCharArray()

      2. case conversion

        toUpperCase()

        toLowerCase()

    Judgment function:

      1.equals (string): the same content compare two strings, for example: comparing the user name, password and the password comparison is repeated

      2.euqalsIgnoreCase (): Compares two strings ignores case, for example: Code

      

      3.contains (String str): determining whether the character string contains (a character string in the adjacent composition) specified substring 

      

      4.startsWith (String str): Screening // to specify surname Zhang, Ouyang ...

      5.endsWith (String str): Screening specified file extensions // end with .txt, ending with .torrent

    

    Acquisition function:

      1.charAt (int index) // Gets the specified character index, the operation of a single character

      

      2.indexOf (String str) // Returns the index of the first character string, or -1 if no

      3.lastIndexOf (String str) // backwards lookup to find the subject for the first time, look for the index being returned, also did not find returns -1 for example: name intercepted file (not including the extension) 

    

      4.substring (int start, int end) // taken range [start, end-1] characters

    other functions:

      1.Trim () // // remove the string ends, for example, a user name spaces

      2.String [] split () // We generally, and spaces as a delimiter, this character string is cut   

  3.byt [] getBytes: converts a string into a byte array according to a certain coding table

  4 "\ r \ n" .getBytes:. Newline.

 

 

  

  b.StringBuilder: See above

 

9. The file operation related classes

   1. Write a string to a text file: FileWriter / BufferedWriter: write (String str)

   2. Read a character in the text: FileReader: read ()

     Reads one character array: char [] ch = new char [1024] 

                          FileReader: int length = read (char [] ch) // read the data into an array of characters

                                                               // represents the effective number of characters to read

   3. a read line: BufferedReaer: readLine () // does not contain a line feed, return null read end

 

   4. time to write a newline: BufferedWriter: newLine () // This method of cross-platform, the platform will be different depending on the selected line breaks

 

   5. A copy of the document are five ways (go to practice under)

   

   close (): notification system and the release of documents related resources.

   

   Difference close () and flush () method:

       flush ():. flush the buffer stream object can continue to use.

  close (): first flush the buffer release notification system resources, not in the stream object.

   

   Keyword static: is a class member variables and member methods modified.

   

10.static features:

       1. be shared by all objects.

  2. You can use the class name called.

  3. Static load precedence over objects

  4. With the load and load type.

 

static precautions:

    Takes precedence over the object, loaded with class and loaded, it takes precedence over any object, all it did not object.

 

Static methods:

    You can call a static member variables.

Members can call the static method.

You can not call non-static member method.

You can not call non-static member variables.

Static methods can only call the static member.

    

Non-static method:

    Member variables can be called static member methods.

You can call non-static member variables, non-static member methods.

 

Static advantages and disadvantages:

    A: Static advantages:

    1. Shared data object to provide a separate storage space, space saving.

2. The class name can be invoked directly, you do not have to create an object heap memory

3. accessed by class name, is relatively easy to create Object Access members.

B: Disadvantages:

    Access appears limitations (can only be static access);

 

application:

Math class is a class of commonly used mathematical operations, all members of the class are static modification, it can be accessed directly through the class name.

  Math contains some basic operation method

  static double PI;

 

  static double abs (double a): returns the absolute value

  static double ceil (double a): rounding up

  static double floor (double a): rounded down

  static double round (double a): rounding

  static long max (double a, double b): two double return values ​​larger one (min Method as);

  static double pow (double a, double b): returns the first argument of the second argument power.

  static double random (): returns a random number greater than zero and less than a

Guess you like

Origin www.cnblogs.com/haizai/p/10964333.html