Java对象创建方法

查阅网上诸多博客,觉得这篇是写的最完善的(虽然似乎这篇也是转载的、却没有给出转出网址)
主要参考:
http://blog.csdn.net/mhmyqn/article/details/7943411

显式

  1. 使用new关键字创建对象,这是最常用的创建对象的方式。
  2. 运用Java**反射机制,调用**java.lang.Class或者java.lang.reflect.Constructor类的newInstance()实例方法。
  3. 调用对象的clone()方法。 但是必须实现Cloneable接口,重写Object.clone()方法。
  4. 运用反序列化手段(类要实现序列化接口),调用java.io.ObjectInputStream对象的readObject()方法。

隐式

  1. 对于java命令中的每个命令行参数,Java虚拟机都会创建相应的String对象,并把它们组织到一个String数组中,再把该数组作为参数传给程序入口main(String args[])方法。
  2. 程序代码中的String类型的直接数对应一个String对象。(参见:http://blog.csdn.net/u012440687/article/details/51981198
  3. 字符串操作符“+”的运算结果为一个新的String对象。(同样参见上篇链接博客,有详细介绍String类)
  4. 当Java虚拟机加载一个类时,会隐含地创建描述这个类的Class实例(…)。

用new语句或Class对象的newInstance()方法创建LearnStrings 对象时,都会执行LearnStrings 类的构造方法,而用对象的clone()方法创建LearnStrings 对象时,不会执行LearnStrings 类的构造方法。

package xianggen.others;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

/**
 * Java创建对象方法!!
 * @author xianggen
 * @date 2016年7月21日 上午11:36:42
 */
public class CreateObjectMethods implements Cloneable,Serializable {
    /**
     * 
     */
    private static final long serialVersionUID = -163156355527604072L;
    private String name;
    private int age;

    public CreateObjectMethods() {
        this("unknown", 0);
        System.out.println("call default constructor");
    }

    public CreateObjectMethods(String name, int age) {
        this.name = name;
        this.age = age;
        System.out.println("call second constructor");
    }

    // 实现Cloneable接口
    public Object clone() throws CloneNotSupportedException {
        return super.clone();
    }

    public boolean equals(Object o) {
        if (this == o)
            return true;
        if (!(o instanceof CreateObjectMethods))
            return false;
        final CreateObjectMethods other = (CreateObjectMethods) o;
        if (this.name.equals(other.name) && this.age == other.age)
            return true;
        else
            return false;
    }

    public String toString() {
        return "name=" + name + ",age=" + age;
    }
    private static String filename = CreateObjectMethods.class.getResource("")
            .getPath() + "/obj.txt";
    static File file = new File(filename);
    static {
        if (!file.exists())
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }

    }

    public static void main(String args[]) throws Exception {
        // 1. 运用反射手段创建CreateObjectMethods对象,调用构造函数
        Class objClass = Class.forName("xianggen.others.CreateObjectMethods"); // 这个类CreateObjectMethods一定要写全地址,否则会抛出异常!
        CreateObjectMethods object1 = (CreateObjectMethods) objClass.newInstance(); // 会调用CreateObjectMethods类的默认构造方法
        System.out.println("object1: " + object1); // 打印name=unknown,age=0

        // 2. 用new语句创建CreateObjectMethods对象,调用构造函数
        CreateObjectMethods object2 = new CreateObjectMethods("Tom", 20);
        System.out.println("object2: " + object2); // 打印name=tom,age=20

        // 3. 运用克隆手段创建CreateObjectMethods对象,不调用构造函数
        CreateObjectMethods object3 = (CreateObjectMethods) object2.clone(); // 不会调用CreateObjectMethods类的构造方法
        System.out.println("object2==object3 : "
                + (object2 == object3)); // 打印false
        System.out.println("object2.equals(object3) : "
                + object2.equals(object3)); // 打印true
        System.out.println("object3: " + object3); // 打印name=tom,age=20

        // 4.用对象流来实现 前提是对象必须实现 Serializable,不调用构造函数

        @SuppressWarnings("resource")
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(
                new FileOutputStream(filename));
        objectOutputStream.writeObject(object1);

        @SuppressWarnings("resource")
        ObjectInput input = new ObjectInputStream(new FileInputStream(filename));

        CreateObjectMethods object4 = (CreateObjectMethods) input.readObject();
        System.out.println("object4: " +object4);
//      System.out.println("filename="+filename);

    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值