新聞中心
Java構(gòu)造方法的調(diào)用方式有哪些

在Java中,構(gòu)造方法是一種特殊的方法,用于初始化對(duì)象,構(gòu)造方法的名稱與類名相同,沒有返回值類型(即使是void也不需要寫),可以有參數(shù),構(gòu)造方法的主要作用是完成對(duì)象的初始化工作,本文將介紹Java構(gòu)造方法的調(diào)用方式。
1、無參構(gòu)造方法的調(diào)用
當(dāng)創(chuàng)建一個(gè)對(duì)象時(shí),如果沒有提供任何參數(shù),系統(tǒng)會(huì)自動(dòng)調(diào)用無參構(gòu)造方法。
public class Person {
private String name;
private int age;
// 無參構(gòu)造方法
public Person() {
this.name = "未知";
this.age = 0;
}
}
public class Main {
public static void main(String[] args) {
// 創(chuàng)建Person對(duì)象,自動(dòng)調(diào)用無參構(gòu)造方法
Person person = new Person();
}
}
2、有參構(gòu)造方法的調(diào)用
當(dāng)創(chuàng)建一個(gè)對(duì)象時(shí),如果提供了參數(shù),系統(tǒng)會(huì)調(diào)用與參數(shù)匹配的有參構(gòu)造方法。
public class Person {
private String name;
private int age;
// 有參構(gòu)造方法
public Person(String name, int age) {
this.name = name;
this.age = age;
}
}
public class Main {
public static void main(String[] args) {
// 創(chuàng)建Person對(duì)象,調(diào)用有參構(gòu)造方法
Person person = new Person("張三", 25);
}
}
3、通過this關(guān)鍵字調(diào)用其他構(gòu)造方法
在一個(gè)類中,可以定義多個(gè)構(gòu)造方法,當(dāng)一個(gè)構(gòu)造方法中需要調(diào)用另一個(gè)構(gòu)造方法時(shí),可以使用this關(guān)鍵字。
public class Person {
private String name;
private int age;
private String gender;
// 無參構(gòu)造方法
public Person() {
this("未知", 0, "未知");
}
// 有參構(gòu)造方法1
public Person(String name, int age) {
this(name, age, "未知");
}
// 有參構(gòu)造方法2
public Person(String name, int age, String gender) {
this.name = name;
this.age = age;
this.gender = gender;
}
}
4、通過反射機(jī)制調(diào)用構(gòu)造方法
Java反射機(jī)制允許在運(yùn)行時(shí)訪問類的元數(shù)據(jù),包括構(gòu)造方法,通過反射機(jī)制,可以在運(yùn)行時(shí)動(dòng)態(tài)地創(chuàng)建對(duì)象并調(diào)用構(gòu)造方法。
import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; import org.apache.commons.lang3.ClassUtils; import org.apache.commons.lang3.reflect.ConstructorUtils; import org.apache.commons.lang3.reflect.MethodUtils; import org.springframework.core.DefaultParameterNameDiscoverer; import org.springframework.core.ParameterNameDiscoverer; import org.springframework.core.ResolvableType; import org.springframework.core.io.Resource; import org.springframework.util.ReflectionUtils; import org.springframework.util.StringUtils; import org.springframework.util.ClassUtils; import org.springframework.util.ReflectionUtils; import org.springframework.util.ReflectionUtils; import org.springframework.util.ReflectionUtils; import org.springframework.util.ReflectionUtils; import org.springframework.util.ReflectionUtils; import org.springframework.util.ReflectionUtils; import org.springframework.util.ReflectionUtils; import org.springframework.util.ReflectionUtils; import org.springframework.util.ReflectionUtils; import org.springframework.util.ReflectionUtils; import org.springframework.util.ReflectionUtils; import org.springframework.util.ReflectionUtils; import org.springframework.util.ReflectionUtils; import org.springframework.util.ReflectionUtils; import org.springframework.util.ReflectionUtils; import org.springframework.util.ReflectionUtils; import org.springframework.util.ReflectionUtils; import org.springframework.util.ReflectionUtils; import org.springframework.util
當(dāng)前標(biāo)題:java構(gòu)造方法的調(diào)用方式有哪些
標(biāo)題網(wǎng)址:http://m.fisionsoft.com.cn/article/ccscgih.html


咨詢
建站咨詢
