Hallo,
ich hab folgende Klasse gegeben, wo ich nur die Methodenrümpfe verändern darf. Sowie außerhalb der Methodenrümpfe, z.B für Attribute.
Jetzt hab ich bisher das, was gerade im Anfangsstadium ist. Zum Beispiel hab ich noch keine Verbindung zwischen ref und id und get ist noch falsch. Nur komm ich nicht weiter und würde gerne um Tipps bitten.
ich hab folgende Klasse gegeben, wo ich nur die Methodenrümpfe verändern darf. Sowie außerhalb der Methodenrümpfe, z.B für Attribute.
Code:
interface Freeable {
default void free() {
System.out.println(this + " is now free!");
}
}
final public class SmartPointer<T extends Freeable> {
/**
* @returna SmartPointer pointing to a newly allocated T.
*/
public static <T extends Freeable> SmartPointer<T> make(T target) throws IllegalArgumentException;
/**
* @return a SmartPointer pointing to nothing.
*/
public static <T extends Freeable> SmartPointer<T> nullPointer();
/**
* @return true iff X is a NullPointer
*/
public boolean isNull();
/**
* @return the target of this pointer or null iff isNull()
*/
public T get();
/**
* After assigning this to to, to Points to the same Object as from
*/
public void assign(SmartPointer<T> to);
/**
* Releases the pointer. Afterwards, X is a NullPointer.
*/
public void release();
/**
* Prints the Object, that this points to. Prints an error message iff
* isNull().
*/
@Override
public String toString();
}
Jetzt hab ich bisher das, was gerade im Anfangsstadium ist. Zum Beispiel hab ich noch keine Verbindung zwischen ref und id und get ist noch falsch. Nur komm ich nicht weiter und würde gerne um Tipps bitten.
Code:
interface Freeable {
default void free() {
System.out.println(this + " is now free!");
}
}
final public class SmartPointer<T extends Freeable> {
private SmartPointer<T> smartpointer;
private String ref;
private T id;
public SmartPointer(T object) {
smartpointer = new SmartPointer<>(object);
ref = null;
id = null;
}
public SmartPointer() {
smartpointer = new SmartPointer<>();
}
/**
* @return a SmartPointer pointing to a newly allocated T.
*/
public static <T extends Freeable> SmartPointer<T> make(T target) throws IllegalArgumentException {
SmartPointer<T> smartpointer2 = new SmartPointer<>(target);
return smartpointer2;
}
/**
* @return a SmartPointer pointing to nothing.
*/
public static <T extends Freeable> SmartPointer<T> nullPointer() {
SmartPointer <T> smartpointern = new SmartPointer<>();
return smartpointern;
}
/**
* @return true iff X is a NullPointer
*/
public boolean isNull() {
SmartPointer <T> smartpointern = new SmartPointer<>();
return true;
}
/**
* @return the target of this pointer or null iff isNull()
*/
public T get() {
if(isNull()) {
return null;
}
return null;
}
/**
* After assigning this to to, to Points to the same Object as from
*/
public void assign(SmartPointer<T> to) {
}
/**
* Releases the pointer. Afterwards, X is a NullPointer.
*/
public void release() {
}
/**
* Prints the Object, that this points to. Prints an error message iff
* isNull().
*/
@Override
public String toString() {
if(id == null) {
System.err.println("Es wird auf einen Nullzeiger gezeigt.");
}
return id.toString();
}
}