题目01
//自定义枚举类
package com.jerry.java; /** * @author jerry_jy * @create 2022-10-05 9:11 */ public class SeasonTest { public static void main(String[] args) { Season spring = Season.SPRING; System.out.println(spring); } } //自定义枚举类 class Season { //1.声明Season对象的属性:private final修饰 private final String seasonName; private final String seasonDesc; //2.私有化类的构造器,并给对象属性赋值 private Season(String seasonName, String seasonDesc) { this.seasonName = seasonName; this.seasonDesc = seasonDesc; } //3.提供当前枚举类的多个对象:public static final的 public static final Season SPRING = new Season("春天", "春暖花开"); public static final Season SUMMER = new Season("夏天", "夏日炎炎"); public static final Season AUTUMN = new Season("秋天", "秋高气爽"); public static final Season WINTER = new Season("冬天", "冰天雪地"); //4.其他诉求1:获取枚举类对象的属性 public String getSeasonName() { return seasonName; } public String getSeasonDesc() { return seasonDesc; } //4.其他诉求1:提供toString() @Override public String toString() { return "Season{" + "seasonName='" + seasonName + '\'' + ", seasonDesc='" + seasonDesc + '\'' + '}'; } }
题目02
enum类的使用
package com.jerry.java; /** * @author jerry_jy * @create 2022-10-05 9:31 */ public class SeasonTest1 { public static void main(String[] args) { Season1 summer = Season1.SUMMER; System.out.println(summer.toString());//Season1{seasonName='夏天', seasonDesc='夏日炎炎'} System.out.println(Season1.AUTUMN.getSeasonName());//秋天 System.out.println(Season1.WINTER.getSeasonDesc());//冰天雪地 System.out.println(Season1.class.getSuperclass());//class java.lang.Enum System.out.println("****************"); //values():返回所有的枚举类对象构成的数组 Season1[] values = Season1.values(); for (int i = 0; i < values.length; i++) { System.out.println(values[i]); values[i].show(); } System.out.println("****************"); Thread.State[] values1 = Thread.State.values(); for (int i = 0; i < values1.length; i++) { System.out.println(values1[i]); } //valueOf(String objName):返回枚举类中对象名是objName的对象。 Season1 spring = Season1.valueOf("SPRING"); spring.show();//春天在哪里? System.out.println(spring);//Season1{seasonName='春天', seasonDesc='春暖花开'} System.out.println(Season1.valueOf("WINTER1"));//Exception in thread "main" java.lang.IllegalArgumentException: No enum constant com.jerry.java.Season1.WINTER1 } } interface info { void show(); } enum Season1 implements info { SPRING("春天", "春暖花开") { @Override public void show() { System.out.println("春天在哪里?"); } }, SUMMER("夏天", "夏日炎炎") { @Override public void show() { System.out.println("宁夏"); } }, AUTUMN("秋天", "秋高气爽") { @Override public void show() { System.out.println("秋天不回来"); } }, WINTER("冬天", "冰天雪地") { @Override public void show() { System.out.println("大约在冬季"); } }; private final String seasonName; private final String seasonDesc; private Season1(String seasonName, String seasonDesc) { this.seasonName = seasonName; this.seasonDesc = seasonDesc; } public String getSeasonName() { return seasonName; } public String getSeasonDesc() { return seasonDesc; } @Override public String toString() { return "Season1{" + "seasonName='" + seasonName + '\'' + ", seasonDesc='" + seasonDesc + '\'' + '}'; } }
题目03
/* 创建月份枚举类,枚举值包含十二个月份,月份要求用英文单词 */
package com.jerry.exer; /** * @author jerry_jy * @create 2022-10-05 10:22 */ public class Exer1 { /* 创建月份枚举类,枚举值包含十二个月份,月份要求用英文单词 */ public static void main(String[] args) { System.out.println(Month.January);//January Month february = Month.valueOf("February"); System.out.println(february);//February Month[] months = Month.values(); System.out.println("=================="); for (int i = 0; i < months.length; i++) { System.out.println(months[i]); } } } enum Month{ January,February,March,April,May,June,July,August,September,October,November,December; }
题目04
/* 创建星期枚举类,有7个枚举值,包含计划属性plan,为此属性赋值(使用构造器)。 把上题的赋值修改为通过构造器赋值。 */
package com.jerry.exer; /** * @author jerry_jy * @create 2022-10-05 10:30 */ public class Exer2 { public static void main(String[] args) { MyWeek[] values = MyWeek.values(); for (int i = 0; i < values.length; i++) { System.out.println(values[i]); } System.out.println("================"); System.out.println(MyWeek.Monday.getPlan());//上课 System.out.println(MyWeek.Saturday.toString());//MyWeek{plan='放假'} } } enum MyWeek { Monday("上课"), Tuesday("上课"), Wednesday("上课"), Thursday("上课"), Friday("上课"), Saturday("放假"), Sunday("放假"); private final String plan; private MyWeek(String plan) { this.plan = plan; } public String getPlan() { return plan; } @Override public String toString() { return "MyWeek{" + "plan='" + plan + '\'' + '}'; } }
题目05
/* 创建一个Color枚举类, 1)有 RED,BLUE,BLACK,YELLOW,GREEN这个五个枚举值; 2)Color有三个属性redValue,greenValue,blueValue, 3)创建构造方法,参数包括这三个属性, 4)每个枚举值都要给这三个属性赋值,三个属性对应的值分别是red:255,0,0 blue:0,0,255 black:0,0,0 yellow:255,255,0 green:0,255,0 5)重写toString方法显示三属性的值 6)在Color中添加抽象方法meaning,不同的枚举类的meaning代表的意思各不相同 */
package com.jerry.exer; /** * @author jerry_jy * @create 2022-10-05 10:47 */ public class Exer3 { public static void main(String[] args) { System.out.println(Color.BLACK.getMeaning()); } } enum Color { RED(255, 0, 0, "红色"), BLUE(0, 0, 255, "蓝色"), BLACK(0, 0, 0, "黑色"), YELLOW(255, 255, 0, "黄色"), GREEN(0, 255, 0, "绿色"); private final int redValue; private final int greenValue; private final int blueValue; private final String meaning; Color(int redValue, int greenValue, int blueValue, String meaning) { this.redValue = redValue; this.greenValue = greenValue; this.blueValue = blueValue; this.meaning = meaning; } public int getRedValue() { return redValue; } public int getGreenValue() { return greenValue; } public int getBlueValue() { return blueValue; } public String getMeaning() { return meaning; } @Override public String toString() { return "Color{" + "redValue=" + redValue + ", greenValue=" + greenValue + ", blueValue=" + blueValue + ", meaning='" + meaning + '\'' + '}'; } }
题目06
/* 自定义annotation,里面包括1个String类型的属性,一个有默认值类型的属性,使用自定义的annotation */
package com.jerry.exer; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** * @author jerry_jy * @create 2022-10-05 11:11 */ @MyAnnotation("tom") public class Exer5 { public static void main(String[] args) { System.out.println(Exer5.class.getAnnotation(MyAnnotation.class).value());//tom } } @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) @interface MyAnnotation { String value() default "jerry"; }
题目07
/* 定义一个Student类,要求重写toString()的方法,并且此方法要使用Annotation的三个基本的注释,创建Test类,输出Student类的toString方法的所有注释 */
package com.jerry.exer; /** * @author jerry_jy * @create 2022-10-05 11:15 */ public class Exer6 { public static void main(String[] args) { Student jerry = new Student(123, "jerry"); System.out.println(jerry.toString()); jerry.print(); } } class Student { int StuID; String name; public Student(int stuID, String name) { StuID = stuID; this.name = name; } @Override public String toString() { return "重写的toString方法()"; } @Deprecated public void print() { System.out.println("过时的方法"); @SuppressWarnings("unused") int a = 1000; a=1; System.out.println("a= " + a); } }
题目08
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); } }
题目09
答案: 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("测试"); }}; } }
题目10
5、我没有指定父类,也没有实现接口,还能不能写匿名内部类。 答案: new Object(){ void fun(){} }.fun();
题目11
6、对成员内部类的继承说明 public class Test1 extends WithInner.Inner { Test1(WithInner w){ w.super(); } } class WithInner{ class Inner{ } }