一、變量作用域
Java 中的變量有3種:
?? ① 全局變量:被定義在類中(成員變量)
?? ② 局部變量:被定義在成員方法、代碼塊、靜態(tài)代碼塊中定義的變量
?? ③ 參數(shù):方法聲明中的變量
There are several kinds of variables(變量):
?? Member variables(成員變量) in a class:these are called fields(屬性)
?? Variables in a method or block of code(代碼塊):these are called local variables(局部變量)
?? Variables in method declarations(方法聲明):these are called parameters(參數(shù))
?? 全局變量(成員變量)作用域?yàn)椋赫麄€(gè)類體
?? 局部變量(除全局變量之外的變量)作用域?yàn)椋核诘拇a塊
?? 全局變量可以不賦值,直接使用(全局變量有默認(rèn)值)
?? 局部變量必須賦值后才能使用
?? 參數(shù)的值在方法被調(diào)用的時(shí)候才有
public class VariableDomain {
// 全局變量
private String name;
{
int age = 10; // 局部變量
}
static {
double pai = 3.14; // 局部變量
}
// num1、description 參數(shù)
public void test(int num1, String description) {
String hobby = "睡覺"; // 局部變量
}
}
?? 全局變量名和局部變量名可以一樣,訪問的時(shí)候遵循就近原則
?? 在同一作用域中(eg:同一成員方法中),不能有重名的局部變量
?? 同一類的同一代碼塊中的成員變量也不能重名
全局變量和局部變量重名的時(shí)候,訪問遵循就近原則:
public class VariableDomain {
// 全局變量
private String name = "張浩男";
public static void main(String[] args) {
VariableDomain domain = new VariableDomain();
domain.test();
}
private void test() {
String name = "莫松"; // 局部變量(可以和全局變量重名)
// output: name = 莫松
System.out.println("name = " + name);
}
}
不同類的成員變量可以重名:
?? 全局變量(成員變量)的生命周期長(zhǎng):【對(duì)象就像一個(gè)人??,成員變量就像一個(gè)人的手??】全局變量的生命伴隨著對(duì)象的存在而存在,便隨著對(duì)象的銷毀而銷毀
?? 局部變量生命周期短:與它所在的代碼塊共生
?? 全局變量可以在本類或其他類中使用
?? 局部變量只能在它所在類的指定方法中使用
?? 全局變量可以被修飾符修飾(eg:private、static、final)
?? 局部變量不能被修飾符修飾
二、構(gòu)造方法(Constructor)
(1) 官方教程解釋構(gòu)造方法
?? A class contains constructors that are invoked to create objects from the class blueprint. Constructor declarations look like method declarations:except that they use the name of the class and have no return type.
?? ① 類中包含構(gòu)造方法(可通過調(diào)用構(gòu)造方法從一個(gè)類模板中創(chuàng)建一個(gè)對(duì)象)
?? ② 聲明構(gòu)造方法和聲明成員方法一樣,但也有區(qū)別(??構(gòu)造方法的方法名和類名一模一樣;??構(gòu)造方法沒有返回值類型)
下面是一個(gè)構(gòu)造方法的例子:
public class Student {
private String name;
private int age;
private double score;
// (1) 沒有返回類型
// (2) 方法名和類名一致
public Student(String stuName, int stuAge, double stuScore) {
// 構(gòu)造方法的方法體中一般給成員變量賦值
name = stuName;
age = stuAge;
score = stuScore;
}
}
?? To create a new Student object called tom, a constructor is called by the new operator.
?? 要想創(chuàng)建一個(gè)名為 tom 的 Student 對(duì)象,可通過 new 運(yùn)算符調(diào)用構(gòu)造方法new 運(yùn)算符調(diào)用構(gòu)造方法,創(chuàng)建 Student 對(duì)象:
Student zhn = new Student("張浩男", 12, 99.5);
?? new Student("張浩男", 12, 99.5):
creates space in memory(內(nèi)存空間) for the object and initializes its fields
?? new Student("張浩男", 12, 99.5):
該代碼在內(nèi)存中為對(duì)象開辟了內(nèi)存空間,并且初始化了成員變量的值
?? Although Student only has one constructor, it could have others, including a no-argument constructor(無參構(gòu)造方法):
/**
* Student 類的無參構(gòu)造方法
*/
public Student() {
name = "慶醫(yī)";
age = 8;
score = 100;
}
?? 和成員方法一樣,構(gòu)造方法也是可以重載(Override)的:方法名和類名一致,參數(shù)列表各不相同
?? Both constructors could have been declared in Student because they have different argument lists(參數(shù)列表). As with methods(與方法一樣), the Java platform differentiates(區(qū)分) constructors on the basis of the number of arguments in the list and their types. You cannot write two constructors that have the same number and type of arguments for the same class, because the platform would not be able to tell(區(qū)分) them apart. Doing so causes a compile-time error.
?? 兩個(gè)構(gòu)造方法(有參構(gòu)造方法和無參構(gòu)造方法)都可在 Student 類中被聲明,因?yàn)樗鼈冇胁煌膮?shù)列表。與成員方法一樣,Java 平臺(tái)可通過參數(shù)數(shù)量和參數(shù)類型來區(qū)分構(gòu)造方法。在同一個(gè)類中,你不能寫兩個(gè)擁有相同參數(shù)數(shù)量和參數(shù)類型的構(gòu)造方法,因?yàn)?Java 平臺(tái)無法區(qū)分。寫兩個(gè)擁有相同參數(shù)數(shù)量和參數(shù)類型的構(gòu)造方法將會(huì)導(dǎo)致編譯時(shí)錯(cuò)誤。
?? You don’t have to provide any constructors for your class, but you must be careful when doing this. The compiler(編譯器) automatically provides a no-argument, default constructor(默認(rèn)構(gòu)造方法) for any class without constructors. This default constructor will call the no-argument constructor of the superclass(超類). In this situation, the compiler will complain(抱怨、埋怨、發(fā)惱騷) if the superclass(超類) doesn’t have a no-argument constructor so you must verify(驗(yàn)證) that it does. If your class has no explicit(明確的、顯式的) superclass, then it has an implicit (隱式的、含蓄的)superclass of Object, which does have a no-argument constructor.
?? 你并不是必須得給你的類提供一個(gè)構(gòu)造方法(你可以不同你的類提供任何構(gòu)造方法)。若不給類提供任何構(gòu)造方法的話,你必須當(dāng)心一點(diǎn)。編譯器會(huì)自動(dòng)提供一個(gè)無參的默認(rèn)構(gòu)造方法,這個(gè)默認(rèn)的無參構(gòu)造方法是提供給那些沒有任何構(gòu)造方法的類的。這個(gè)默認(rèn)的無參構(gòu)造方法將會(huì)調(diào)用超類的無參構(gòu)造方法,所以必須確保超類擁有無參構(gòu)造方法,否則編譯器會(huì)發(fā)惱騷(報(bào)錯(cuò)的)??。如果你的類沒有明確的超類,你的類會(huì)有一個(gè)隱式的超類(Object),Object 類是所有類的超類(是 Java 平臺(tái)提供的),Object 類百分百擁有一個(gè)無參構(gòu)造方法。
?? You can use access modifiers(訪問修飾符) in a constructor’s declaration to control which other classes can call the constructor.
?? 你可以在聲明構(gòu)造方法的使用使用訪問修飾符(public、protected、private),用以控制哪些其他類可以調(diào)用這個(gè)構(gòu)造方法。
?? If another class cannot call a Student constructor, it cannot directly(直接地) create Student objects.
?? 如果其他類不能調(diào)用 Student 類的構(gòu)造方法,其他類就無法直接地創(chuàng)建 Student 對(duì)象。
(2) 構(gòu)造方法概念
?? 構(gòu)造方法:也叫構(gòu)造器,用于更方便地創(chuàng)建一個(gè)對(duì)象
?? 方法名必須和類名一模一樣
?? 沒有返回值類型
?? 可以重載(方法名一樣,方法簽名不一樣)
構(gòu)造方法案例:
public class Handsome {
private int age;
private double weight;
public Handsome() {
}
public Handsome(int age) {
this.age = age;
}
public Handsome(int age, double weight) {
this.age = age;
this.weight = weight;
}
}
使用構(gòu)造方法創(chuàng)建 Handsome 對(duì)象:
// 可通過3種構(gòu)造方法創(chuàng)建 Handsome 對(duì)象
Handsome hs1 = new Handsome();
Handsome hs2 = new Handsome(17);
Handsome hs3 = new Handsome(17, 123.5);
(3) this 的本質(zhì) ☆☆☆☆☆
?? this:指向當(dāng)前對(duì)象的引用
this 的用途:
?? ① 訪問當(dāng)前類中定義的成員變量
?? ② 調(diào)用當(dāng)前類中定義的方法(包括構(gòu)造方法)
public class Computer {
private String brand;
private double price;
public Computer(String brand) {
// this 作用:調(diào)用當(dāng)前類中定義的方法(包括構(gòu)造方法)
this(brand, 0.0);
}
public Computer(String brand, double price) {
// this 作用:訪問當(dāng)前類中定義的成員變量
this.brand = brand;
this.price = price;
}
}
?? 在類中直接寫成員變量、直接調(diào)用成員方法,默認(rèn)都是訪問和調(diào)用當(dāng)前對(duì)象的
public class Cat {
public int age;
public void eat() {
// 等價(jià)于:System.out.println(this.age + "eat()");
System.out.println(age + "eat()");
}
public void run() {
// 等價(jià)于:this.eat();
eat();
}
}
?? this 的本質(zhì)是一個(gè)隱藏的、位置最靠前的方法參數(shù)。(面向?qū)ο蟮恼Z(yǔ)言的 this 都是這樣的)
?? 只能在構(gòu)造方法中用 this
調(diào)用其他構(gòu)造方法
?? 如果在構(gòu)造方法 A 中調(diào)用了其他構(gòu)造方法,調(diào)用構(gòu)造方法的語(yǔ)句必須是構(gòu)造方法 A 中的第一條語(yǔ)句
?? 默認(rèn)構(gòu)造方法(Default Constructor):如果一個(gè)類沒有自定義構(gòu)造方法,編譯器會(huì)自動(dòng)為它提供無參數(shù)的默認(rèn)構(gòu)造方法
?? 一旦自定義了構(gòu)造方法,默認(rèn)構(gòu)造方法就不再存在
三、對(duì)象創(chuàng)建流程分析
public class Person {
private int age = 17;
private String name;
public Person(String name, int age) {
this.age = age;
this.name = name;
}
}
Person zhn = new Person("張浩男", 18);
對(duì)象創(chuàng)建流程:
?? ① 方法區(qū)加載一次類信息
?? ② 在堆中分配對(duì)象的內(nèi)存空間
?? ③ 完成對(duì)象屬性初始化(屬性默認(rèn)初始化;顯示初始化;構(gòu)造器初始化)
?? ④ 對(duì)象在堆中的內(nèi)存地址賦值給 zhn(zhn 指向?qū)ο笤诙阎械牡刂罚?br>文章來源:http://www.zghlxwxcb.cn/news/detail-402020.html
Bye-Bye 如發(fā)現(xiàn)錯(cuò)誤,請(qǐng)不吝賜教!
文章來源地址http://www.zghlxwxcb.cn/news/detail-402020.html
到了這里,關(guān)于11、Java 變量作用域、構(gòu)造方法官方教程的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!