The content is a summary of their own, just according to their own understanding to write. Reference is a beginner's tutorial.
Java is an object-oriented language.
OOP is object-oriented programming.
Encapsulation: In some classes, some properties do not want to be exposed, but we would like to provide a way to access or modify it, what to do?
Encapsulation is now the solution to this problem:
If there is a student class
public class student{
constructor, you can write a default constructor that can be written without writing and not written by default in Java
private int number;
private String name;
public void Setnumber (int number) {
This.number = number;
}
public int GetNumber () {
return this.number;
}
public void SetName (String name) {
This.number = number;
}
public int GetName () {
return this.name;
}
}
At the moment, the two attributes of the student class are private (proprietary) and we cannot use them directly:
Student stu = new Student ();
Stu.number = 4;
Stu.name = "Test";
But the method is public, and we can write or obtain these two private properties by calling these methods:
Student stu = new Student ();
Stu.setnumber (4);
Stu.getnumber;
Stu.setname ("test");
Stu.getname;
This is the encapsulation of classes, what are the benefits of encapsulation?
1. Good encapsulation can reduce coupling.
2. The structure within the class can be freely modified.
3. More precise control of member variables is possible:
The member variables of a class can be read-only or write-only.
such as accessor getxxxx (); modifier setxxxx (); With these, you can control read and write permissions
4. Hide the information and implement the details:
For example, it's a way to get a birthday in a class.
Public String Getbirthday () {
Return year + "-" + month + "-" + day;
}
public void Setbirthday (String birthday) {
Split the string, remove the date, and assign the year, month, and day
}
And when this method is called externally
This.getbirthday ();
This.setbirthday ("1990-9-9");
At this point the external is not know how to store birthday this method, the user can only invoke it.
Inherited
In Java, you can have a subclass go extend (Inherit) a parent class, and a child class can inherit only one parent class.
Subclasses inherit all the methods of the parent class.
In these methods, as long as the method name and its parameter type and quantity, only the code inside the method is rewritten, is called rewriting,
If the names of these methods are not changed, but the parameter types change, or if the number of arguments changes, or if both are changed, it is called overloading.
Overrides and overloads are part of polymorphism.
Of course it is possible that the parent class is a suction class, the subclass inherits the abstract class to add specific code to the abstract method in the parent class, and if not, the method in the subclass is declared as an abstract method, the subclass is declared abstract, and the default public.
The subclass inherits the implementation of the abstract method (you can also continue with the above scenario).
Polymorphic
A tool in which different uses produce different results or behaviors.
More for interfaces?
Where overrides and overloads are an important part of polymorphism.
Overrides are inherited and occur between two classes (parent-child class)
Overloading occurs between a class.
Java oop (some of their own understanding, and did not unfold very thin)