JAVA objects (3) -- Super, static, final keywords, staticfinal
Super
The super keyword can be used to directly call the constructor, attribute, and common method in the parent class.
Super calls the operation of the parent class constructor. Like this calls the constructor, the statement must be placed in the first line of the subclass constructor.
Public class Person {// defines the parent class Person private String name; // defines the name attribute private int age; // defines the age attribute public Person (String name, int age) {// set name and age this through the constructor. setName (name); // set the name attribute content this. setAge (age); // set the age attribute content} public String getName () {// get the name attribute return name;} public void setName (String name) {// set the name attribute this. name = name;} public int getAge () {// get age value return age;} public void setAge (int age) {// set age value this. age = age;} public String getInfo () {// return "name:" + this. getName () + "; age:" + this. getAge () ;}} public class Student extends Person {private String school; // defines the new attribute school public Student (String name, int age, String school) {super (name, age); // call the constructor this in the parent class. school = school;} public String getSchool () {// get the school attribute return school;} public void setSchool (String school) {// set the school attribute this. school = school ;}@ Override public String getInfo () {// Override the method return super in the parent class. getInfo () + "; School:" + this. getSchool (); // extended parent class method} public class Demo {public static void main (String [] args) {Student stu = new Student ("Li Si", 25, "Peking University"); System. out. println (stu. getInfo (); // call the overwrite method to print the output }}
Running result:
Name: Li Si; age: 25; School: Peking University
From the above program, we can see that the super () method in the subclass calls the constructor with two parameters in the parent class, and then overwrites the getInfo () method in the parent class, the output content is the content that has been overwritten by the quilt class;
Super and this are very similar. You can call constructor methods, attributes, and common methods. But there are also differences:
| Serial number |
Differences |
This |
Super |
| 1 |
Attribute access |
Access the attributes in this class. If this class does not exist, search for the attributes in the parent class. |
Access attributes in the parent class |
| 2 |
Method |
Access methods in this class. If this class does not exist, search |
Directly access methods in the parent class |
| 3 |
Call Construction |
Call this class construction, which must be placed in the first line of the Construction Method |
Call the parent class construction, which must be placed in the first line of the subclass Construction Method |
| 4 |
Special |
Indicates the current object |
No concept |
Since both can call the constructor method, this and super cannot appear at the same time, because they must be placed in the first line of the constructor method. In addition, no matter how the subclass operates, the constructor in the parent class is called first.
Static
Static indicates the meaning of "global" or "static", which is used to modify member variables or member methods. The member variables and member Methods Modified by static are independent of any object of this class, in other words, it does not depend on any instance objects. When this class is created, it is also created. Therefore, static objects can be accessed before any of its objects are created, without reference to any objects.
Static modified member variables or member methods are traditionally called static variables or static methods and can be accessed directly by class names. The access syntax is:
Class Name. Static Method Name (parameter list)
Class Name. static variable name
Final
Final indicates final. You can use final keywords to declare classes, attributes, and methods.
- Classes declared using final cannot have subclasses.
- The method declared using final cannot be overwritten by the quilt class.
- Variables declared using final become constants and constants cannot be modified.
When using final to declare variables, uppercase letters are required.