zoukankan      html  css  js  c++  java
  • Java 中创建对象的 5 种方式!

    Java中有5种创建对象的方式,下面给出它们的例子还有它们的字节码

    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种方式创建Employee对象。

    public class ObjectCreation {   
       public static void main(String... args) throws Exception {   
           // 1.使用new关键字
           Employee emp1 = new Employee();   
           emp1.setName("Naresh");   
           System.out.println(emp1 + ", hashcode : " + emp1.hashCode());   
           // 2.  使用Class类的newInstance方法
           Employee emp2 = (Employee) Class.forName("org.programming.mitra.exercises.Employee")   
                                  .newInstance();   
           // 或者这样
           // Employee emp2 = Employee.class.newInstance();   
           emp2.setName("Rishi");   
           System.out.println(emp2 + ", hashcode : " + emp2.hashCode());   
           // 3.使用Constructor类的newInstance方法  
           Constructor<Employee> constructor = Employee.class.getConstructor();   
           Employee emp3 = constructor.newInstance();   
           emp3.setName("Yogesh");   
           System.out.println(emp3 + ", hashcode : " + emp3.hashCode());   
           // 4.使用clone方法
           Employee emp4 = (Employee) emp3.clone();   
           emp4.setName("Atul");   
           System.out.println(emp4 + ", hashcode : " + emp4.hashCode());   
           // 5.使用反序列化  
           // 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());   
       }   
    }
  • 相关阅读:
    Silverlight应用小知识点
    svn如何给新文件加锁
    sql统计上周销售量的起止时间
    sql中null 和 ‘’(空字符串)
    蓝桥杯 算法训练 操作格子 [ 线段树 ]
    90分 蓝桥杯 算法提高 道路和航路 [ 最短路 ]
    蓝桥杯 算法训练 安慰奶牛 [ 最小生成树 ]
    蓝桥杯 算法提高 金属采集 [ 树形dp 经典 ]
    转 蓝桥杯 历届试题 大臣的旅费 [ dfs 树的直径 ]
    蓝桥杯 算法训练 最短路 [ 最短路 bellman ]
  • 原文地址:https://www.cnblogs.com/heqiyoujing/p/10765925.html
Copyright © 2011-2022 走看看