Article Directory
One, static variables
01. Static: static
-
Static variable
-
Static meaning: load static members into the static area under the method area when the class is loaded in the program
-
The definition of static variables:
-
static String 变量名; //静态变量都有默认值
-
-
Use of static variables:
-
类名.静态成员变量="值";//赋值 数据类型 变量名 = 类名.静态成员变量;//取值
-
-
Characteristics of static variables:
- Shared data (only one copy in the method area)
-
-
Static method
-
Definition of static method:
-
public static 返回值类型 方法名(参数){ // }
-
-
Use of static methods:
-
类名.静态成员方法(实参);
-
-
Features when using static methods:
- When you want to access external variables or call external methods in a static method:
- Variable, must be static variable
- Method, must be a static method
- Non-static method: can access non-static content, can also access static content
- When you want to access external variables or call external methods in a static method:
-
02. Tools:
- In the development, when most programs need to use a certain function or business, you can extract the common content to be used as: Tools
- In Java: Arrays (tools for arrays), Math (tools for mathematical operations)
- Features:
- Static method
- Do not allow instantiation (private construction method)
03. Features of static methods:
- Static methods, can only access static content (static member variables, static member methods)
Non-static method:
-
Can access non-static content
-
Can access static content
Second, the internal class
-
Member inner class
-
Syntax format:
-
public class OuterClass{ private String name; public void method(){ } //成员内部类 class InnerClass{ } }
-
-
Instantiate member inner class:
-
外部类.内部类 对象 = new 外部类().new 内部类();
-
-
-
Anonymous inner class
-
Syntax format:
-
new 父类/接口(){ //重写父类/接口中方法 }
-
The essence of anonymous inner classes: subclass objects without names
-
-
effect:
- Simplify code writing
- The creation of subclasses can be omitted
- Simplify code writing
-
Application method:
-
1. When the method in the parent class/interface is called once
-
new 接口(){ //重写方法 public void method(){ /// } }.method();
-
-
2. As a parameter
-
//方法 public void show(InterA a){ a.method(); } //调用方法 show(new Inter(){ public void method(){ } });
-
-
3. As the return value
-
-
01, member inner class
-
In one class contains another class, the inner class is written in the member position
-
Instantiation:
-
外部类名.内部类名 对象 = new 外部类().new 内部类(); //内部类访问外部类中的成员: 外部类名.this.成员
-
02, local internal class
In the external class, the class defined outside the method (omitted)
Anonymous inner class
-
Role: to simplify the code writing in the program (you can omit the creation of subclasses)
-
grammar:
-
new 父类/接口(){ //重写父类/接口中的方法 }
-
-
Application scenarios:
-
Call method once
-
new 接口(){ //重写方法 }.重写后的方法();
-
-
As a parameter
-
public void method(Inter a){ } method(new Inter(){ //重写接口中的方法 });
-
-
As return value
-
public Inter get(){ return new Inter(){ //重写方法 }; }
-
-
Anonymous inner class instance
interface HelloInter{
public abstract void sayHello(String name);
}
//实现类
class HelloInterImpl implements HelloInter{
//重写sayHello方法
public void sayHello(String name){
///
}
}
//实例化 实现类
HelloInterImpl hello = new HelloInterImpl();
hello.sayHello();
Anonymous inner class: simplified code interface, subclass instantiation of abstract class
new HelloInter{
//重写sayHello方法
public void sayHello(String name){
///
}
}.sayHello("张三");
public void showHello(HelloInter helloInter){
helloInter.sayHello("");
}
HelloInterImpl hello = new HelloInterImpl();//实例化接口的实现类
showHello( hello);//把子类对象传递给方法中
showHello( new HelloInter(){
//重写
public void sayHello(){
}
});
Three, the code block
01, static code block
-
Syntax format:
-
public class 类{ static{ //静态代码块 } }
-
-
Features:
- Execute with the loading of the class, only once (the class will only be loaded once by the JVM)
-
Application scenarios:
- Initialize values for static members
- Before creating the object, you can initialize some data in advance
- Initialize values for static members
02, structure code block
-
Syntax format:
-
public class 类{ private String name; public void setName(String name){ } { //构造代码块 } }
-
-
Features:
- When an object is created, the JVM will automatically call the constructor to execute it, and it will be executed each time the constructor is executed: the construction code block
-
Application scenarios:
- Extract the common codes in multiple construction methods into the construction code block (usually used for non-static member variable initialization)
03, partial code block
-
Syntax format:
-
static{ //局部代码块 { } }
-
-
Role: limited scope
Four, enumerate Enum
-
Syntax format:
-
public enum 枚举名{ 成员1,成员2,..; }
-
-
Use enumeration:
-
枚举名.成员;
-
-
Application scenarios of enumeration:
- When there is a fixed range of data in the program, you can use enumeration to define
- Example: gender (male, female)
- When there is a fixed range of data in the program, you can use enumeration to define
Five, commonly used API
Date class
Use classes to describe things:
public class 学生{
//属性:
String 姓名;
int 年龄;
double 成绩;
java.util.Date 出生日期; //2020-2-20
}
Conclusion: When storing date data in the program, use the Date type
DateFormat class
effect:
- Convert String type to Date type: Analysis
- Convert Date type to String type: Format
java.text.DateFormat类:
- Is an abstract class (cannot be instantiated)
- Usually use subclasses: java.text.SimpleDateFormat class
Instantiation method:
//多态的格式:
DateFormat df = new SimpleDateFormat("日期模式字符串")
//日期模式:
y:年
M:月
d:日
H:时(24小时制)
m:分
s:秒
日期模式示例:
"yyyy-MM-dd" 对应格式: 2020-03-20
"HH:mm:ss" 对象格式:16:17:20
"yyyy/MM/dd HH:mm:ss" 2020/03/20 16:17:20
Common methods in DateFormat:
-
Date => String
-
public String format(Date date)
-
-
String => Date
-
public Date parse(String strDate)
-
Calendar class
java.util.Calendar class:
- Is an abstract class (cannot be instantiated)
- Use the functions in the Calendar class to replace the obsolete functions in the Date class
The way to instantiate the Calendar class:
-
Directly use subclass instantiation
-
Calendar c = new GregorianCalendar();
-
-
Use static method: getInstance()
-
Calendar c = Calendar.getInstance(); //本质:getInstance()方法的底层代码: new GregorianCalendar()
-
Common methods in the Calendar class:
-
public int get(int field) //field represents a field value (a static constant value in Calendar)
-
//获取日历对象中的:年、月 Calendar c = Calendar.getInstance(); int year = c.get( Calendar.YEAR );//获取年 int month = c.get( Calendar.MONTH );//获取月
-
-
public void set(int field , int value)
-
//把日历中的年份设置为:2019年 Calendar c = Calendar.getInstance(); c.set( Calendar.YEAR , 2019 );
-
-
public void add(int field , int value)
-
//在当前日期的基础上,向后+3天 Calendar c = Calendar.getInstance(); c.add( Calendar.DAY_OF_MONTH , 3);//向后+3天 c.add( Calendar.DAY_OF_MONTH , -3);//向前-3天
-
Math class
Math class:
- Class: Tools. Tools for mathematical operations. Encapsulate a large number of static methods
- Instantiation: No need for instantiation. Call the method directly using the class name
- Common methods:
- Find the best value: max(), min()
- Rounding: round()
- Round up/down
Example:
public class MathDemo1 {
public static void main(String[] args) {
System.out.println("最大值:"+Math.max(21,20));
System.out.println("最小值:"+Math.min(21,20));
System.out.println("四舍五入:"+Math.round(3.76));
System.out.println(Math.ceil(3.01));
}
}
System class
Class: The tool classes provided by java and related to the system.
Instantiation: No need for instantiation. Call the method directly using the class name
Common methods:
- currentTimeMillis() //Get the millisecond value of the current system time
Example:
public class SystemDemo1 {
public static void main(String[] args) {
long startTime = System.currentTimeMillis();
int count=0;
for (int i = 0; i < 100000; i++) {
if(i%2==0){
count++;
}
}
long endTime =System.currentTimeMillis();
System.out.println("执行了:"+(endTime-startTime)+"毫秒");
}
}
Six, summary
-
static
- It is a decoration symbol in java, which can be used to modify: variables, methods
- Modified variables: static variables
- Modification method: static method
- Static characteristics:
- Appears in the method area of the memory as the class is loaded (under the static area)
- There is only one copy of static content in memory (shared) [No matter how many objects are created, there is only one]
- Application of static in development:
- Static constant tool class
- Static method tool class
- It is a decoration symbol in java, which can be used to modify: variables, methods
-
Code block
-
Static code block
-
Features: Execute with the loading of the class (code executed when the class is loaded)
-
Role: usually used to initialize static content (static variable initialization value)
-
format:
-
public class 类{ static{ } }
-
-
-
Structure code block
-
Features: before the construction method is executed, it will be executed (when each construction method is executed, the construction code block will be called first)
-
Function: The common content of multiple construction methods is extracted into the construction code block (non-static variable initialization)
-
format:
-
public class 类{ private String name; { } public 类(){ } }
-
-
-
Local code block
-
-
enumerate
-
Function: used to constrain the data in a fixed range in the program
-
definition:
-
public enum 枚举名{ 成员1,成员2,...; }
-
-
use:
-
枚举名.成员名;
-
-
-
Object class
-
Class: The Object class is the top-level parent class in the Java system (all classes directly or indirectly inherit the Object class)
-
Instantiation: public Object()
-
Common methods:
- public String toString() //Convert the object to a string
- public boolean equals(Object) //Compare whether two objects are the same (the memory address is compared)
-
The difference between equals(Object obj) in the Object class and equals(Object obj1, Object obj2) in the Objects tool class:
-
//调用Object类中的equals Student stu = null; stu.equals( new Student("张三",20) );//运行结果:报错。 空指针异常 //结论:Object类中的equals方法会出现空指针异常 //Objects类中的equals Student stu = null; Objects.equals( stu , new Student("张三",20) );//可以正常运行,不会引发 空指针异常 //运行结果:false
-
-
Date class
- Class: The type used to store date data in a java program
- Instantiation: public Date(), public Date(long time)
- Common methods:
- void setTime(long time)
- long getTime()
-
DateFormat class
- Class: Used to convert between String type and Date type. Abstract class (cannot be instantiated)
- Instantiation: DateFormat df = new SimpleDateFormat("Date pattern string")
- Common methods:
- String format(Date date) // Date => String
- Date parse(String strDate) // String => Date
-
Calendar class
- Class: Calendar class. Used to replace the obsolete methods in the Date class. Abstract class
- Instantiation: Calendar c = Calendar.getInstance();
- Common methods:
- void set(int field, value) //Set the value of the data in the calendar class object (overwrite value)
- int get( int field) //Get the data value in the calendar class object
- void add( int field, int value) //Modify the data value in the calendar class object (add or subtract from the original value)