- 相關推薦
Java中對象類型如何進行轉換
導語:Java中對象類型如何進行轉換呢?下面是小編給大家提供的Java中對象類型的強制轉換代碼實現,大家可以參考閱讀,更多詳情請關注應屆畢業生考試網。
class person
{
void f1()
{
System.out.println("person f1 is calling !");
}
void f2()
{
f1();
}
}
class student extends person
{
void f1()
{
System.out.println("student f1 is calling! ");
}
void f3()
{
System.out.println("student f3 is calling!");
}
void f4()
{}
}
class Rt20
{
public static void main(String[]args)
{
student s=new student();
call(s);
}
public static void call(person p)//子類的對象可以自動轉換為父類的對象.
{
if(p instanceof student)//這句意思:p確實是student的對象嗎.
{
student s=(student)p;//把person類型強制轉換為student類型.
s.f1();
s.f2();
s.f3();
}
else
{
p.f1();
p.f2();
}
//p.f4();//p只能調用person類的內容,雖然說p來源于student .但是它帶上person類的
//帽子,所以只能調用person類的成員.
}
}
【Java中對象類型如何進行轉換】相關文章:
Java如何完成數據類型轉換02-27
Java數據類型轉換08-04
java類型的字符轉換的方法02-26
Java中float類型的范圍及其與十六進制的轉換方法06-18
Java如何面向對象08-15
如何理解Java面向對象07-06
Java中創建對象的方式08-02