最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java中五種不同方法的創(chuàng)建對(duì)象

 更新時(shí)間:2016年07月22日 10:54:16   投稿:daisy  
本文為大家詳細(xì)介紹了在Java中5種不同創(chuàng)建對(duì)象的方法和實(shí)例代碼,感興趣的小伙伴們可以參考一下

前言

作為Java開(kāi)發(fā)者,我們每天都會(huì)創(chuàng)建大量的對(duì)象,但是,我們總是使用管理依賴系統(tǒng)(如Spring框架)來(lái)創(chuàng)建這些對(duì)象。其實(shí)還有其他方法可以創(chuàng)建對(duì)象,在接下來(lái)的文章中我會(huì)進(jìn)行詳細(xì)介紹。

1.使用new關(guān)鍵字

這是最常見(jiàn)的創(chuàng)建對(duì)象的方法,并且也非常簡(jiǎn)單。通過(guò)使用這種方法我們可以調(diào)用任何我們需要調(diào)用的構(gòu)造函數(shù)。

Employee emp1 = new Employee();
0: new      #19     // class org/programming/mitra/exercises/Employee
 3: dup
 4: invokespecial #21     // Method org/programming/mitra/exercises/Employee."":()V

2.使用class類的newInstance方法

我們也可以使用class類的newInstance方法來(lái)創(chuàng)建對(duì)象。此newInstance方法調(diào)用無(wú)參構(gòu)造函數(shù)以創(chuàng)建對(duì)象。

我們可以通過(guò)newInstance() 用以下方式創(chuàng)建對(duì)象:

Employee emp2 = (Employee) Class.forName("org.programming.mitra.exercises.Employee").newInstance();

或者

Employee emp2 = Employee.class.newInstance();
51: invokevirtual  #70  // Method java/lang/Class.newInstance:()Ljava/lang/Object;

3.使用構(gòu)造函數(shù)類的 newInstance方法

與使用class類的newInstance方法相似,java.lang.reflect.Constructor類中有一個(gè)可以用來(lái)創(chuàng)建對(duì)象的newInstance()函數(shù)方法。通過(guò)使用這個(gè)newInstance方法我們也可以調(diào)用參數(shù)化構(gòu)造函數(shù)和私有構(gòu)造函數(shù)。

Constructor<Employee> constructor = Employee.class.getConstructor();
Employee emp3 = constructor.newInstance();
111: invokevirtual #80 // Method java/lang/reflect/Constructor.newInstance:([Ljava/lang/Object;)Ljava/lang/Object;

這些 newInstance() 方法被認(rèn)為是創(chuàng)建對(duì)象的反射手段。實(shí)際上,內(nèi)部類的newInstance()方法使用構(gòu)造函數(shù)類的 newInstance() 方法。這就是為什么后者是首選并且使用不同的框架如Spring, Hibernate, Struts等。

4.使用clone方法

實(shí)際上無(wú)論何時(shí)我們調(diào)用clone方法,JAVA虛擬機(jī)都為我們創(chuàng)建了一個(gè)新的對(duì)象并且復(fù)制了之前對(duì)象的內(nèi)容到這個(gè)新的對(duì)象中。使用 clone方法創(chuàng)建對(duì)象不會(huì)調(diào)用任何構(gòu)造函數(shù)。

為了在對(duì)象中使用clone()方法,我們需要在其中實(shí)現(xiàn)可克隆類型并定義clone方法。

Employee emp4 = (Employee) emp3.clone();
162: invokevirtual #87 // Method org/programming/mitra/exercises/Employee.clone ()Ljava/lang/Object;

5.使用反序列化

無(wú)論何時(shí)我們對(duì)一個(gè)對(duì)象進(jìn)行序列化和反序列化,JAVA虛擬機(jī)都會(huì)為我們創(chuàng)建一個(gè)單獨(dú)的對(duì)象。在反序列化中,JAVA虛擬機(jī)不會(huì)使用任何構(gòu)造函數(shù)來(lái)創(chuàng)建對(duì)象。

對(duì)一個(gè)對(duì)象進(jìn)行序列化需要我們?cè)陬愔袑?shí)現(xiàn)可序列化的接口。

