0
代碼1:CODE1Overriding-爲什麼長被稱爲的不是int
class Tyre
{
public void front() throws RuntimeException
{
System.out.println("Tire");
}
public void front(int a)
{
System.out.println("Radial Tire with int in tyre");
}
public void front(long a)
{
System.out.println("Radial Tire with long");
}
}
class RadialTyre extends Tyre
{
public void front()
{
System.out.println("Radial Tire");
}
public void front(int a)
{
System.out.println("Radial Tire with int");
}
}
class Test
{
public static void main(String... args)
{
Tyre t = new RadialTyre();
int a = 10;
t.front(a);
}
}
O/P:-Radial輪胎爲int
代碼2: -
class Tyre
{
public void front() throws RuntimeException
{
System.out.println("Tire");
}
public void front(long a)
{
System.out.println("Radial Tire with long");
}
}
class RadialTyre extends Tyre
{
public void front()
{
System.out.println("Radial Tire");
}
public void front(int a)
{
System.out.println("Radial Tire with int");
}
}
class Test
{
public static void main(String... args)
{
Tyre t = new RadialTyre();
int a = 10;
t.front(a);
}
}
O/p for code2: - 長徑向輪胎
爲什麼在code1中調用child class int方法而在code2中調用父類long方法?如果這是由於擴大而發生的,那麼爲什麼case1擴展不會發生?在code1中爲什麼當父類中已經存在一個int方法時調用子類的int方法?
感謝您回答我的第一個問題。如果Tire不知道RadialTyre,那麼爲什麼RadialTyre的int方法在代碼1中被執行而不是來自Tyre類的int方法?這些int方法肯定會被覆蓋。 – Snehal
@Snehai也回答了第三個問題。但是,我的主要語言不是英語,我從來沒有聽說過「擴大」。我正在維基百科上閱讀它,它似乎與您的問題沒有關係。你能詳細說明嗎? – BackSlash
你可以參考這個理解拓寬https://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html – Snehal