一、static
(1)static相關變量和方法
1、所以static關鍵字修飾的都是類相關的,類級別的。
2、所以static修飾的,都是採用“類名.”的方式訪問。
3、static修飾的變量:靜態變量
4、static修飾的方法:靜態方法
(2)什麼時候變量聲明爲實例的,什麼時候聲明爲靜態的?
這個類型的所有對象的某個屬性值都是一樣的,不建議定義爲實例變量,浪費內存空間。
建議定義爲類級別特徵,定義爲靜態變量,在方法區中只保留一份,節省內存開銷。
(3)加static的變量叫做靜態變量,靜態變量在類加載的時候初始化,也就是說,靜態變量是存放在方法區的。那麼局部變量,實例變量,靜態變量在JVM內存的分佈是什麼樣子的?
(4)如何訪問實例、靜態。
實例的,一定是需要"引用."來訪問,靜態的:建議使用"類名."來訪問,但使用"引用."也行(不建議使用)。
(5)關於方法來說什麼時候定義爲實例方法,什麼時候定義爲靜態方法?
class User{
private int id;
public static void setId(int I ){
Id = I;
}
Public static int getId(){
Return id;
}
}
這裏方法加上了static行嗎?
不行,如果加上static說明是類級別的方法,然而id是實例的變量,所以會報錯,原理就是每一個對象的id都不一樣,但是setId.getId,加上了static,說明是類級別的,類級別的也就是沒有創建對象就已經存在了,裏面的含有實例的變量,實例變量是對象創建之後纔有的,所以會報錯。
(6)static在內存中的變量
class Chinese{
String idCard;
String name;
static String country ="中國";
//無參數
public Chinese(){
}
//有參數
public Chinese(String s1,String s2){
idCard = s1;
name = s2;
}
}
public class StaticTest{
public static void main(String [] args){
//訪問中國人的國際
//靜態變量應該使用類名.的方式訪問
System.out.println(Chinese.country);
Chinese c1 = new Chinese("12314","張三");
System.out.println(c1.idCard);
System.out.println(c1.name);
Chinese c2 = new Chinese("789","李四");
System.out.println(c2.idCard);
System.out.println(c2.name);
}
}
idCard是實例變量,必須先new對象,通過"引用."訪問
static內存圖如下:
靜態代碼塊
(a)使用static關鍵字可以定義:靜態代碼塊
(b)什麼是靜態代碼塊,語法是什麼?
Static{
java語句;
java語句;
}
(c)static靜態代碼塊在什麼時候執行?
類加載時執行,並且只執行1次。
(d)注意:靜態代碼塊在類加載時執行,並且在main方法執行之前執行
(e)靜態代碼塊一般都是按照自上而下的順序執行。
(f)靜態代碼塊有啥作用,有什麼用?
(I)靜態代碼塊不常用。
(II)靜態代碼塊是SUN公司給java程序員一種特殊的時刻,叫做類加載時機。
具體業務:
所以我們編寫的程序中,只要類加載了,請記錄一下類加載的日誌信息(那年那月,那個類加載到了JVM當中了)
寫到靜態代碼塊當中。
二、this
(1)this是關鍵字,小寫。
(2)this是什麼,在內存方面是怎麼樣的?
一個對象是一個this,this是一個變量,是一個引用,this保存當前對象的內存地址指向自身,
this就是代表的當前對象,this存儲在堆內存當中對象裏面。
public class ThisTest
{
public static void main(String [] args){
Customer c1 = new Customer();
c1.name = "張三";
c1.shopping();
Customer c2 = new Customer();
c2.name = "李四";
c2.shopping();
}
}
class Customer
{
String name;
public void shopping(){
System.out.println(name+"正在購物");
}
}
內存圖:
(3)this什麼時候不能省略
1、this可以使用在實例方法中,不能使用在靜態方法中。
2、this關鍵字大部分情況下可以省略,什麼時候不能省略呢?
public class HomeWork
{
public static void main(String [] args){
Wife beautiful = new Wife();
beautiful.name ="hahha";
Husband handsome = new Husband(111,"趙帥閣",beautiful);
//handsome.show();
//能夠輸出這個“丈夫對象”的妻子的名字。
//System.out.println(handsome.show());
System.out.println(handsome.name +"的妻子是"+handsome.wife.name);
}
}
class Husband
{
//身份證號
int userNumber;
//姓名
String name;
//出生日期
//UserDate birthDate;
//妻子
Wife wife;
public Husband(){
}
public Husband(int userNumber,String name,UserDate birthDate,Husband husband){
userNumber = userNumber;
name=name;
birthDate=birthDate;
husband=husband;
}
//展現
public void show(){
System.out.println("這個丈夫是"+name+"他的妻子是"+wife);
}
}
class Wife
{
//身份證號
int userNumber;
//姓名
String name;
//出生日期
UserDate birthDate;
//丈夫
Husband husband;
public Wife(){
}
public Wife(int userNumber,String name,UserDate birthDate,Husband husband){
userNumber = userNumber;
name=name;
birthDate=birthDate;
husband=husband;
}
}
class UserDate
{
int year;
int month;
int day;
}
這時候運行會出現Null值的情況,原因是因爲:
變量是遵循了就近原則,userNumber=userNumber,裏面的變量,都會認爲是int的局部變量。而name還是實例變量,所以就會是null。
this.表示引用,this的作用就是區分局部變量和實例變量的,所以這時候就不能省,並且可讀性高了。
(4)this()的用法
1、this除了可以用在實例方法中,還可以用在構造方法中。
2、新語法:通過當前的構造方法區調用另一個本類的構造方法,可以使用一下語法格式:
this(實際參數列表);
通過一個構造方法1去調用構造方法2,可以做到代碼複用,但需要注意的是“構造方法1”和“構造方法2”都是在同一個類當中。
注意:this()的調用必須出現在構造方法的第一行。
3、this()的作用就是代碼複用。
4、用代碼演示this()的作用:
public class ThisTest
{
public static void main(String [] args){
Date d1 = new Date();
d1.show();
Date d2 = new Date(2020,4,29);
d2.show();
}
}
class Date
{
private int year;
private int month;
private int day;
//設置get和set,可以通過set設置關卡
public int getYear(){
return year;
}
public void setYear(int year){
this.year = year;
}
public int getMonth(){
return month;
}
public void setMonth(int month){
this.month = month;
}
public int getDay(){
return day;
}
public void getDay(int day){
this.day = day;
}
//構造函數
public Date(){
this.year = 1970;
this.month =1;
this.day =1;
}
public Date(int year,int month,int day){
this.year = year ;
this.month = month;
this.day = day;
}
//寫一個方法區展示year,month,day
public void show(){
System.out.println(this.year +"年"+this.month+"月"+this.day+"日");
}
}
這裏會顯的特別的囉嗦,兩個構造函數也就重複了。
所以這時會使用this()。
三 總結this
1.1、this是一個關鍵字
1.2、this可以使用在實例方法中,也可以使用在構造方法中。
1.3、this出現在實例方法中其實代表的是當前對象
1.4、this不能使用在靜態方法中。
1.5、this.大部分情況下可以省略,但是用來區分局部變量和實例變量的時候不能省略。
1.6、this()這種語法只能出現在構造方法第一行,表示當前構造方法調用本類其他的構造方法,目的是代碼複用。