Class name as parameter and return value (at)

* Anonymous inner classes essence

  * essentially: an inherited class or subclass that implements the interface anonymous object

* details anonymous inner classes

  * anonymous inner classes acceptable polymorphic form of

    `` `Java
    Inter new new I = Inter () {
      @Override
        public void method () {
            
        }
    }
    `` `

* anonymous inner classes call a method

  ` `` Java
  interface Inter {
      void method ();
  }
  
  class the Test {
      public static void main (String [] args) {
          new new Inter () {
              @Override
              public void method, () {
                  System.out.println ( "I am the anonymous inner class");
              }
          } .method (); // call the direct method
      }
  }
  `` `

### 2.4 anonymous inner classes used in the development of the (application)

* anonymous inner classes use in development

  * When they find a method requires, subclass object interface or abstract class, we can pass an anonymous internal class past, to simplify the traditional code

* sample code:

  `` `Java
  interface Jumpping {
      void Jump ();
  }
  class cat {Jumpping the implements
      @Override
      public void Jump () {
          System.out.println (" cat can jump up ");
      }
  }
  class dog the implements Jumpping {
      @Override
      public void Jump () {
          System.out.println (" the dog can jump ");
      }
  }
  class JumppingOperator {
             public void Method (Jumpping J) {// new new Cat ( ); new new Dog ();
                 j.jump ();
             }
  }
  Class JumppingDemo {
      public static void main (String [] args) {
          // requirements: Create Object interface operation class method calls the method
          JumppingOperator new new JumppingOperator Jo = ();
          Jumpping J = new new Cat ();
          jo.method ( J);
  
          Jumpping J2 = new new Dog ();
          jo.method (J2);
          System.out.println ( "--------");
  
          // simplified anonymous inner classes
          jo.method (new Jumpping ( ) {
              @Override
              public void Jump () {
                  System.out.println ( "cat can jump up");
              }
          });
                  simplified anonymous inner classes //
          jo.method (new new Jumpping () {
              @Override
              public void Jump () {
                  System.out.println ( "the dog can jump");
              }
          });
      }
  }
  `

## 3. Common the API

### 3.1 Math (application)

*. 1, Math class overview

  * Math method comprising performing elementary operations

* 2, the process is called Math

  * no Math class constructor, but the interior of the method are static, it can ** the class name to call **

* 3, commonly used methods of the Math class of

  | method name method name | description |
  | -------------------------- -------------------- | ----------------------------- ----------------- |
  | Public static int abs (int a ) | return parameter absolute value |
  | public static double ceil (double A) | returns a minimum value greater than or equal to double the parameters, an integer equal |
  | Floor public static double (double A) | returns the maximum value is less than or equal double parameter, an integer equal |
  | public static int round (a float a) | returns the closest parameter in accordance with the rounding int |
  | public static int max (a int, int B) | two return int the larger the value of |
  | public static int min (int a, int b) | returns the smaller of two values int values |
  | public static Double POW (a Double, Double b) | a b-th power returns value |
  | public static double Random () | return value of double positive, [0.0,1.0) |

### the System 3.2 (application)

* System class common method of 

| method name | Description |
| ------------------------------------- --- | ---------------------------------------------- - |
| public static void Exit (int Status) | terminate the currently running Java virtual machine, a non-zero indicates an abnormal termination |
| public static Long currentTimeMillis () | returns the current time (in milliseconds) |

* Sample Code

  * demand : 1-10000 console output, calculating how many milliseconds code do 

  `` `Java
  public class SystemDemo {
      public static void main (String [] args) {
          // Get the time of the start node
          long start = System.currentTimeMillis ( );
          for (int I =. 1; I <= 10000; I ++) {
              System.out.println (I);
          }
          // Get the time point after the end of the code run
          Long End = System.currentTimeMillis ();
          System.out.println ( "Total time:" + (end - start) + " msec");
      }
  }
  `

## method # 3.3 Object class toString (application)

* Object class Overview

  * Object is the root of the class hierarchy, each class as a superclass may Object. All classes are directly or indirectly inherit from this class, in other words, this class has a method, all classes will have a

source * Viewing Method ways

  * Select the method, press Ctrl + B

mode * rewrite toString method

  * 1. Alt + Insert select toString
  * class 2. in a blank area, right -> Generate -> toString selected

action toString method *:

  * good format, attribute values and more convenient display object

