JAva構築方法
3693 ワード
FxのFx()は既にsuper()を呼び出しており、Fx(int i)もthis()を呼び出しているので、super()も間接的に呼び出しているので、new Fx(5)の場合はthis()によって親コンストラクタEx()を明示的に動かす
public class Ex {
Ex() {
System.out.println("Ex,no-args");
}
Ex(int i) {
System.out.println("Ex,int");
}
public static void main(String[] args) {
Fx f = new Fx(5);
}
}
class Fx extends Ex {
Fx() {
super();
System.out.println("Fx,no-args");
}
Fx(int i) {
this();
System.out.println("Fx,int");
}
}
public class Obj implements Cloneable {
private int a = 0;
public int getA() {
return a;
}
public void setA(int a) {
this.a = a;
}
public Obj() {
System.out.println("sonstruct");
}
@Override
protected Object clone() throws CloneNotSupportedException {
Object o = null;
o = (Obj)super.clone();
return o;
}
public static void main(String[] args) {
Obj a = new Obj();
Obj b = null;
a.setA(11);
try {
b = (Obj)a.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
System.out.println(a.getA());
a.setA(99);
System.out.println(b.getA());
}
}
class Person{
String name = "H";
public Person() {
System.out.println("construct");
}
@Override
public String toString() {
return name;
}
}
public class Test {
public static void main(String[] args) {
Class clazz;
try {
clazz = Class.forName("Person");
Person person = (Person)clazz.newInstance();
System.out.println(person);
} catch (Exception e) {
e.printStackTrace();
}
}
}
public class People implements Serializable{
private String name;
public People() {
this.name = "OOOO";
System.out.println("construct");
}
@Override
public String toString() {
return name;
}
public static void main(String[] args) {
People p = new People();
System.out.println(p);
ObjectOutputStream oos = null;
ObjectInputStream ois = null;
try {
FileOutputStream fos = new FileOutputStream("people.out");
oos = new ObjectOutputStream(fos);
oos.writeObject(p);
oos.close();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
People p1;
try {
FileInputStream fis = new FileInputStream("people.out");
ois = new ObjectInputStream(fis);
p1 = (People)ois.readObject();
System.out.println(p1);
if (p != p1) {
System.out.println("tow object");
}
ois.close();
fis.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}