ObjectInputStream in = new ObjectInputStream(new FileInputStream("data.obj"));
Employee emp5 = (Employee) in.readObject();
261: invokevirtual #118  // Method java/io/ObjectInputStream.readObject:()Ljava/lang/Object;

正如我們?cè)谝陨系淖止?jié)代碼片段中所看到的,除第一種被轉(zhuǎn)換為一個(gè)新的函數(shù)和一個(gè) invokespecial 指令以外,其它4種方法都被調(diào)用并轉(zhuǎn)換為invokevirtual

示例

讓我們來(lái)看看準(zhǔn)備創(chuàng)建對(duì)象的 Employee 類:

class Employee implements Cloneable, Serializable {
  private static final long serialVersionUID = 1L;
  private String name;
  public Employee() {
    System.out.println("Employee Constructor Called...");
  }
  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
  @Override
  public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((name == null) ? 0 : name.hashCode());
    return result;
  }
  @Override
  public boolean equals(Object obj) {
    if (this == obj)
      return true;
    if (obj == null)
      return false;
    if (getClass() != obj.getClass())
      return false;
    Employee other = (Employee) obj;
    if (name == null) {
      if (other.name != null)
        return false;
    } else if (!name.equals(other.name))
      return false;
    return true;
  }
  @Override
  public String toString() {
    return "Employee [name=" + name + "]";
  }
  @Override
  public Object clone() {
    Object obj = null;
    try {
      obj = super.clone();
    } catch (CloneNotSupportedException e) {
      e.printStackTrace();
    }
    return obj;
  }
}

在下面的Java程序中我們用5種方式來(lái)創(chuàng)建 Employee對(duì)象。

public class ObjectCreation {
  public static void main(String... args) throws Exception {
    // By using new keyword
    Employee emp1 = new Employee();
    emp1.setName("Naresh");
    System.out.println(emp1 + ", hashcode : " + emp1.hashCode());
    // By using Class class's newInstance() method
    Employee emp2 = (Employee) Class.forName("org.programming.mitra.exercises.Employee")
                .newInstance();
    // Or we can simply do this
    // Employee emp2 = Employee.class.newInstance();
    emp2.setName("Rishi");
    System.out.println(emp2 + ", hashcode : " + emp2.hashCode());
    // By using Constructor class's newInstance() method
    Constructor<Employee> constructor = Employee.class.getConstructor();
    Employee emp3 = constructor.newInstance();
    emp3.setName("Yogesh");
    System.out.println(emp3 + ", hashcode : " + emp3.hashCode());
    // By using clone() method
    Employee emp4 = (Employee) emp3.clone();
    emp4.setName("Atul");
    System.out.println(emp4 + ", hashcode : " + emp4.hashCode());
    // By using Deserialization
    // Serialization
    ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("data.obj"));
    out.writeObject(emp4);
    out.close();
    //Deserialization
    ObjectInputStream in = new ObjectInputStream(new FileInputStream("data.obj"));
    Employee emp5 = (Employee) in.readObject();
    in.close();
    emp5.setName("Akash");
    System.out.println(emp5 + ", hashcode : " + emp5.hashCode());
  }
}

此程序輸出結(jié)果如下:

Employee Constructor Called...
Employee [name=Naresh], hashcode : -1968815046
Employee Constructor Called...
Employee [name=Rishi], hashcode : 78970652
Employee Constructor Called...
Employee [name=Yogesh], hashcode : -1641292792
Employee [name=Atul], hashcode : 2051657
Employee [name=Akash], hashcode : 63313419

以上內(nèi)容是關(guān)于java創(chuàng)建對(duì)象的5種不同方法,希望給大家學(xué)習(xí)java時(shí)有所幫助。也謝謝大家對(duì)腳本之家的支持。