* sample code:

  Java `` `
  class Student the extends Object {
      Private String name;
      Private int Age;
  
      public Student () {
      }
  
      public Student(String name, int age) {
          this.name = name;
          this.age = age;
      }
  
      public String getName() {
          return name;
      }
  
      public void setName(String name) {
          this.name = name;
      }
  
      public int getAge() {
          return age;
      }
  
      public void setAge(int age) {
          this.age = age;
      }
  
      @Override
      public String toString() {
          return "Student{" +
                  "name='" + name + '\'' +
                  ", age=" + age +
                  '}';
      }
  }
  Public class ObjectDemo {
      public static void main (String [] args) {
          Student Student new new S = ();
          s.setName ( "Brigitte Lin");
          s.setAge (30);
          System.out.println (S); 
          System.out.println (s.toString ()); 
      }
  }
  `` `

* run results: `

  `` Java
  Student {name = 'Brigitte', Age = 30}
  Student {name = 'Brigitte' } = 30 Age
  `` `

equals method ### 3.4 Object classes (applications)

* action equals method

  * for comparison between objects, returns true and false results of
  * example: s1.equals (s2); s1 and s2 are two objects

* override equals method scenes

  * do not want to address the value of comparison, want to combine object properties to compare the time.

* Equals method override mode

  * 1. alt + insert selection equals () and hashCode (), IntelliJ Default, all the way next, finish to
  * 2 in the blank area-based, right -> Generate -> select equals () and hashCode (), back above.

* Sample code:

  `` `Java
  class Student {
      Private String name;
      Private int Age;
  
      public Student () {
      }
  
      public Student (String name, int Age) {
          this.name = name;
          this.age = Age;
      }
  
      public String getName () {
          return name;
      }
  
      public void the setName (String name) {
          this.name = name;
      }
  
      public int getAge () {
          return age;
      }
  
      public void setAge(int age) {
          this.age = age;
      }
  
      @Override
      public boolean equals(Object o) {
          //this -- s1
          //o -- s2
          if (this == o) return true;
          if (o == null || getClass() != o.getClass()) return false;
  
          Student student = (Student) o; //student -- s2
  
          if (age != student.age) return false;
          return name != null ? name.equals(student.name) : student.name == null;
      }
  }
  public class ObjectDemo {
      public static void main(String[] args) {
          S1 = new new Student Student ();
          s1.setName ( "Brigitte Lin");
          s1.setAge (30);
  
          Student Student new new S2 = ();
          s2.setName ( "Brigitte Lin");
          s2.setAge (30);
  
          // demand: compare the contents of two objects are the same
          (s1.equals (S2)) System.out.println;
      }
  }
  
  `` `

### 3.5 of bubble sort principle (understood)

* bubble sort outlined
  * a ranking of mode, the data to be sorted in the neighboring data pairwise comparison, on the back of the large data sequentially operate on all data until all the data required to complete the sort
* If there are n data sorting , a total of n-1 time Comparative
* each comparison is completed, the next data comparison will be involved in at least one

### 3.6 bubble sort code implements (understood)

* code for

`` `Java
/ *
    bubble sort:
        a species sort mode to sort the data in adjacent data pairwise comparison, on the back of the large data,
        sequentially operating on all data until all the data required to complete sorting
* /
Public class ArrayDemo {
    public static void main (String [] args) {
        // definition of an array
        int [] = {ARR 24, 69, 80, 57 is, 13 is};
        System.out.println ( "Sort front:" arrayToString + (ARR));

        // Save 1 where, round is a control to compare times
        for (int X = 0; X <arr.length - 1; X ++) {
            // -1 to avoid overindexing, the -X- In order to increase the efficiency comparison is
            for (int I = 0; I <arr.length -. 1 - X; I ++) {
                IF (ARR [I]> ARR [I +. 1]) {
                    int ARR TEMP = [I];
                    ARR [I] = ARR [I +. 1];
                    ARR [I +. 1] = TEMP;
                }
            }
        }
        System.out.println ( "sorted:" + arrayToString (arr)) ;

    }

    // the elements in the array of rules in accordance with a specified string: [element 1, element 2, ...]
    public static String arrayToString (int [] ARR) {
        the StringBuilder the StringBuilder new new SB = ();
        sb.append ( "[");
        for (int I = 0; I <arr.length; I ++) {
            IF (I == arr.length -. 1) {
                sb.append (ARR [I]);
            } the else {
                sb.append . (ARR [I]) the append ( ",");
            }
        }
        sb.append ( "]");
        String sb.toString S = ();
        return S;
    }
}
`` `

###. 3.7 Arrays (application)

common method * Arrays of

  | method name | Description |
  | -------------------------------------- | ---------- ------------------------ |
  | static public string toString (int [] a) | returns the contents of the specified array string representation |
  | public static void sort (int [] a ) | specified array are arranged in numerical order |

* design tools

  1, modified private constructor method

  2, using public static member modified

Published 157 original articles · won praise 43 · views 90000 +

Guess you like

Origin blog.csdn.net/qq_39581763/article/details/103919183