- 取得連結
- X
- 以電子郵件傳送
- 其他應用程式
which two class definitions fail to compile?
在將程式碼逐行輸入編譯器後他會出現兩個錯誤訊息"The blank final field i may not have been initialized"和"The abstract method doIt in type D can only be defined by an abstract class"
第一個錯誤訊息只需將i賦值即可修正
第二個錯誤訊息則是說虛擬方法只能寫在虛擬類別裡,因此要將Class D轉為虛擬類別,或把虛擬方法doIt的abstract去掉
第二是E 在輸入類別的一開始編譯器就會跳出"The class E can be either abstract or final, not both"的錯誤訊息,字面意思:Class只能為abstract和final的其中一種
abstract class A
{
private static int i;
public void doStuff(){}
public A() {}
}
final class B
{
public B() {};
}
class C
{
private static int i;
private C() {}
}
class D
{
protected static final int i;
void doStuff() {}
abstract void doIt();
}
final abstract class E
{
protected static int i;
void doStuff(){}
abstract void doIt();
}
這題的答案為DE
class D
{
protected static final int i;
void doStuff() {}
abstract void doIt();
}
final abstract class E
{
protected static int i;
void doStuff(){}
abstract void doIt();
}
首先是D在將程式碼逐行輸入編譯器後他會出現兩個錯誤訊息"The blank final field i may not have been initialized"和"The abstract method doIt in type D can only be defined by an abstract class"
第一個錯誤訊息只需將i賦值即可修正
第二個錯誤訊息則是說虛擬方法只能寫在虛擬類別裡,因此要將Class D轉為虛擬類別,或把虛擬方法doIt的abstract去掉
第二是E 在輸入類別的一開始編譯器就會跳出"The class E can be either abstract or final, not both"的錯誤訊息,字面意思:Class只能為abstract和final的其中一種
留言
張貼留言