inherit
Commonness extraction
In inheritance, a subclass is a parent, that is, a subclass can be treated as a parent
For example, the parent class is an employee, the child class is a lecturer, and the lecturer is an employee
Define parent class: (the definition of ordinary class is the same)
public class parent class name{
//......
}
Define subclass format:
public class subclass name extends parent class name{
//......
}
Access order of member variables in parent-child classes
In the inheritance relationship between parent and child classes, if the member variables have the same name, there are two ways to access when creating child class objects
Access member variables directly through subclass objects
Whoever is on the left of the equal sign has priority. If not, look up
Accessing member variables indirectly through member methods
Whoever the method belongs to will give priority to the use, and if not, look up
Local variable: write the variable name directly
Member variable in this class: this. Member variable name
Member variable of parent class: super. Member variable name
public class Son extends Father{ int num=200; public void methodSon(){ int num=30; System.out.println(num); System.out.println(this.num); System.out.println(super.num); } }
Characteristics of member variable access in inheritance
In the inheritance relationship between parent and child classes, if the member variables have the same name, there are two access methods when creating child class objects
Directly access the member variable through the subclass object. Whoever is on the left of the equal sign has priority. If not, look up
Accessing member variables indirectly through member methods
Characteristics of member method access in inheritance
Rules for creating subclass objects and accessing member methods in the parent-child inheritance relationship:
Whoever creates the object will be used first. If not, continue to look up
-
matters needing attention:
-
Whether it's a member variable or a member method, if it doesn't, it's looking up for the parent class and never looking down for the child class
public class Test { public static void main(String[] args) { // Father father = new Father(); // father.methodFather(); // father.method(); Son son = new Son(); son.methodSon(); son.methodFather(); son.method(); } void method(){ } }
super keyword
The super keyword is used to access the content of the parent class, while the this keyword is used to access the content of this class
1. In the member method of this class, access the member variables of this class
2. In the method of this class, access another member method of this class
3. In the constructor of this class, access another constructor of this class
In the inheritance relationship, the access characteristics of the construction method are as follows:
1. There is a default implicit [super();] in the subclass construction method, so the construction of the parent class is called first, and then the construction of the subclass is executed
2. For subclass construction, you can use the [super] keyword to call the content of the parent class
3. The parent class construction call of super must be the first sentence [important] in the subclass construction. A subclass construction cannot call super construction multiple times
[summary]:
The subclass must call the constructor of the parent class without writing super()
public class Son extends Father{ int num=20; public Son(){ // super(); System.out.println("Parameterless construction of subclasses!"); } public Son(String name){ this(); // super(); can be omitted System.out.println("Parametric construction of subclasses!"); // super(); / / incorrect writing } public void method(){ int num=30; System.out.println(num); System.out.println(this.num); System.out.println(super.num); super.method(); } }
Method override
Re compare with overload
Override: the name of the method is the same, and the parameter list is the same. Overwrite and duplicate
Overload: the name of the method is the same, but the parameter list is different
Method: if you create a subclass object, the subclass method is preferred
Precautions for method Rewriting:
1. It must be ensured that the names of methods and parameter lists between parent and child classes are the same
@Override: it is written in front of the method to verify whether it is valid and correct,
Play the role of safety detection
2. The return value of the subclass method must be less than or equal to the return value range of the parent method
Tip: java.lang.String, parent class: java.lang.Object
3. The permission of the subclass method must be greater than or equal to the permission modifier of the parent method
Tip: public > protected > Default > private
Use of tool classes
Date and time class
java.util.Date: represents the date and time class
Class Date represents a specific moment, accurate to milliseconds
Milliseconds: thousandth of a second, 1000 milliseconds = 1 second
Convert date to milliseconds:
Current time: July 22, 2021
Origin of time (0 ms): 1970-01-01 00:00:00 (green, UK)
1626942611592
It is to calculate the total number of milliseconds (a long) between the current date and the time origin
Convert millisecond value to date:
1 day = 246060 * 1000 = 86400000 milliseconds
Note: China belongs to Dongba District, which will increase the time by 8 hours
1970-01-01 08: 00: 00
DateFormat class
-
In the java.text package is the time / date formatting subclass [abstract class], which helps us format the date and time
Date class to string:
String to Date class:
* Date Class to string: * * String conversion Date Class: */ public class Demo1Date { public static void main(String[] args) throws ParseException { //Get the total number of milliseconds elapsed between the current system time and 1970-01-01 08:00:00 // System.out.println(System.currentTimeMillis()); System.out.println(24*60*60*1000); // Date date = new Date(); // long time = date.getTime(); // System.out.println(date); // System.out.println("2021-07-22"); // System.out.println(time); String dateStr="2002-01-01"; SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); Date parse = sdf.parse(dateStr); System.out.println(parse); } }
String to date:
-
Date parse(String str):
-
be careful:
-
The parse() method declares an exception mechanism called ParseException
-
If the pattern of string and constructor is different, the program will throw an exception
/** * String to date: * Date parse(String str): * be careful: * parse()Method declares an exception mechanism called ParseException * If the pattern of string and constructor is different, the program will throw an exception */ public void strFormatDate(){ public static void main(String[] args){ String dateStr="2002-01-01"; Student student = new Student(); SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); try { Date date = sdf.parse(dateStr); student.setBrithDate(date); } catch (ParseException e) { e.printStackTrace(); } } }
Calendar Class
java.util.Calendar: Calendar Class
Calendar class is an abstract class, which provides many methods to operate calendar fields
The Calendar class cannot create objects directly. There is a static method called getInstance(),
This method can directly return the subclass object of the Calendar class
public class Demo2Calendar { public static void main(String[] args) { Calendar c = Calendar.getInstance(); int year = c.get(Calendar.YEAR); System.out.println(year); int month = c.get(Calendar.MONTH);//Month 0-11 in the West and 1-12 in the East System.out.println(month); int day = c.get(Calendar.DAY_OF_MONTH); System.out.println(day); int date = c.get(Calendar.DATE); System.out.println(date); } }