Java -Instanz einer Klasse
// instanceof can be used to check whether an object
// is an instance of a given class or not, as illustrated below
public class Main {
public static void main(String[] args) {
String str = new String("Foo");
System.out.println( str instanceof String); // true
Shape s = new Triangle();
// s is instance of parent class Shape
System.out.println(s instanceof Shape); // true
// s is also instance of child class called Triangle
System.out.println(s instanceof Triangle); // true
}
}
class Shape {
public void draw() {
System.out.println("Shape draw.");
}
}
class Triangle extends Shape {
public void draw() {
System.out.println("Triangle draw.");
}
}
Wissam