java运行时总抛出异常,好像是说未初始化,大家帮看一下,谢谢
import java.util.*;
import java.math.*;
abstract class LotteryTicket{
int[] result;
int[] custom_result;
abstract void getTicket();
abstract void judge();
}
class Lottery_5in21 extends LotteryTicket{
Lottery_5in21(){
int[] result=new int[5];
int[] custom_result=new int[5];
}
}
public class TicketTest{
public static void main(String[] args){
Lottery_5in21 t=new Lottery_5in21();
System.out.println(t.result[0]);
}
}
出错的就是这部分,谢谢
------解决方案--------------------你要t.result[0],这里的result不是你Lottery_5in21的result,是父类的,因为你的初始化在子类的构造方法里初始化的。而父类的并没有初始化。
你要改成这样:
abstract class LotteryTicket{
int[] result;
int[] custom_result;
abstract void getTicket();
abstract void judge();
}
class Lottery_5in21 extends LotteryTicket{
Lottery_5in21(){
super.result=new int[5];
super.custom_result=new int[5];
}
}
public class TicketTest{
public static void main(String[] args){
Lottery_5in21 t=new Lottery_5in21();
System.out.println(t.result[0]);
}
}