java基礎面試題

java基礎面試題

面試題1

public class Test {
    public static void main(String[] args) {
        Parent p = new Child();
        System.out.println("Parent:" + p.getAge()); //40
        System.out.println("Parent:" + p.age); //18 
        Child c = new Child();
        System.out.println("Child:" + c.getAge()); //40
        System.out.println("Child:" + c.age); //40
    }
}


class Child extends Parent {
    public Integer age = 40;
      public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}


class Parent {
    public Integer age = 18;
      public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}

輸出結果

Parent:40
Parent:18
Child:40
Child:40

原因:

  • 屬性屬於實例自己的**,所以Parent的age屬性值是18,這就解釋通了
  • 屬性不存在覆蓋(即使同名),而方法是實實在在的覆蓋(複寫)

當父類調用getAge()方法時會調用子類的方法

面試題2

public class Test {
    public static void main(String[] args) {
        Parent p= new Child();
        System.out.println(p.age);
        System.out.println("---------");
        Child c= new Child();
        System.out.println(c.age);
    }
}


class Child extends Parent {
    Integer age=5;
    static {
        System.out.println("Child的靜態塊");
    }
    {
        System.out.println("Child的構造塊age="+age);
    }
    Child() {
        System.out.println("Child的構造方法age="+age);
    }
    public Integer getAge() {
        return age;
    }
}


class Parent {
    public static Integer sex=20;
    Integer age = 18;
    static {
        System.out.println("Parent的靜態塊sex="+sex);
    }
    {
        System.out.println("Parent的構造塊age="+age);
    }
    Parent() {
        System.out.println("Parent的構造方法age="+age);
    }

    public Integer getAge() {
        return age;
    }
}

輸出

Parent的靜態塊sex=20
Child的靜態塊
Parent的構造塊age=18
Parent的構造方法age=18
Child的構造塊age=5
Child的構造方法age=5
18
---------
Parent的構造塊age=18
Parent的構造方法age=18
Child的構造塊age=5
Child的構造方法age=5
5

類的生命週期是:加載->驗證->準備->解析->初始化->使用->卸載。

類的準備階段`:需要做是爲類變量(static變量)分配內存並設置`默認值
需要注意的是,如果類變量是final的,編譯時javac就會爲它賦上值

1、靜態變量設置默認值

static byte b=(byte)0;
static short s=(short)0;
static int i=0;
static long l=0;
static char c='\u0000' 0; //代表空格
static boolean bol=false;
static float f=0f;
static double d=0d;

2、靜態變量賦值

3、靜態代碼塊

4、成員變量設置默認值

5、成員變量賦值

6、父類構造代碼塊

7、父類構造函數

8、構造代碼塊

9、構造函數

面試題3

public class StaticTest {

    public static void main(String[] args) {
        staticFunction();
    }

    // 靜態變量(有實例化的過程,這就是本題的重點)
    static StaticTest st = new StaticTest();
    static {
        //System.out.println(b); // 編譯報錯:因爲b在構造代碼塊後邊,此處不能引用。因此Java代碼是從上到下的順序
        System.out.println("1");
    }
    {
        System.out.println("2");
    }
    StaticTest() {
        System.out.println("3");
        System.out.println("a=" + a + ",b=" + b);
    }
    public static void staticFunction() {
        System.out.println("4");
    }

    // 這兩個變量寫在最後面
    int a = 110;
    static int b = 112;
}

結果

2
3
a=110,b=0
1
4
發佈了61 篇原創文章 · 獲贊 48 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章