object-oriented
Macroscopically, we classify through the thinking mode of classification, and then think about the problem of heap classification separately.
Thought: Abstract!!
Object-oriented Programming OOP
Organize code in the form of classes and encapsulate data in the organization of objects
Encapsulation, inheritance, polymorphism
1. Static and non static methods
class Student(){ public static void a(){ } public static void b(){ } } // a is static, b is static // b() can be called in a(), both of which exist when the class is created // a is static, b is not, // b() cannot be called in a(), because b() exists only when instantiated, and a already exists // a is not static, b is not static //b() can be called in a(), both of which exist at the same time when instantiating.
Value passing and reference passing:
a= 1; Pass in the function. The value of a is passed in instead of the variable name of A,
Pass person.name into the function. The reference name of this variable is passed.
2. Create and initialize objects
When using new to create, in addition to allocating memory space, the default initialization of the created object and calling the constructor in the class.
Construction method features: it must have the same name as the class. It must have no return value and write void
View the precompiled class file in the IDEA:
Open structure and module, import the out folder under the add directory, and you can see the compiled class file
After the parameterized construction is defined, if you want to construct without parameters (return null), you must write the method of parameterized construction again. (constructors can be overloaded)
Shortcut keys: directly generate parametric and nonparametric structures: Alt + Insert,
3. Memory analysis
4. Packaging
Property private, get/set
- Improve security and protect data
- Implementation details of hidden code (judgment can be added in get and set)
- Unified interface access
- System maintainability increase
Shortcut keys: Alt + Insert to generate get() and set()
public class Student{ //Property private private String name; private int id; private char sex; //The get set method is used to manipulate private properties public String getName(){ return this.name; } public void setName(String name){ this.name = name; } }
5. Inherit extensions
object, super, rewrite three blocks
1. object
Shortcut keys: View inheritance tree: Ctrl + H
Four modifiers
public, protected, default, private
Student should inherit the attribute of Person,
- The property of Person is public
- The property of Person is private, but the provided method can be accessed (get, set)
All classes in Java implicitly inherit the object class.
private methods and properties of parent class cannot be inherited!!!
Private properties cannot be inherited
class Student extends Person{ this.name; //Call your own name super.name;//Call the name of the parent class }
2. super
The constructor that calls this() must also be placed on the first line (the super construct and this construct cannot be called at the same time)
public class Person{ public Person(){ } } //another file public class Student extends Person{ public Student(){ super();//The constructor of the subclass calling the parent class is hidden. If you want to construct it explicitly, it must be placed in the first sentence!!! } }
3. Rewrite method
Shortcut keys: Alt + Insert, @ override
- With static, the B object calls the method of class B, so the output is related to the data type defined on the left.
class B{ public static void test(){//Note that both are static methods sout("B"); } } class A extends B{ public static void test(){ sout("A"); } } class Application{ psvm{ A a = new A(); a.test(); B b = new A();//Important: the reference of the parent class points to the child class b.test(); } } >>Output results a.test()by A, b.test() by B
- There is no static and the method is rewritten. The object b calls the object method when new A(), so the output is the object method of new A().
class B{ public void test(){ sout("B"); } } class A extends B{ @Override public void test(){ sout("A"); } public void test2(){ sout("just A not B"); } } class Application{ psvm{ A a = new A(); a.test(); B b = new A();//Important: the reference of the parent class points to the child class b.test(); } } >>Output results a.test()by A, b.test() by A b.test2()//Cannot access because B is still an object of type B.
- Summary: the actual type of an object is determined
When B is created, although new A(), A inherits from B, and B transforms upward to B(), but uses the object method when new A(). If it is A method that does not exist in the parent class B (), such as test2(), B cannot access it.
6. Polymorphism
Polymorphism: the reference of the parent class can point to the implementation of the child class
Student s1 = new Student();//The actual type of an object is determined Person s2 = new Student();//The reference type that can be pointed to is uncertain. The reference type refers to the variable on the left, and the reference of the parent class can point to the variable of the child class
As summarized in Fig. 5:
s1 is a Student class. The methods that can be called are their own or inherited methods.
s2 is a Person class, which can point to subclasses, but cannot call methods unique to subclasses.
Objects can point to those methods. It mainly depends on the object type on the left, which has little to do with the right.
Polymorphism is the polymorphism of methods, and there is no polymorphism of attributes
7. instanceof keyword type conversion
- Used to determine whether a class is another class
sout(s1 instanceof Person);//Return True
-
Forced transformation (downward transformation)
Like the basic type forced conversion, the conversion from high to low requires forced conversion, and the conversion from low to high does not require forced conversion (8 bytes to 4 bytes, 4 bytes to 8 bytes directly)
Person s1 = new Student();//Conversion from low to high is not required Student s2 new Student(); /* Person : run() Student: run(), go() */ s1.go();//Can't execute. You need to cast s1 into student class before you can use go method. ((Student) s1).go();//Feasibility and execution
Subclass to parent: (transition upward)
Person person = s2;//Subclass to parent class will lose method (similar to precision problem)
8. Static summary
-
Already
-
Static code block, which is executed when the class is loaded,
public class Student{ { sout("Anonymous code block");//It is also executed when the class is loaded the second time } static { sout("Static code block");//This is executed first and only once } public Student(){ sout("Construction method"); } } >>(order) Static code block,Anonymous code block, Construction method
-
The class modified by final cannot be inherited, which is equivalent to being broken.
9. Abstract class
public abstract class Person{ public abstract run(); }
You can't new abstract classes. You can only implement them by subclasses and restrict subclasses
Abstract methods must be in abstract classes
10. Interface (only specification)
An interface is a specification that defines a set of rules. Essence? interface
public interface interfacename{ //Return value type method name (parameter) void run(String name);//The public abstract in front of the method can be omitted //Only public abstract methods can be written } public class xxxImpl implements interfacename{ //Specific implementation, rewrite the interface method @Override }
11. Internal class
A java file can only have one public class, but can have multiple classes
- Inner class
public class Outer { private int id =10; public void out(){ System.out.println("outer method"); } class Inner{//Define internal classes public void in(){ System.out.println("inner method"); } public void getID(){//The inner class can access the data and methods of the outer class System.out.println(id); } } } //another files public static void main(String[] args) { Outer outer = new Outer(); Outer.Inner inner = outer.new Inner();//Definition of inner class inner.in(); }
-
Static inner class
The definition of the internal class is class static Inner {}, and the id value cannot be accessed because the id does not exist when the class is created,
-
Local inner class
public class Outer{ public void method(){ class Inner{//Class in method } } }
-
Anonymous class
class Apple{} //use: new Apple().eat();//Directly call the method of the class