static static
The problem with this graph is how many people it will print as many times and what is the country
We can define it as a static variable at the beginning, so that later it will default to the Qing Dynasty for the country column.
And the content corresponding to static is shared. In line 22, we change the country of one of them to the Republic of China, and we will find that both of them have changed.
However, we do not recommend accessing the static content set through the object, that is, a.country”Republic of China” should not be used as much as possible.
This is because static belongs to a class, try to access it through the class name.
Features:
-
data sharing
-
Belongs to a class, not an object
-
takes precedence over object generation
generic constructor, static constructor
The process of creating an object (simple)
Print order:
-
Static constructor 2. General constructor 3. Construction method -> Create object
Since the process of creating an object is after the static content is loaded, this cannot be used in static methods and static blocks
Static content is accessed using class commands
It is ok to call static things in static methods
Package and export package
Package: In fact, it is essentially a folder
You need to write package + package name in the code
Import package: import + package + class
This is the imported package package1
The third line of operation: import the imported package
No package needed:
-
in your bag
-
All content under java.lang does not need to import packages
Such as: String System.out.println
Access permissions
-
public public access
-
Default access rights, commonly used such as String name, can be accessed at will in your own package
-
private private
We can find that there is no problem in calling after defining three kinds of permissions in our own package.
If it is used in other packages, an error will occur, and only public ones can be used
What about different classes under the same package?
Private does not work
In general, public and private are used the most
getters and setters
Useful: After the member variable is declared with private, it can protect the variable from random assignment
In human terms, when you want to use private-defined member variables in other packages or different classes in the same package, you can use the method of writing getters and setters.
The following is the class that defines private
The following are different classes in the same package
The following are in different packages
getter, get data from member variable
Setter, assign values to member variables, do some protection
Note: getters and setters can be automatically generated
Idea users can right-click on the blank space, select getter and setter in generate, and then select all to generate automatically.
The eclipse user will be warned after writing private, and can directly click to generate automatically according to the content of the prompt.
Inheritance
Inheritance: subclasses can automatically own everything except the parent class private
When x is a kind of y, x can inherit y
public class class extends parent class{ }
Role: Simplify code development
The subclass extends the parent class
The following as a parent class
The following as a subclass
The extended content is what is not in the parent class
The following is to write the main method as the entry to call the defined “parent class” (actually defined as a subclass, but the child inherits the parent’s business)
See the fifth line, it is actually a little bull monster (son) not a big bull monster (father)
The ninth line is defined in the bull monster, because it is a private type, so it is not available
The following are the output results: the output results of lines 6, 8, and 11 respectively
super keyword
super: content in the parent class
this: the content in the subclass
super and this are used to distinguish the same parts of the parent class and the subclass
father:
Subclass:
result:
Summary: There is no name in the subclass, so the name in the parent class will be borrowed
So, what happens when we define the name in the subclass and the parent class remains unchanged?
Summary: If there is a defined name in the subclass, the name defined in the subclass will be used as this.name. If not, the parent class will be considered.
So, how to use both parent class and child class?
We need to use the above super.
As shown in the figure, super.name is used to represent the keyword of the parent class, so the parent class will be brought out.
In addition, super can also call the construction method in the parent class, which must be written in the first line of the subclass construction method. If the parent class has no parameters, it can be omitted, and it must be written if there are parameters.
For example, there is String a in the parentheses of hero in the fifth line of the first picture, and String a must also be in the parentheses of the tenth line in the second picture.
Override of method
Rewriting: subclass redefines the method provided by the parent class
Grammar: The methods declared by the subclass and the parent class are exactly the same
Overriding is also known as method overriding
The following first writes the method in the parent class and calls it in the subclass
Then we need to write a method in the subclass with the same syntax as the method in the parent class, the purpose is to verify the rewriting of the method
Without any changes in the parent class, the subclass is as follows
We can see that the method of the subclass is still used in the end, which is the rewriting of the method
If we want to use the methods in the parent class together, then we need to use the knowledge learned in the previous section
Just use super
Polymorphic
Polymorphism: the same object can have multiple forms
Function: unify different data types, so that programmers have super scalability
Example: Write a code for a person to feed cats and dogs
Kitten’s
puppy
people to feed
main method
Summary: We will find that every time we add a small animal, we need to add a new method of weieat to the person class. The operation is cumbersome and repetitive. If we use polymorphic knowledge, it will actually be very convenient.
Kittens and puppies inherit animal
People don’t need to feed them one by one, they just need to use the transferred animal type variable, and then find out which animal (cat or dog) it is, and execute the corresponding action
In the main method, because cats and dogs inherit animal, the subclass object can be assigned as the animal type, and then the action can be performed by humans (the eighth and ninth lines).
defined animal
little knowledge
-
Assign the object of the subclass to the variable of the parent class —> upcast
-
Assign the variables of the parent class to the variables of the subclass —> downcast
Downward transformation is risky and involves forced transformation
(type to be converted) variable
Object-oriented practice
Requirement: Choose and play among the three games of lol cs dnf
Analysis: Three classes need to be written, which are three games respectively, and then use the polymorphic knowledge in the previous section to classify them into the parent class game, then create a user person class to call them, and finally write a main method to create all object.
game
lol
dnf
cs
user
client
Execution results can be self-tested