相關(guān)文章

  • idea2022創(chuàng)建javaweb項(xiàng)目步驟(超詳細(xì))

    idea2022創(chuàng)建javaweb項(xiàng)目步驟(超詳細(xì))

    本文主要介紹了idea2022創(chuàng)建javaweb項(xiàng)目步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-07-07
  • xxl-job定時(shí)任務(wù)配置應(yīng)用及添加到springboot項(xiàng)目中實(shí)現(xiàn)動(dòng)態(tài)API調(diào)用

    xxl-job定時(shí)任務(wù)配置應(yīng)用及添加到springboot項(xiàng)目中實(shí)現(xiàn)動(dòng)態(tài)API調(diào)用

    XXL-JOB是一個(gè)分布式任務(wù)調(diào)度平臺(tái),其核心設(shè)計(jì)目標(biāo)是開(kāi)發(fā)迅速、學(xué)習(xí)簡(jiǎn)單、輕量級(jí)、易擴(kuò)展,本篇文章主要是對(duì)xuxueli的xxl-job做一個(gè)簡(jiǎn)單的配置,以及將其添加到自己已有的項(xiàng)目中進(jìn)行api調(diào)用,感興趣的朋友跟隨小編一起看看吧
    2024-04-04
  • 使用spring工廠讀取property配置文件示例代碼

    使用spring工廠讀取property配置文件示例代碼

    這篇文章主要介紹了使用spring工廠讀取property配置文件示例代碼,具有一定借鑒價(jià)值,需要的朋友可以參考下
    2018-01-01
  • Spring boot配置文件加解密詳解

    Spring boot配置文件加解密詳解

    這篇文章主要給大家介紹了關(guān)于Spring boot配置文件加解密的相關(guān)資料,文中通過(guò)示例代碼以及圖文介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03
  • java轉(zhuǎn)樹(shù)形結(jié)構(gòu)工具類詳解

    java轉(zhuǎn)樹(shù)形結(jié)構(gòu)工具類詳解

    這篇文章主要為大家詳細(xì)介紹了java轉(zhuǎn)樹(shù)形結(jié)構(gòu)工具類,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-08-08
  • springboot 集成支付寶支付的示例代碼

    springboot 集成支付寶支付的示例代碼

    這篇文章主要介紹了springboot 集成支付寶支付的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-06-06
  • 基于Mybatis Plus實(shí)現(xiàn)多表分頁(yè)查詢的示例代碼

    基于Mybatis Plus實(shí)現(xiàn)多表分頁(yè)查詢的示例代碼

    這篇文章主要介紹了基于Mybatis Plus實(shí)現(xiàn)多表分頁(yè)查詢的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-12-12
  • 一文徹底搞懂Java日期時(shí)間類詳解

    一文徹底搞懂Java日期時(shí)間類詳解

    這篇文章主要給大家介紹了關(guān)于Java日期時(shí)間類的相關(guān)資料,Calendar類的功能要比Date類強(qiáng)大很多,可以方便的進(jìn)行日期的計(jì)算,獲取日期中的信息時(shí)考慮了時(shí)區(qū)等問(wèn)題,需要的朋友可以參考下
    2023-10-10
  • SpringBoot下無(wú)節(jié)制和數(shù)據(jù)庫(kù)建立連接的問(wèn)題及解決方法

    SpringBoot下無(wú)節(jié)制和數(shù)據(jù)庫(kù)建立連接的問(wèn)題及解決方法

    本文介紹了無(wú)節(jié)制建立MySQL連接的危害,包括數(shù)據(jù)庫(kù)服務(wù)端資源耗盡、應(yīng)用端性能劣化和監(jiān)控與運(yùn)維困境,提出了系統(tǒng)性解決方案,包括連接池標(biāo)準(zhǔn)化配置、代碼規(guī)范與防御式編程、全鏈路監(jiān)控體系和架構(gòu)級(jí)優(yōu)化,感興趣的朋友一起看看吧
    2025-03-03
  • Java基礎(chǔ)字符編碼與內(nèi)存流詳細(xì)解讀

    Java基礎(chǔ)字符編碼與內(nèi)存流詳細(xì)解讀

    這篇文章主要給大家介紹了關(guān)于Java中方法使用的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-08-08

最新評(píng)論

辛集市| 开平市| 深圳市| 虎林市| 剑河县| 阿拉善左旗| 诸城市| 剑川县| 耿马| 广平县| 高邑县| 洛浦县| 江口县| 修武县| 抚宁县| 宜川县| 舟曲县| 石阡县| 南平市| 安庆市| 英吉沙县| 酉阳| 昭平县| 获嘉县| 乌苏市| 南充市| 新绛县| 贵港市| 浙江省| 梓潼县| 太谷县| 彭州市| 清苑县| 石狮市| 万州区| 兴城市| 屏南县| 射洪县| 湘阴县| 秦安县| 沧州市|