Encapsulation of object-oriented features
What is object-oriented encapsulation?
1) Hide implementation details and expose only interfaces (behavior)
We usually need to turn on the TV. We only need to click the power button to turn on the TV, but we do not need to care about the specific startup behavior of the TV
2) Multiple objects with the same properties and behavior can be encapsulated into a single class
For example, xiao Ming and Xiao Hong both have the same learning behavior, such as name and age, so we can encapsulate these attributes and behaviors into a student class
3) Privatization of attributes,
Use the private keyword to decorate properties that cannot be accessed directly by the outside world. Properties are usually defined as private in the workplace and can be accessed by providing setter and getter methods.
Benefits: 1. Hide attributes 2. Verify the validity of attribute data.
Question: When defining methods, do we prefer to use private or public decoration?
Make it public if I’m defining a method that needs to be used by the outside world, and make it private if I’m just using it inside my class. All definition methods take precedence over private.
Note: you can’t use the private keyword for class. You can only use the public or default modifier. If you define a class for someone else to use, if you privatize it and make it unavailable to the outside world, the class is meaningless
4) Privatize constructors
Encapsulates the core
Key words:
Public: indicates that the data member and member function are open to all users and can be directly invoked by all users
Private: Can’t access anywhere except this class
How do I access private attributes? Setter methods, which set the value of a property, and getter methods, which get the value of a property, are usually called first and then the getter
Note: Each method defined in Java does only one thing (single responsibility)
Inheritance of object-oriented features
1) The concept of inheritance
A subclass inherits its parent (superclass) on top and its subclass on bottom through the extends keyword. Subclasses can reuse code by using non-private attributes and behaviors of their parent class.
2) The behavior of a subclass using its parent class
The parent class
/** * @description TODO * @author caojie * @date 2020/7/21 14:10 * @version 1.0 * Person is a parent class: **/ public class Person {private String name; public void setName(String name) { this.name = name; } public String getName() { return name; } public void eat(){system.out.println (name+" eating "); }}Copy the code
A subclass
/** * @description TODO * @author caojie * @date 2020/7/21 14:12 * @version 1.0 * Student There are learned behaviors * extends, which extends the behavior and properties of the parent class * The parent class is usually defined first and then the child class * When the extends Person is removed, an error is reported by compilation. **/ public class Student extends Person{private int stuAge; public int getStuAge() { return stuAge; } public void setStuAge(int stuAge) { this.stuAge = stuAge; } /** * study(){public void study(){eat();} /** * study(); // System.out.println(name+getName()+" learning. i am "+stuAge); System.out.println(getName()+" learning. i am "+stuAge); }}Copy the code
The test class
/** * @description TODO * @author caojie * @date 2020/7/21 14:22 * @version 1.0 **/ public class TestStudent {public static void main(String[] args) { Student liukai = new Student(); liukai.setStuAge(19); liukai.setName("LiuKai"); liukai.study(); }}Copy the code
3) Final keyword
The keyword is both an adjective and a modifier.
The final result of a basketball game is 99:98
In Java, final is a modifier that can modify types, attributes, methods, local variables, and parameters