1, Three characteristics - encapsulation, inheritance and polymorphism
(1) Encapsulation:
Instead of directly assigning values to object properties, you can assign values and take values by setting the set/get method
Benefits: it hides the details of internal implementation and improves security and reusability
Steps: 1. Change the assignment and value to set/get method
2. Attribute privatization
The code is as follows
class Book{ private String author; private double price; //set + the initial letter of the attribute name is capitalized, and the parameter is consistent with the attribute name public void setAuthor(String author) { this.author = author; } //The set + attribute name is capitalized, and the return value type is consistent with the corresponding attribute public String getAuthor() { return author; } }
Note: encapsulation encapsulates attributes into methods, so all methods can be used. You can try to ensure the correctness and rationality of data by passing parameters and setting access conditions in the method body.
(2) Inheritance:
If the relationship between two classes satisfies is a, inheritance can be used. Subclasses can inherit the properties and methods of the parent class
Keyword extends
Advantages: simplify the amount of code and improve reusability
FAQ: can inheritance have multiple fathers? No (single inheritance)
Can there be multiple levels of inheritance (father, father of father)? can (indirect inheritance)
The code is as follows
//Grandfather class public class GranderFather { String nameString; String ageString; String tuding; String moneyString; public void eat() { System.out.println("having dinner"); } }
public class Father extends GranderFather{ //The father class inherits the grandfather class }
//The son class inherits the father class and has its own unique attributes public class Son extends Father{ String hairString; }
//Test class, instantiate the child class, and call the methods and properties of the parent class inherited by the parent class, as well as its own properties and methods public class Test { public static void main(String[] args) { Son son =new Son(); son.ageString="18"; son.eat(); son.moneyString="2000"; son.hairString="red"; } }
Note: cases that cannot be inherited:
1. Characteristics and behavior of private enterprises
2. Construction method - it is best to measure the belt reference structure
In addition, in any case, there is an Object class (ancestral class)
Supplement 1: access modifier
Supplement 2: Rewriting
Rewriting overview: 1. Subclass methods are completely consistent with parent methods: return value type, method name, parameter list
2. The child permission is greater than or equal to the parent permission
Application scenario: when the method of the parent class is not applicable to the child class, the child class can override the method of the parent class;
So when you call, what is the tone Class override method
Supplement 3: super keyword (and its similarities and differences with this)
The usage of super keyword is similar to this:
this: current object
Usage:
This. Attribute: call the attribute of the current object. If the current object does not have this attribute, the attribute of the parent class can be adjusted according to inheritance This. Method (): ibid
this(): call the construction method of the current object
super: parent object
Usage:
super. Attribute -- the attribute of the calling parent class
super. Method () -- Method of calling parent class
super() -- construction method of calling parent class
(3) Polymorphism (key):
All changes are inseparable from its origin: the parent class reference points to the child class object, that is, polymorphism is formed
The premise of polymorphism: there must be inheritance
Polymorphism mode 1: direct parent class reference points to child class objects
(the parent class reference points to the subclass object, and the subclass override method can be called)
The parent class refers to the method that can call the parent class; If the subclass has an override method, the subclass override method is called first
A parent class reference cannot invoke methods unique to a child class
The code is as follows (= = = = separate different classes)
public class Animal { public void jiao() { // Method stub automatically generated by TODO System.out.println("There are too many kinds of calls"); } } ======================================= public class Dog extends Animal{ @Override public void jiao() { System.out.println("Woof, woof~Good brother, take me home"); } } ======================================= public class Cat extends Animal{ @Override public void jiao() { System.out.println("cat ~Shit shoveling officer, take me home"); } } ======================================= public class Bird extends Animal{ @Override public void jiao() { System.out.println("Tweet tweet~Take me home"); } } ======================================= //Good brothers, I didn't write notes. I should be able to understand it //First define the parent class reference, and then point to different subclass objects through different input results in the loop //In this way, the parent class can not be changed. If there is anything to add, add individual animals to sell, and generate a class inheritance, it is finished import java.util.Scanner; public class Test { public static void main(String[] args) { // Method stub automatically generated by TODO Scanner scanner = new Scanner(System.in); System.out.println("Please enter the pets you want to buy: 1 cat, 2 dog, 3 bird"); Animal animal; int i; int condition = 0; while (condition <= 3) { i = scanner.nextInt(); switch (i) { case 1: animal = new Cat(); animal.jiao(); condition++; break; case 2: animal = new Dog(); animal.jiao(); condition++; break; case 3: animal = new Bird(); animal.jiao(); condition++; break; default: System.out.println("Don't make trouble if you don't buy it, poor man"); condition++; break; } } } }
I haven't finished the summary yet. I'll go back and add [dog head saves life]