Some of the content is made by other bloggers, and the invasion and deletion
June 12-July Daily update
Java Learning Trajectory
First, object-oriented
Classes and Objects
1. What is done with the New keyword
1) opens up new memory space for the object
2) constructor method of calling class
3) Returns the address of the generated object
Dog dog=new Dog();
Dog is a reference, new dog () generates a dog object that refers to the address of the object that the dog is pointing to
2. Construction method
1) The construction method must be the same as the class name
2) Construction method has no return value "and cannot use void for return value"
3) If you do not construct a class declaration when defining a class the Java compiler automatically adds a default constructor for the class that has no parameters and the method body is empty, and if the constructor method is declared, the compiler no longer automatically adds the default constructor method
4) You cannot explicitly call the constructor of a class, usually using the New keyword implicitly calling the constructor, which is called when the new object is
5) The constructor method can be overloaded: The method name is the same, but the parameters are different, and the corresponding method is automatically selected according to the different parameters.
3. member variables and local variables
1) member variables do not have to be assigned an initial value, default is assigned the initial value
2) Local variables must be assigned initial value
Usage of 4.static
1) Static statically variable (class member)
① it belongs to the entire class, not to an object, and is shared by all objects of the class.
② static members can be accessed directly using the class name or by using the object name.
③ static members belong to the entire class, and when the system first uses the class, it allocates memory space until the class is unloaded for resource reclamation.
2) Static Statics method (class method)
Static members in the same class can be called directly in ① static methods, but non-static members cannot be called directly.
② if the system calls a non-static variable in a static method, you can access the non-static variable by creating the object of the class and then through the object.
③ in the normal member method, you can directly access the same non-static variables and static variables.
Non-static methods cannot be called directly in ④ static methods, and non-static methods need to be accessed through objects.
static modified variable is a class variable, there is only one memory space, once the class variable is modified in other methods, the class variable will also change;
the static modification method is a class method, and when a class creates an object, it calls the class method
3) Initialization block
Normal initialization block
{ /**** ****/ }
Static initialization blocks
static{ /**** ****/ }
Encapsulation : Some of the information about the class is hidden inside the class, not allowed to be accessed directly by the external program, but through the method class provided by the class to implement the operation and access to the hidden information.
The benefits of
- encapsulation: Data can only be accessed with the specified method, and the implementation details of the class are hidden for easy modification and implementation. That is (① reduces the coupling (association) rate ② can repeatedly invoke properties in the class ③ improve security)
- implement encapsulation steps:
1) Modify the visibility of the property (private) to restrict access.
2) Sets the Read method for the property (creates the Getter/setter method).
3) in the method of reading the property, add a restriction on the property read. (Adding attribute control statements to the Getter/setter method: Judging the legitimacy of property values)
- access modifiers: Decorated properties and methods, set permissions
1) Private: accessible and used only in this class.
2) Default: can be accessed and used in this class and in the same package.
3) Protected: Can be used in this class, in the same package, in subclasses.
4) Public: used in this class, in the same package, in subclasses, in other classes.
Note: ①final-decorated classes cannot be inherited and have no subclasses.
②abstract decorated classes cannot be instantiated, and must be inherited by the quilt class. A class must be an abstract class as long as it has an abstract method, but abstract classes do not have to be abstract.
- this: Represents the current object
- inner class: The definition of a class is placed inside another class, and this class is called an inner class. (Still a standalone class, the inner class is compiled into a separate. class file after compilation, but preceded by the class name and the $ symbol of the outer class.)
Benefits of using internal classes:
1) Internal classes can be very good to implement the hidden, general non-internal class, is not allowed to have private and protected permissions, but the inner class can
2) The inner class has access to all the elements of the perimeter class (if the inner class is declared static, it cannot casually access the member variables of the outer class, at which point the inner class can access only the static member variables of the outer class.) )
3) is the implementation of multiple inheritance
4) You can avoid modifying the interface and implementing the invocation of two methods of the same name in the same class.
The inner class is divided into four types:
① member Inner class: The most common inner class "note: After defining a member inner class, you must use an external class object to create an inner class object, rather than go directly to the new inner class object. 】
public class Outer{ int outi=0; private int prii=0; private class Inner{ int inni=0; outi++; prii++; public void seeOut{ System.out.println(outi+prii); }} }
where any variable (outi,prii) in the outer class can be called in the inner class, and the inner class's variable (Inni) cannot be called in the outer class
② method Inner classes: Putting classes inside a method
(1), the method inner class can only be instantiated within a method that defines the inner class, and it cannot be instantiated outside this method.
(2), the method inner class object cannot use the non-final local variable of the method in which the inner class resides.
Publicclass outer{public void go () {final int outi=0; private class inner{public void seeout () {system. Out.println (Outi); }}}inner in=new Inner (); In.seeout (); public static void main (string[] args) {Outer out = new Outer (); out.go ()}}
This code is for your own understanding
③ static inner class (decorated by the static keyword)
1) Static inner classes cannot access non-static members of external classes directly, but can be accessed through the "New external Class (). Member" method
2) If the static member of the outer class is the same as the member name of the inner class, the static member of the external class can be accessed through the class name. static member; If the static member of the outer class is not the same as the member name of the inner class, it can be called directly through the member name.
3) When creating an object of a static inner class, an object of the outer class is not required and can be created directly: Inner class object name = New inner class ();
public class Outer{ static class Inner{}}public class{ public static void main(String[] args){ Outer.Inner n = new Outer.Inner(); }}
The code above shows that the static meaning is that the inner class can access it just like any other static member without an external class object.
④ anonymous inner class: When an anonymous inner class is used in a program, an object of that class is often created directly where the anonymous inner class is defined.
new ParentName(){...// 内部类的定义}
An anonymous inner class is an inner class that has no name. Under what circumstances do I need to use anonymous internal classes? Using an anonymous inner class is appropriate if some of the following conditions are true:
• Only one instance of the class is used.
• The class is used immediately after the definition.
• The class is very small (sun recommended is below 4 lines of code)
• Naming a class does not make your code easier to understand.
Here are a few principles to keep in mind when using anonymous inner classes:
• Anonymous inner classes cannot have a constructor method.
• Anonymous inner classes cannot define any static members, static methods.
• Anonymous inner class cannot be public,protected,private,static.
• Only one instance of an anonymous inner class can be created.
• An anonymous inner class must be behind new, implementing an interface with its implication or implementing a class.
• Because the anonymous inner class is a local inner class, all restrictions on the local inner class are applied to it.
There is no specific use of anonymous internal classes, it is not very well understood, some places are biased please indicate
Java Learning track "Object oriented" (Continuous update)