class A {}
class B : A {}
class C : B {}
class D : C {}
class TestInheritance {
public static void Main(string[] args) {
object o = new C();
if (o is B)
System.Console.WriteLine("I'm a B");
if (o.GetType() == typeof(B))
System.Console.WriteLine("I'm the B");
if (o is C)
System.Console.WriteLine("I'm a C");
if (o.GetType() == typeof(C))
System.Console.WriteLine("I'm the C");
}
}
Checking the type on Java
class A {}
class B extends A {}
class C extends B {}
class D extends C {}
class TestInheritance {
public static void main(String[] args) {
Object o = new C();
if (o instanceof B)
System.out.println("I'm a B");
if (o.getClass() == B.class)
System.out.println("I'm the B");
if (o instanceof C)
System.out.println("I'm a C");
if (o.getClass() == C.class)
System.out.println("I'm the C");
}
}
Both languages has this output:
I'm a B I'm a C I'm the C
No comments:
Post a Comment