subject:คำสั่ง instanceof
syntax:object instanceof className;
content:ใช้ตรวจสอบว่า object ถูกสร้างมาจาก class หรือไม่
โดยการสืบทอดกันก็ถือว่าเป็นเงื่อนไขเดียวกัน
example:class Life
{
private String name;
protected String sex;
public Life ( String name )
{
this.name = name;
}
public void setSex ( String sex )
{
this.sex = "Super Sex :" + sex;
}
}
class Person extends Life
{
private int age;
public Person ( String name )
{
super ( name );
}
public void setSex ( String sex )
{
this.sex = sex;
}
public void setDetail ( String sex, int age )
{
super.setSex ( sex );
this.age = age;
}
}
Person bamboo = new Person ( "bamboolabcode.com" );
if ( bamboo instanceof Life )
{
System.out.println ( "bamboo created by life" );
}