構造方法是一個比較特殊的方法,通過構造方法可以完成對象的創(chuàng)建,以及實例變量的初始化。換句話說,構造方法就是用來創(chuàng)建對象,同時給對象的屬性賦值
創(chuàng)新互聯堅持“要么做到,要么別承諾”的工作理念,服務領域包括:成都做網站、網站建設、外貿營銷網站建設、企業(yè)官網、英文網站、手機端網站、網站推廣等服務,滿足客戶于互聯網時代的蚌山網站設計、移動媒體設計的需求,幫助企業(yè)找到有效的互聯網解決方案。努力成為您成熟可靠的網絡建設合作伙伴!(PS:當實例變量沒有被我們手動去賦值的時候,系統(tǒng)會給實例變量賦默認值)
2、構造方法的語法結構構造方法的語法結構
[修飾符列表]?構造方法名(形式參數列表){
構造方法體;
通常在構造方法體當中給屬性賦值,完成屬性的初始化
}
注意:
1、修飾符列表統(tǒng)一寫:public
2、構造方法名和類名必須一致
3、構造方法不能寫返回值的類型
對比普通方法的語法結構
[修飾符列表] 返回值類型 方法名(形式參數列表){
方法體;
}
怎么調用構造方法
使用new運算符來調用構造方法
語法格式:
new 構造方法名(實際參數列表);
重點(需要記憶):當一個類沒有提供任何構造方法,系統(tǒng)會默認提供一個無參數的構造方法(而這個構造方法被稱為缺省構造器)?。
public class ConstructorTest01{
public static void main(String[] args){
//創(chuàng)建Student類型的對象
Student s1 = new Student();
//輸出“引用”
//只要輸出結果不是null,說明這個對象一定是創(chuàng)建完成了
System.out.println(s1);
//調用Student類的無參數構造方法
new Student();//結果:無參數的構造方法執(zhí)行了!
}
}
class Student{
//學號
int no;
//姓名
String name;
//年齡
int age;
//當Student這個類當中并沒有定義任何構造方法
//但是我們創(chuàng)建對象的時候成功了,這里就是系統(tǒng)提供的缺省構造器
//將無參數的構造方法(缺省構造器)寫出來
public Student(){
System.out.println("無參數的構造方法執(zhí)行了!");
}
}
3、構造方法的重載我們都知道普通方法能夠重載,那構造方法能夠重載嗎?
答案:能,并且和普通方法重載一樣,可以創(chuàng)建多個構造方法,方法名相同,參數列表不同
具體請參照方法重載:(6條消息) Java方法重寫與重載的區(qū)別_徐念安的博客-博客_請簡述方法重寫和方法重載的區(qū)別
當一個類當中手動的提供了構造方法,那么系統(tǒng)將不再提供無參數構造方法
(建議:將無參數構造方法手動寫出來,這樣一定不會出問題)
public class ConstructorTest01{
public static void main(String[] args){
//這里將會報錯,因為在Student中我們手動的定義了一個帶參數列表的構造方法
//所以缺省構造器已經沒了,除非我們在Student當中再去手動的把無參構造方法定義出來
new Student();
}
}
class Student{
//學號
int no;
//姓名
String name;
//年齡
int age;
public Student(int i){
System.out.println("有參數的構造方法執(zhí)行了!");
}
}
當我們手動的定義了無參構造方法和有參構造方法之后,無參構造方法和有參構造方法都可以調用
public class ConstructorTest01{
public static void main(String[] args){
new Student();//無參數的構造方法執(zhí)行了!
new Studnet(100);//有參數的構造方法執(zhí)行了!
new Studnet("zhangsan");//有參數的String構造方法執(zhí)行了!
}
}
class Student{
//學號
int no;
//姓名
String name;
//年齡
int age;
public Student(){
System.out.println("無參數的構造方法執(zhí)行了!");
}
public Student(int i){
System.out.println("有參數的構造方法執(zhí)行了!");
}
public Student(String name){
System.out.println("有參數的String構造方法執(zhí)行了!");
}
}
實例變量沒有手動賦值的時候,實際上系統(tǒng)會默認賦值
那么這個默認賦值操作是在什么時間進行的?
? 是在類加載的時候給這些實例變量賦值嗎?
? 不是,實例變量是在構造方法執(zhí)行的過程中完成初始化的,完成賦值的
public class ConstructorTest01{
public static void main(String[] args){
Student student = new Student();
System.out.println(student.no);//輸出結果30
System.out.println(student.name);//輸出結果zhangsan
System.out.println(student.age);//輸出結果10
//由于有參構造方法我們并沒有對實例變量進行賦值
//所以在這里的輸出依然是系統(tǒng)的默認值
Student student1 = new Student(1);
System.out.println(student1.no);//輸出結果0
System.out.println(student1.name);//輸出結果null
System.out.println(student1.age);//輸出結果0
}
}
class Student{
//學號
int no;//默認值0
//姓名
String name;//默認值null
//年齡
int age;//默認值0
public Student(){
//這里手動賦值,將不再使用系統(tǒng)默認值
no = 30;
name = "zhangsan";
age = 10;
}
public Student(int i){
}
}
除此之外,我們還可以在調用構造方法創(chuàng)建對象的時候對實例變量進行賦值
public class ConstructorTest01{
public static void main(String[] args){
Student student = new Student();
System.out.println(student.no);//輸出結果30
System.out.println(student.name);//輸出結果zhangsan
System.out.println(student.age);//輸出結果10
//由于該構造方法我們設置了參數列表,所以我們可以在創(chuàng)建對象的時候傳值
//此處表示no = 20;name = lisi;age = 30;
Student student1 = new Student(20,lisi,30);
System.out.println(student1.no);//輸出結果20
System.out.println(student1.name);//輸出結果lisi
System.out.println(student1.age);//輸出結果30
}
}
class Student{
//學號
int no;//默認值0
//姓名
String name;//默認值null
//年齡
int age;//默認值0
public Student(){
//這里手動賦值,將不再使用系統(tǒng)默認值
no = 30;
name = "zhangsan";
age = 10;
}
//這里使用傳參的形式來賦值
public Student(int xueHao,String mingZi,int nianLing){
no = xueHao;
name = mingZi;
nianLing = age;
}
}
你是否還在尋找穩(wěn)定的海外服務器提供商?創(chuàng)新互聯www.cdcxhl.cn海外機房具備T級流量清洗系統(tǒng)配攻擊溯源,準確流量調度確保服務器高可用性,企業(yè)級服務器適合批量采購,新人活動首月15元起,快前往官網查看詳情吧
文章題目:Java中的構造方法-創(chuàng)新互聯
網頁路徑:http://www.rwnh.cn/article42/cspehc.html
成都網站建設公司_創(chuàng)新互聯,為您提供網站營銷、用戶體驗、App設計、網頁設計公司、微信小程序、搜索引擎優(yōu)化
聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