首页 > 编程知识 正文

java之构造方法,java多态

时间:2023-05-05 12:01:21 阅读:147565 作者:2263

1、枚举类型

1.1 .含义:如果一个变量只有几个可能的值,可以定义为枚举类型。

例如,enum Color{red,yellow,blue,white,black}; 声明了枚举类型

然后,可以使用此类型定义变量,如enum Color color1和color2

枚举类型枚举变量

1.2 .特点

)枚举变量与其他数值类型不同,仅限于大括号中定义的值之一。

)枚举中的每个元素都表示一个整数,默认值为0、1、2、3。

(3)赋值: red=9; 错了。 enum Color{red=5,yellow=4,blue=4,white=2,black=1}是正确的。

)枚举元素的比较用初始化时指定的整数进行比较。

可以将生成方法添加到枚举类型中,但规定的生成方法必须用private修饰符限定。 通过示例说明如何使用生成方法。

2、枚举类型的成员方法

枚举类型有许多成员方法,可以将枚举类型视为集成在java.lang.Enum类中的类。 这些方法包括:

用列子说明这些方法的使用方法。

1 publicclasshowenum {2enum constants2} 3a、b、c; //可以在枚举类型中不使用分号输入常量

4 )5//作为枚举类型比较的方法,自变量作为枚举类型

6基于从publicstaticvoidcompare (constants 2c )7//values )方法返回的数组进行循环操作

8for(intI=0; i Constants2.values ().length; I ()9//返回比较结果

10system.out.println(c (和(Constants2.values ) ) [i] )的比较结果: ) c.compareto ) Constants2.values ) ) ) I ) 11 ) 12 ) 13调用publicstaticvoidmain (string [ ] args ) 14//compare方法

15公司(constants2. value of (' b ' ) ); 16system.out.println ((() ) ) ) ) ) ) (() ) ) ) ) ) ) ) ) ) ) ) 65 17 //循环中返回values的数组

18for(intI=0; I

20 System.out.println (枚举成员变量为' Constants2.values () [i] ); 21 ) 22system.out.println (() () () () ) ) ) ) ) ) ) ) ) ) ) 22system.out.println ) ) 652 23for(intI=0; I

29 }

执行结果:

B和A的比较结果:1

B和B的比较结果:0

B和C的比较结果:-1

~~~~~~~~~~~~~~~~~~~ ~

枚举成员变量为a

枚举成员变量为: b

枚举成员变量为c

~~~~~~~~~~~~~~~~~~~ ~

枚举类型a的索引位置为0

枚举类型中b的索引位置为1

枚举类型的c的索引位置为2

3、枚举类型下的构建方法

示例:

1 publicclassenumlndextest (2enum constants2)3constants_a )、4 Constants_B )、5 Constants_C )、5 consts _ c )。 7 privateString description; 8私有int I=4; 9

10专用常量2 ({ 11 }

12 ) 13

14私有约定2 (string description ) { 15 this.description=description; (十七)十七

18privateconstants2(inti ) {19 this.i=this.i i; 20 ) 21

22 publicstringgetdescription ((23返回描述); 24 ) 25

26公共信息获取((27 returni ); ) 29 ) 29

30

31 ) 32 publicstaticvoidmain (string [ ] args ) 33for ) intI=0; i Constants2.values ().length; I6534system.out.println(Consta

nts2.values()[i]+"调用getDescription()方法为:"+Constants2.values()[i].getDescription());35 }36 System.out.println(Constants2.valueOf("Constants_D")+"调用geti() 方法为:"+Constants2.valueOf("Constants_D").geti());37 }38 }

运行结果:

Constants_A调用getDescription()方法为:枚举成员A

Constants_B调用getDescription()方法为:枚举成员B

Constants_C调用getDescription()方法为:枚举成员C

Constants_D调用getDescription()方法为:null

Constants_D调用geti() 方法为:7

分析:将枚举类型中的构造方法设置为private,防止客户代码实例化一个枚举对象

上述代码中的getDescription()方法也可以放在接口中,如下代码:

