尚硅谷JavaSE進階第8章枚舉與註解8.3練習

8.3 練習

1ide

聲明Week枚舉類,其中包含星期一至星期日的定義;測試

TestWeek類中聲明方法中printWeek(Week week),根據參數值打印相應的中文星期字符串。大數據

提示,使用switch語句實現。ui

main方法中從命令行接收一個1-7的整數(使用Integer.parseInt方法轉換),分別表明星期一至星期日,打印該值對應的枚舉值,而後以此枚舉值調用printWeek方法,輸出中文星期。spa

 

2命令行

1.編寫一個Person類,使用Override註解它的toString方法blog

2.自定義一個名爲「MyTiger」的註解類型,它只能夠使用在方法上,帶一個String類型的value屬性,而後在第1題中的Person類上正確使用。繼承

 

3、判斷輸出結果爲什麼?教程

public class Test {接口

     public Test() {

          Inner s1 = new Inner();

          s1.a = 10;

          Inner s2 = new Inner();

          s2.a = 20;

          Test.Inner s3 = new Test.Inner();

          System.out.println(s3.a);

     }

 

 class Inner {

          public int a = 5;

     }

 

     public static void main(String[] args) {

          Test t = new Test();

          Inner r = t.new Inner();

          System.out.println(r.a);

         }

      }

4、擴展題

答案:

public class TestAnonymous2 {

 

public static void main(String[] args) {

Test.function().method();

}

 

}

interface Inter{

void method();

}

class Test{

public static Inter function(){

return new Inter(){

 

@Override

public void method() {

System.out.println("測試");

}};

}

}

5、我沒有指定父類,也沒有實現接口,還能不能寫匿名內部類。

答案:

new Object(){

void fun(){}

}.fun();

6、對成員內部類的繼承說明

public class Test1 extends WithInner.Inner {

Test1(WithInner w){

w.super();

}

}

class WithInner{

class Inner{

}

}

本教程由尚硅谷教育大數據研究院出品,如需轉載請註明來源,歡迎你們關注尚硅谷公衆號(atguigu)瞭解更多。

相關文章
相關標籤/搜索