I. Overview
1, object-oriented is a kind of thought, let us from the executor into the conductor, the executor is the process-oriented, the conductor is object-oriented. For example, people open the refrigerator door, open the refrigerator door This action should belong to the door and not people, the refrigerator itself the most clear door should be how to open, people just call the refrigerator this action
2, object-oriented development Example 1. Computer is broken, find a repair computer, we call repair computer This person repair the function of the computer, he does not need to know how to repair; 2. The boss of the company to complete software development ...
3, the development of the time to think whether Java has realized this function, to find this object to use, if not, oneself build an object to use, oneself can use others also can use, development is to find object, can't find on Build object, then use object.
4, class and object relationship, class is the description of things (attributes, behavior), the object is a real individual.
5. The difference between a reference pass and a value pass.
6, member variable and local variable difference: scope, storage location, there is no default value.
7. Object-oriented three features: encapsulation, inheritance, polymorphism.
Second, the package
1, to provide a simple interface, package details, private is a form of representation of the package.
2, the constructor can be privatized generally for security.
3, the constructor initializes the different objects, constructs the code block to unify the initialization for all objects (can refer to here the common thing in the constructor function).
Third, this
1. This represents a reference to the current object of the function in which this type of function is invoked, since the object is not defined when writing the class.
2. This is used to call each other in the constructor, only with this statement, and can only be written in the first line.
3, the method in the class can call each other, the static method omits the class name, non-static omitted is this.
Iv. Static
1, can only modify member variables and member methods, can not modify local variables.
2. As the class is loaded (creating a class object or invoking a static method of a class, the class name variable =null, not loaded) is loaded, disappears as the class disappears, and executes only once (the life cycle is longer than the non-static member variable).
3, 1. Precedence over the existence of an object 2. All object sharing 3. Class calls 4. Put in the method area or call data area
4. Non-static variables cannot be accessed in static methods; This,supper cannot be used in static methods.
5. When to use static?
For member variables, it is generally the data that needs to be shared, and for member methods, it is generally the method that does not encapsulate the unique properties of the class (that is, non-static member variables), such as the method in the tool class array.
V. Sequence of initialization
Static variables--Static code blocks (for class initialization, which can be used to assign values to static variables)--a block of code, such as a non-static member variable (to initialize the object, not to assign a static variable), to a specific constructor.
Vi. Succession
1, improve the code reusability, the relationship between the class, the parent class provides a subclass of the common things.
2, does not support multiple inheritance, there is a security risk, because when the same method is defined in more than one parent class, the subclass object calls the indeterminate call which, support multilayer inheritance, grandchildren three generations.
3, the Super Parent class object reference, if the child class and the parent class appears the same name non-private member variable, the subclass accesses the variable in this class with this, the parent class to access the variables in this class with super,super use and this is almost the same.
4, another property of the function overrides (Overrides), the child class and the parent class is identical (the subclass permission must be greater than the parent class permission), the subclass object calls, the child class's method is called, another property of the function is overloading.
5, subclass all constructors default the first line is super (), written in the first line, and this usage is the same as super (4) Call the parent class argument pass 4 constructor, parentheses off the constructor, no parentheses represent the current object and the parent class reference.
Vii. final
One drawback of inheritance is to break the encapsulation of a class, because its methods can be overridden, so there is final, the decorated class cannot be inherited (the String class is final), the adornment method cannot be overridden, the modifier variable is a constant and can only be assigned once ( As a function of the formal parameters, the function is called multiple times can be assigned a different value, this is two things. A constant letter capitalized, between words with _, the equivalent of a lock variable block, at this time also often add public static used to share.
Eight, abstract class
1, abstract methods (no method body) can only be placed in the abstract class.
2. Abstract classes and abstract methods are all modified with abstract.
3. The abstract class cannot be new because it doesn't make sense to invoke an abstract method.
4, if the subclass only covers some abstract methods, that subclass or abstract class because inherited other abstract methods, abstract class can have non-abstract method, abstract class through inheritance implementation.
5. There is no abstract class for abstract methods in Java, just to not let the instantiation.
/* Template Method design mode */
Template method: When defining a function, part of the function is deterministic, part is indeterminate, and the identified part is used in the indeterminate part, then the indeterminate part can be exposed and the subclass completed.
example: Getting the execution time of a programAbstract classgetruntime{ PublicFinalintGetTime () {intStart =System.currenttimemillis (); RunCode (); intEnd =System.currenttimemillis (); System. out. Print (End-end)/ +; }Abstract voidRunCode ();}classZi extends getruntime{voidRunCode (...) Zi Z=NewZi (); Z.gettime ();}
Nine, interface
An example illustrates the benefits of using interfaces:
CPU and motherboard, before the CPU is directly welded on the motherboard, now the motherboard manufacturers exposed pins, as long as the CPU manufacturers in accordance with this stitch CPU can be installed directly to the motherboard, so that after the computer upgrade, you can directly replace the CPU.
1, interface compilation is also a class file, the interface member has a fixed modifier, the variable is public static final, the method is public abstract.
2, the interface can not build objects, subclasses must all overwrite the method of the interface, otherwise the subclass is an abstract class.
3, the class and the class is an inheritance relationship between the class and the interface is the implementation of the relationship between the interface and the interface is an inheritance relationship.
4. Classes can inherit one class at a time and implement multiple interfaces (only multiple inheritance exists between interfaces and interfaces, and only single inheritance is supported between classes).
5, interface features: External exposure rules, improve program function expansion, reduce coupling.
Ten, polymorphic
1, overloading and rewriting are the embodiment of polymorphism, the parent class refers to the child class object, improve code reusability (incoming who run, before directing an object to do things, now command a group of objects to do things, there is a common thing in the abstract).
2, polymorphism premise, 1. There is inheritance or implementation; 2. There is an overwrite (only the parent class's reference is used to access the members of the parent class).
3, Animal a = new Cat ();//upward transformation, cat C = (cat) A;//If you want to use a cat's unique method, transition downward, similar to the conversion between int and Byte, Animal a = new Aniaml (); Cat C = (cat) A; This is wrong when cat doesn't exist yet.
4, instanceof judgment object belongs to the relationship, when you want to use the subclass unique method can be used.
Testinstanceof (new Dog ()); void // when the number of subclasses is limited, so it is more troublesome, if (a instanceof Animal) meaningless. if (a instanceof Cat) {a.catchmouse (); }Elseif(a instanceof Dog) {A.kanjia (); } }
5. Characteristics of a polymorphic (non-static) member function:
Compile time: Check if the referenced variable belongs to a class that has a calling method, no compilation failed
Run time: Check if the object belongs to a class that has a calling method, no throw exception
Simple is to compile look to the left, run to see the right.
6, Interview questions: (generally only appear in the interview, that is, the coverage will not appear on the static member, because it is static binding)
When a variable with the same name (non-static) appears in the parent class and subclass in the polymorphic case, look to the left, Fu f = new Zi (); F.num calls Fu's num, in polymorphic cases, static member functions and static variables, only look to the left.
Xi. Internal class
1. When describing things, there are things inside things with internal classes (which can usually be modified by private), which can inherit the external class directly.
2. Inner classes can directly access any members and methods of an external class, including private, external classes to access the inner class, must define an inner class object.
3. When the inner class is defined locally, you can access members in the outer class, and also hold references that can access only the final decorated local variables (member variables are allowed).
4, Anonymous internal class premise: must inherit or implement an interface, the format: New parent class or interface {define subclass content}, in fact, anonymous inner class is an anonymous subclass object.
5, anonymous inner class inside the most write a two methods, more reading is too poor.
6, so far only springjdbc rowmapper a place to use the inner class, the example is as follows:
PublicList<user>Getuserbyname (string username) {String sql="SELECT * from t_user where username =?"; Object[]params=Newobject[] {username}; List<User> users =NULL; /*implementing classes using Interfaces*/Users= Jdbctemplate.query (SQL,params,NewUserrowmapper ()); /** * Use anonymous inner class * If the Userrowmapper class is used only once, creating a single class for it is redundant, you can use anonymous classes * omit to write an implementation class*/Users= Jdbctemplate.query (SQL,params, NewRowmapper<user>() {@Override PublicUser Maprow (ResultSet RS,introwNum) throws SQLException {User User=NewUser (); User.setid (Rs.getint ("ID")); User.setusername (Rs.getstring ("username")); User.setpassword (Rs.getstring ("Password")); returnuser; } }); return(Users! =NULL&& users.size () >0) ? Users:NULL; } Public classUserrowmapper Implements Rowmapper<user>{@Override PublicUser Maprow (ResultSet RS,introwNum) throws SQLException {User User=NewUser (); User.setid (Rs.getint ("ID")); User.setusername (Rs.getstring ("username")); User.setpassword (Rs.getstring ("Password")); returnuser; } }
PS: Single case design mode
1, design patterns: Similar to "architecture", the most effective way to solve a class of problems, the framework is a large design pattern.
2, singleton design pattern: A class has only one object in memory, example: A link pool can use singleton mode, When initializing, create such as 100 connection objects, and then provide one when needed, then return to the pool after use, we use a singleton mode, is to ensure that the connection pool and only one.
3, single case three steps: 1. constructor Privatization 2. Create a Class object 3. Provides a way to get the object.
4, how to describe a thing of that class, or how to describe it, add three steps when you need the object in memory to be unique.
5, lazy and a hungry man, lazy loading, the actual use of a hungry man, lazy thread problem (in determining whether the CPU switch is empty, you can add sync. Locked, but inefficient).
6, lazy Threading Problem Solution:
class Ehanshi () { privatenull; publicstatic Ehanshi getinstance () { if(e==null) { --
New
Ehanshi (); }}}
Solution One (low efficiency, each need to determine whether to lock)classEhanshi () {PrivateEhanshi () {} Ehanshi e =NULL; Public StaticEhanshi synchronized getinstance () {if(e==NULL) {e =NewEhanshi ();} }} Resolution two, high efficiency (however, it is recommended to use a hungry man-type Simple)classEhanshi () {PrivateEhanshi () {} Ehanshi e =NULL; Public StaticEhanshi getinstance () {if(e==NULL) {synchronized (Ehanshi.class) {if(e==NULL) {e=NewEhanshi (); } }}}
Javase (5) _ Object oriented