在项目中创建d接口和枚举类型的AnyEnum类,在枚举类型AnyEnum类中实现带方法的接口,使每个枚举类型成员实现该接口中的方法。

1 packagecom.lzw;2 import static java.lang.System.*;3 interfaced {4 publicString getDescription();5

6 public intgetI();7 }8

9 public enum AnyEnum implementsd {10 Constants_A { //可以在枚举类型成员内部设置方法

11 publicString getDescription() {12 return ("我是枚举成员A");13 }14

15 public intgetI() {16 returni;17 }18 },19 Constants_B {20 publicString getDescription() {21 return ("我是枚举成员B");22 }23

24 public intgetI() {25 returni;26 }27 },28 Constants_C {29 publicString getDescription() {30 return ("我是枚举成员C");31 }32

33 public intgetI() {34 returni;35 }36 },37 Constants_D {38 publicString getDescription() {39 return ("我是枚举成员D");40 }41

42 public intgetI() {43 returni;44 }45 };46 private static int i = 5;47

48 public static voidmain(String[] args) {49 for (int i = 0; i < AnyEnum.values().length; i++) {50 out.println(AnyEnum.values()[i] + "调用getDescription()方法为:"

51 +AnyEnum.values()[i].getDescription());52 out.println(AnyEnum.values()[i] + "调用getI()方法为:"

53 +AnyEnum.values()[i].getI());54 }55 }56 }

运行结果:

Constants_A调用getDescription()方法为:我是枚举成员A

Constants_A调用getI()方法为:5

Constants_B调用getDescription()方法为:我是枚举成员B

Constants_B调用getI()方法为:5

Constants_C调用getDescription()方法为:我是枚举成员C

Constants_C调用getI()方法为:5

Constants_D调用getDescription()方法为:我是枚举成员D

Constants_D调用getI()方法为:5

4、使用枚举类型设置常量

通常在接口中设置常量,并且该常量不能被修改。因为在接口中定义常量时,该常量被修饰为static和final类型。在调用时不能检测参数的类型,而枚举类型定义的常量参数在调用时可以检测参数的类型。

以下面例子说用枚举类型与与接口定义常量的区别:

1 interface Constants { //将常量放置在接口中

2 public static final int Constants_A = 1;3 public static final int Constants_B = 12;4 }5

6 public classConstantsTest {7 enum Constants2 { //将常量放置在枚举类型中

8 Constants_A, Constants_B9 }10

11 //使用接口定义常量

12 public static void doit(int c) { //定义一个方法,这里的参数为int型

13 switch (c) { //根据常量的值做不同操作

14 caseConstants.Constants_A:15 System.out.println("doit() Constants_A");16 break;17 caseConstants.Constants_B:18 System.out.println("doit() Constants_B");19 break;20 }21 }22

23 /**定义一个方法,这里的参数为枚举类型对象*/

24 public static voiddoit2(Constants2 c) {25 switch (c) { //根据枚举类型对象做不同操作

26 caseConstants_A:27 System.out.println("doit2() Constants_A");28 break;29 caseConstants_B:30 System.out.println("doit2() Constants_B");31 break;32 }33 }34

35 public static voidmain(String[] args) {36 ConstantsTest.doit(Constants.Constants_A); //使用接口中定义的常量

37 ConstantsTest.doit(12);38 ConstantsTest.doit(Constants.Constants_B);39 ConstantsTest.doit2(Constants2.Constants_A); //使用枚举类型中的常量

40 ConstantsTest.doit2(Constants2.Constants_B); //使用枚举类型中的常量

41 ConstantsTest.doit(2);42 //ConstantsTest.doit2(2);//报错

43 }44 }

运行结果:

doit() Constants_A

doit() Constants_B

doit() Constants_B

doit2() Constants_A

doit2() Constants_B

分析:在上述代码中,当用户调用doit()方法时,编译器不接受在接口中定义的常量参数,也不会报错,但调用doit2()方法时,任意传递的参数,编译器就会报错,因为这个方法只接受枚举类型的常量作为其参数。

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。