1 // location package statement declares the class, will create the appropriate folder, storage class 2 // package package name; there can be only one, can not write more than 3 package org.java; 4 5 // Import statement 6 // a class might need another class declaration of class ui want as their own members or repulsive local variables 7 // if the two classes are not the same package, you need to import import statements 8 // import class where the package name . 9 Import classes in java.util *;. // contains class data structure 10 Import the java.io. *; // contains all the input / output class . 11 Import the java.sql *;. // database containing classes 12 is Import the java.net . *; //Class contains all functions for network 13 is Import the java.lang *;. // contains substantially all classes 14 15 / * 16 OOP: There are three characteristics, encapsulation, inheritance, polymorphism . 17 18 is Java-based summary 19 1: a class that implements 20 comprising: a class declaration and body based 21 1.1: class declaration: 22 in the following format: class name {class 23 class body 24 } 25 1.2: body type: 26 declare variables 1.2.1, characterize the properties 27 defines the method 1.2.2, characterize function 28 declaration section variable is declared: variable member 29 parameters of the method body as variables and methods: local variables 30 31 is Access: 32 Class access: 33 is public: other classes in the package can access the class 34 Default: This class can only be accessed in this package 35 access members: 36 public: have access to 37 [ Private: only in this access class 38 is protected: in this package and subclass can be accessed, the other access error 39 default: in this package can be accessed 40 * / 41 is public class the Test { 42 is // the Test class name: legal identifier: letters, underscores, numbers and dollars, the first one character can not be numeric, and the Latin alphabet the first letter capitalized; 43 44 // declare variables: member variables -> valid throughout the interior of the class 45 // member variables divided into: instance variables and class variables (static keyword modification, also referred to as static variables) 46 //Method parameters and variables declared in the body of the method: the local variable -> The method is only valid in the 47 48 // methods: Method and instance method of class 49 @ Example: There is no static keyword, 50 // class methods: adding the method that is based on the static keyword 51 is 52 is 53 is // type variable: basic type integer, floating point, character, etc., and an array of objects, a reference type interface 54 // basic types of data: 55 // logic type: Boolean 56 is // integer types: byte, Short, int, Long; // respectively allocated, 1,2,4,8 byte 57 @ character type: char; // 2-byte 58 // float point type: a float, Doble; // respectively allocated 4,8 bytes 59 //Note: a low-level to high-level variable assignment variable, the type will be automatically converted 60 // however when the high level to the low level variable assignment variable, the type of conversion to be displayed 61 is int width; // instance variables 62 is int height; // instance variables 63 is String name; // instance variables 64 static int CNT; // class variables | static variable 65 66 // class set comprises a method of integrity, GET method, methods and functions toString method, structure method (no parameters, there are reference) 67 68 // function method 69 // definition of the method comprising: the method declaration and the method body 70 // format: method statement { 71 // method body 72 // } 73 // method declaration: including the method name and method return type 74 // follows int (return type) Area (method name) 75 // example method: 76 // can operate instance variables, class variables can be operated; 77 // can call instance method can also be called class methods; 78 public int Area ( int width, int height) { // int height local variables 79 // member variables Hide: if the name of the local variables and member variable name the same, then this method hides the member variable 80 // you can use this name member variables, member variables so effective. 81 int tmpdata = 0; // local variables, only after initialization, it can be used, otherwise an error 82 ++ CNT; // example method of operating a variable class 83 System.out.println (tmpdata); 84 the this .width = width; 85 the this .height = height; 86 return the this .height * the this .width; 87 } 88 // Polymorphic comprising: two kinds: rewritable (overload) and heavy (override) 89 // overload: the method of the same name, but different parameters or a different number of types of parameters 90 // overloaded method area -> different types method 91 / / rewrite: override the parent class or superclass is called a method of covering 92 public Double Area ( Double X, Double Y) { 93 X = width; // the this may be omitted, the operation instance variables 94 Y = the this .height + 1.0 ; 95 return X * Y; 96 } 97 98 // class methods: static keywords plus 99 // class methods can operation class variables, instance variables can not be operated 100 // only calling a class method, can not call instance method 101 public static int COUNT () { 102 CNT ++; // class class method of operating variables 103 Test.cnt ++; // or by the name of the class class variable operation 104 return CNT; 105 } 106 107 108 // constructor: No reference 109 // name and the same as the class name of the method 110 public the Test () { 111 Super (); 112 } 113 // constructor: YES parameters (also a heavy load) 114 public the Test ( int width, int height, String name) { 115 Super (); 1 16 the this .width = width; 117 the this .height = height; 1 18 the this .name = name; 119 } 120 121 //get,set方法 122 public int getWidth() { 123 return width; 124 } 125 126 public void setWidth(int width) { 127 this.width = width; 128 } 129 public int getHeight() { 130 return height; 131 } 132 public void setHeight(int height) { 133 this.height = height; 134 } 135 public String getName () { 136 return name; 137 } 138 public void the setName (String name) { 139 the this .name = name; 140 } 141 is 142 // rewrite object inside toString () method 143 // When creating a class, if the parent is not specified, it will default to object class 144 // all methods will inherit the object inside the 145 // so here toString method to override the parent class inside the object class 146 // rewrite 147 @Override 148 public String toString () { 149 return "Test [width=" + width + ", height=" + height + ", name=" + name + "]"; 150 } 151 152 153 154 155 156 }