目录
三、使用Constructor类的newlnstance方法
一、使用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、克隆和反序列化构造对象是不需要构造函数的。