??個人主頁:?Aileen_0v0
??系列專欄:Java學習系列專欄
??個人格言:"沒有羅馬,那就自己創(chuàng)造羅馬~"
回顧
上次,我們學習了關于Java面向對象編程的構造方法,以及關鍵字this在構造方法/實例化對象中的使用,若有遺忘點擊????
本節(jié)我們`來學習,代碼塊,tostring以及繼承?
那還等什么,直接進入主題吧~
如何調用構造方法:
?通過構造對象進行構造方法的調用,對應自己想調用的構造方法,看構造方法的參數(shù)個數(shù)進行傳參.
上面main函數(shù)利用創(chuàng)建的對象person1調用的是無參構造方法
代碼塊?
代碼塊分類
package lecture1;
class Person{
private String name;
private int age;
private static int count;
//構造方法 - 總是和類名相同,且無返回值
//當沒有構造方法的時候,編譯器會自動提供一個,不帶參數(shù)的構造方法
//構造方法作用:實例化一個對象
//構造方法1:
public Person() {
//this("????",1);
System.out.println("Person<init>");
//this("林????");
}
//構造方法2:
public Person(String name, int age) {
this.name = name;
this.age = age;
System.out.println("Person<String,int>");
}
{
// this.name = "zhangfei";
// count = 99;
System.out.println("實例代碼塊......作用:訪問普通成員變量");
}
static{
// this.name = "zhangfei";//牢記:靜態(tài)不能訪問非靜態(tài)的數(shù)據(jù)成員
// count = 99;
System.out.println("靜態(tài)代碼塊......");
}
public static void func() {
count = 99;
// this.name = "caocao";
}
//構造方法3:
public Person(String name) {
}
public void eat() {
System.out.println(this.getName());
System.out.println(this.age);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
//@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
",age=" + age +
"}";
}
}
public class TestDemo {
public static void main(String[] args) {
Person person1 = new Person();
System.out.println("======================");
Person person2 = new Person();
}
運行結果
根上面代碼的運行結果我們可以得到以下結論:?
代碼塊打印順序:
靜態(tài)的方法 和 靜態(tài)的 成員 不依賴對象.---我們可以通過類名進行訪問.
如果靜態(tài)方法里面訪問 實例成員變量, 那么 這個實例成員變量是依賴對象的,但靜態(tài)方法不依賴.?
重要的事情說三遍:
靜態(tài)里面不能有非靜態(tài)數(shù)據(jù)成員!!!
靜態(tài)里面不能有非靜態(tài)數(shù)據(jù)成員!!!
靜態(tài)里面不能有非靜態(tài)數(shù)據(jù)成員!!!
package lecture1;
class Person{
private String name;
private int age;
public static int count;
static{
// this.name = "zhangfei";//牢記:靜態(tài)不能訪問非靜態(tài)的數(shù)據(jù)成員
count = 99;
System.out.println("靜態(tài)代碼塊......");
}
//構造方法 - 總是和類名相同,且無返回值
//當沒有構造方法的時候,編譯器會自動提供一個,不帶參數(shù)的構造方法
//構造方法作用:實例化一個對象
//構造方法1:
public Person() {
//this("????",1);
System.out.println("Person<init>");
//this("林????");
}
//構造方法2:
public Person(String name, int age) {
this.name = name;
this.age = age;
System.out.println("Person<String,int>");
}
{
System.out.println("實例代碼塊......");
}
public static void func() {
count = 99;
// this.name = "caocao";
}
//構造方法3:
public Person(String name) {
}
public void eat() {
System.out.println(this.getName());
System.out.println(this.age);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
//@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
",age=" + age +
"}";
}
}
public class TestDemo {
public static void main(String[] args) {
System.out.println(Person.count);
}
?
對比上面運行結果,左邊打印出的count值為99,右邊為0,我們可以得出:
如果代碼塊都是靜態(tài)的, 那么打印結果 和定義的前后順序有關
匿名對象
匿名對象就是沒有名字的對象,每次使用只能使用一次
沒有引用的對象稱為匿名對象
匿名對象只能在創(chuàng)建對象時使用
如果一個對象只是用一次,后面不需要用了,可以考慮使用匿名對象
package lecture1;
class Person{
private String name;
private int age;
static{
// this.name = "zhangfei";//牢記:靜態(tài)不能訪問非靜態(tài)的數(shù)據(jù)成員
count = 99;
System.out.println("靜態(tài)代碼塊......");
}
public static int count = 0;
//構造方法 - 總是和類名相同,且無返回值
//當沒有構造方法的時候,編譯器會自動提供一個,不帶參數(shù)的構造方法
//構造方法作用:實例化一個對象
//構造方法1:
public Person() {
System.out.println("Person<init>");
}
//構造方法2:
public Person(String name, int age) {
System.out.println("Person<String,int>");
}
//實例代碼塊
{
System.out.println("實例代碼塊......");
}
public static void func() {
count = 99;
// this.name = "caocao";
}
//構造方法3:
public Person(String name) {
}
public void eat() {
System.out.println(this.getName());
System.out.println(this.age);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
public class TestDemo {
public static void main(String[] args) {
//有名對象↓
Person person1 = new Person();
person1.eat();
person1.eat();
System.out.println(person1.getName());
//匿名對象↓
new Person().eat();
new Person().getName();
}
??總結:
一個類可以產生無數(shù)的對象,類就是模板,對象就是具體的實例。
類中定義的屬性,大概分為幾類: 類屬性,對象屬性。
其中被static所修飾的數(shù)據(jù)屬性稱為類屬性,static修飾的方法稱為類方法,特點是不依賴于對象,我們只需要通過類名就可以調用其屬性或者方法。
靜態(tài)代碼塊優(yōu)先實例代碼塊執(zhí)行,實例代碼塊優(yōu)先構造函數(shù)執(zhí)行。文章來源:http://www.zghlxwxcb.cn/news/detail-756137.html
this關鍵字代表的是當前對象的引用。并不是當前對象。?文章來源地址http://www.zghlxwxcb.cn/news/detail-756137.html
到了這里,關于【JAVA雜貨鋪】一文帶你走進面向對象編程|構造方法調用 | 代碼塊分類| 期末復習系列 | (中3)的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!