1:各类型如果定义的时候没赋初值,那么直接system.out.print();那么会是什么了?int,String,int[]
2:在main中抛出异常,但是在抛出前 xx异常 = null;然后抛出这个异常。会是什么结果。
3:类A,B,C.都有init()方法,B继承A,C继承B。在C中如何调用A的init()方法,是super.super.init()吗?
2:在main中抛出异常,但是在抛出前 xx异常 = null;然后抛出这个异常。会是什么结果。
3:类A,B,C.都有init()方法,B继承A,C继承B。在C中如何调用A的init()方法,是super.super.init()吗?
等我有环境的时候试下,现在没有
int i;
system.out.print(i);
这样编译不过吗?
class A{
int num;//可以
void B(){
int num//不可以,这里必须赋初值
}
}
在构造函数里调用,public A(){this.init();}
当你创建一个C的对象的是后就会自动调用了!
如果方法一摸一样,就是override了,这样子类中定义的方法就把父类中的覆盖掉了,但如果是这样:
class A {
void init(){
System.out.println("a");
}
} class B extends A{
void init(int a){
System.out.println("b");
}
}class C extends B{
void init(String str){
System.out.println("c");
}
public static void main(String[] args){
C c=new C();
c.init();
}
}
那么这就是overload了,这样在子类中调用无参的init方法就会自动找到父类的父类了
string = null ,int =0 , int[] =null.
3. 如果init()没有被重写.重载过,那么直接实例化就可以调用了.如果被重写.重载过,只能实例化a来得到init()方法.
方法里面是编译不过去的,在类里面 int类型和int[]赋0,String类型赋null.我是1.4的,刚测试的.第2个问题大意如下:
[code=Java]
class ThrowsDemo
{
static void procedure() throws IllegalAccessException //throws用在函数声明
{
System.out.println("inside procedure");
IllegalAccessException e = null;//题目中的写法
//IllegalAccessException e = new IllegalAccessException() ;//正常的写法 throw e; //throw 用在这函数体内
}
public static void main(String args[])
{
try {
procedure( );
}catch (IllegalAccessException e)
{
System.out.println("caught " + e);
}
}
}[code]原题不是这个异常,我忘了.我把它扩充成1个程序了.报java.lang.NullPointerException的错误第3个问题:好象是不可以的.
重载方法是可以的
public class hello {
public static void main(String[] args){
System.out.println("hello world!");
A r;
A a = new A();
B b = new B();
C c = new C();
r = c;
r.init();
}
}class A{
void init(){
System.out.println("A");
}
}class B extends A{
void init( char a){
System.out.println("B");
}
}class C extends B{
void inint( int a ){
System.out.println("C");
}
}
在类中定义的话,在创建实例时,会先将值初始化(0,false,null),然后才调用构造函数,所以没问题