Folge dem Video um zu sehen, wie unsere Website als Web-App auf dem Startbildschirm installiert werden kann.
Anmerkung: Diese Funktion ist in einigen Browsern möglicherweise nicht verfügbar.
/**
*
*/
package de.tutorials;
/**
* @author Tom
*
*/
public class A {
private String data;
private int someMagicValue;
/**
* default Constructor
*/
public A() {}
public A(String data, int someMagicValue) {
this.data = data;
this.someMagicValue = someMagicValue;
}
/**
* copy Constructor
*
* @param a
*/
public A(A a) {
this.data = a.data;
this.someMagicValue = a.someMagicValue;
}
public String getData() { return data; }
public void setData(String data) { this.data = data; }
public int getSomeMagicValue() { return someMagicValue; }
public void setSomeMagicValue(int someMagicValue) {
this.someMagicValue = someMagicValue;
}
}
/**
*
*/
package de.tutorials;
/**
* @author Tom
*
*/
public class B {
private String data;
private int someMagicValue;
/**
* default Construtor
*/
public B() {}
public B(String data, int someMagicValue) {
this.data = data;
this.someMagicValue = someMagicValue;
}
/**
* copy Constructor
* @param b
*/
public B(B b) {
this.data = b.data;
this.someMagicValue = b.someMagicValue;
}
public String getData() { return data; }
public void setData(String data) { this.data = data; }
public int getSomeMagicValue() { return someMagicValue; }
public void setSomeMagicValue(int someMagicValue) {
this.someMagicValue = someMagicValue;
}
}
/**
*
*/
package de.tutorials;
/**
* @author Tom
*
*/
public class DynamicInstanceCopyExample {
/**
* @param args
*/
public static void main(String[] args) throws Exception {
for (int i = 0; i < 10; i++) {
int rnd = (int) (Math.random() * 10);
Object o = null;
if (rnd > 4) {
o = new A("" + System.currentTimeMillis(), i);
} else {
o = new B("" + System.currentTimeMillis(), i);
}
Object newInstanceOfO = copyObject(o);
System.out.println("Copy result " + o);
}
}
private static Object copyObject(Object o) throws Exception {
Class clazz = o.getClass();
return clazz.getDeclaredConstructor(new Class[] { clazz }).newInstance(
new Object[] { o });
}
}
private static Object copyObject(Object o) throws Exception {
Class clazz = o.getClass();
if (A.class.equals(clazz)) {
A a = new A((A) o);
return a;
} else if (B.class.equals(clazz)) {
B b = new B((B) o);
return b;
}
//Oder if (o instanceof A) { return new A( (A) o); } ...
return null;
}