Java创建对象的方式

目录

一、使用new运算符创建对象

二、使用Class类的newlnstance方法

三、使用Constructor类的newlnstance方法

四、反序列化创建对象

五、clone()创建对象

总结


一、使用new运算符创建对象

public class test1
{
	public test1()
	{
		System.out.println("这是构造函数");
	}
	
	public static void main(String[] args)
	{
		test1 t=new test1();
	}
}

console:这是构造函数

二、使用Class类的newlnstance方法

test1.java

public class test1
{
	public test1()
	{
		System.out.println("这是test1的构造函数");
	}

}

//----------------------------------------------------------------
test2.java

public class test2
{
	public static void main(String[] args)
	{
		Class obj=test1.class;
		try
		{
			Object targetObj = obj.newInstance();
		} catch (InstantiationException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

Console:这是test1的构造函数


三、使用Constructor类的newlnstance方法

test1.java

public class test1
{
	public test1()
	{
		System.out.println("这是test1的无参构造函数");
	}
	
	public test1(String name)
	{
		System.out.println("这是test1的有参构造函数");
	}

}

//-------------------------------------------------------------------------------

test2.java

public class test2
{
	public static void main(String[] args)
	{
		Class obj=test1.class;
		try
		{	//调用有参
			Constructor declaredConstructor = obj.getDeclaredConstructor(String.class);
			Object newInstance = declaredConstructor.newInstance("张三");
			//调用无参
			Constructor declaredConstructor1 = obj.getDeclaredConstructor(null);
			Object newInstance1 = declaredConstructor1.newInstance(null);
		} catch (NoSuchMethodException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SecurityException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InstantiationException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalArgumentException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	
    }
}

Console:这是test1的有参构造函数
        这是test1的无参构造函数

四、反序列化创建对象

test1.java

public class test1 implements Serializable
{
	private String name;
	private int age;
	
	public test1(String name,int age)
	{
		this.name=name;
		this.age=age;
	}

	@Override
	public String toString()
	{
		return "test1 [姓名=" + name + ", 年龄=" + age + "]";
	}
}

//--------------------------------------------------------------------------
test2.java

public class test2
{
	public void writeObj()
	{
		//序列化存入对象
		try
		{
			ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("E:/targetObject.txt"));
			test1 t=new test1("张三", 23);
			out.writeObject(t);
			out.close();
			System.out.println("------存入成功-------");
			
		} catch (FileNotFoundException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }
	public void readerObj()
	{
		try
		{
			ObjectInputStream in= new ObjectInputStream(new FileInputStream("E:/targetObject.txt"));
			Object readObject = in.readObject();
			test1 targetObj=(test1)readObject;
			System.out.println(targetObj.toString());
			in.close();
		} catch (FileNotFoundException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args)
	{
		test2 t2=new test2();
		//t2.writeObj();
		t2.readerObj();	
	}		
}

Console:test1 [姓名=张三, 年龄=23]

五、clone()创建对象

test1.java

public class test1 implements Cloneable
{
	private String name;
	private int age;
	
	public test1(String name,int age)
	{
		this.name=name;
		this.age=age;
	}

	@Override
	protected test1 clone() throws CloneNotSupportedException
	{
		// TODO Auto-generated method stub
	   test1 obj= (test1) super.clone();
      	return obj;
	}

	public String getName()
	{
		return name;
	}

	public void setName(String name)
	{
		this.name = name;
	}

	public int getAge()
	{
		return age;
	}

	public void setAge(int age)
	{
		this.age = age;
	}
}

//-----------------------------------------------------------------------------

test2.java

public class test2
{
	public static void main(String[] args)
	{
		//源对象
		test1 t1 = new test1("张三", 24);
		//克隆对象
		try
		{
			test1 clone = t1.clone();
			System.out.println("克隆对象=="+clone.getName()+"--"+clone.getAge());
		} catch (CloneNotSupportedException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}		
}

总结

1、Class类的newlnstance和Constructor的newlnstance的方法构造对象都是使用的反射;

2、克隆和反序列化构造对象是不需要构造函数的。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值