当前位置: 首页 > news >正文

网站建设置顶/百度收录方法

网站建设置顶,百度收录方法,网站运营团队各岗位的职责是什么,怎么做类似美团的网站Enum是在Java中用来定义枚举类型的关键字。Enum一般用来表示一组相同类型的常量,如性别、日期 、颜色等. 下面是一个最简单的枚举。 1 2 3 4 5 6 7 8 9 10 public enum Color {RED,GREEN,BLUE}实际上在Java中枚举类型本质上就是一个类,其继承自java.lang.Enum类。 通…

Enum是在Java中用来定义枚举类型的关键字。Enum一般用来表示一组相同类型的常量,如性别、日期 、颜色等.

下面是一个最简单的枚举。

1
2
3
4
5
6
7
8
9
10
public enum Color
{

RED,

GREEN,

BLUE

}

实际上在Java中枚举类型本质上就是一个类,其继承自java.lang.Enum类。

通过默认提供的GetValues()方法可以获取所有枚举对象,其以一个数组的形式返回。

1
2
3
4
5
6
for(Color color : Color.values())
{

System.out.printf("%s: %s%n",color,color.toString());

}

输出结果为:

1
2
3
4
5
RED: RED

GREEN: GREEN

BLUE: BLUE

既然是enum本质上是类,那么我们当然可以给其加一些方法。注意最后一个枚举对象要使用“;”结尾,说明枚举值结束使用。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public enum Color
{

RED("Red Color"),

GREEN("Green Color"),

BLUE("Blue Color");

private final String color;

private Color(String color) {

    this.color = color;

}

public String getColor() {

return color;

}

}

注意在enum中是不能声明public的构造函数的,这样是为了避免直接实例化enum对象。

我们可以通过getValues()方法调用getColor()方法。

1
2
3
4
5
6
for(Color color : Color.values())
{

System.out.printf("%s: %s%n",color,color.getColor());

}

以下是输出结果:

1
2
3
4
5
RED: Red Color

GREEN: Green Color

BLUE: Blue Color

如果大家还是看不太明白的话,我可以展示一个类来对个对比。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public final class Color extends java.lang.Enum{

    public static final Color RED;

    public static final Color GREEN;

    public static final Color  BLUE;

    static {};

    public java.lang.String getColor();

    public static Color[] values();

    public static Color valueOf(java.lang.String);

}

这个类的作用等同于我们的Color枚举对象。而每个被枚举的成员其实就是定义的枚举类型的一个实例,它们都被默认为final。无法改变常数名称所设定的值,它们也是public和static的成员,这与接口中的常量限制相同。可以通过类名称直接使用它们。

所以我们大胆的在里面增加一些其它的方法来实现我们的新特性。

在这里我增加了一个新的方法isRed()来判断当前枚举实例是否是红色的。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
public enum Color
{

RED("Red Color"),

GREEN("Green Color"),

BLUE("Blue Color");


private final String color;



private Color(String color) {

    this.color = color;

}


public String getColor() {

return color;

}


public boolean isRed() {

return this.equals(RED);

}

}

可以对其进行一个测试。

1
2
3
4
5
6
7
8
Color green = Color.GREEN;

System.out.print(green.isRed());


Color red = Color.RED;

System.out.print(red.isRed());

第一个输出结果为false,第二个输出结果为true。

通过对Java中enum的运用,往往会产生奇效。比如有这样一个例子,有一个Rover对象,它有一个类型为Enum的direction属性,我们要给Rover实现左转的指令。你可能会写出这样的代码:

Direction.java
1
2
3
4
5
6
public enum Direction {
    North,
    East,
    South,
    West
}
Rover.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public class Rover {

    private Direction direction;

    public Rover(Direction direction) {
        this.direction = direction;
    }

    public void turnLeft() {
        if (direction.equals(Direction.East)) {
            direction = Direction.North;
        } else if (direction.equals(Direction.North)) {
            direction = Direction.West;
        } else if (direction.equals(Direction.West)) {
            direction = Direction.South;
        } else if (direction.equals(Direction.South)) {
            direction = Direction.East;
        }

    }
}

一大堆if...else的代码看起来真的很丑陋。这还好,如果让你给Rover再加几个方法,比如向右转,旋转到反方向等,那代码就没法看了。 我们可以这样分析一下,其实给定一个方向之后,向左转的方向也就确定了,所以我们可以将这些逻辑放置到Direction对象中去。下面是改进后的版本。

Direction.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
public enum Direction {
    North {
        @Override
        public Direction turnLeft() {
        return West;
        }
    },
    East {
        @Override
        public Direction turnLeft() {
            return North;
        }

    },
    South {
        @Override
        public Direction turnLeft() {
            return East;
        }
    },
    West {
        @Override
        public Direction turnLeft() {
            return South;
        }
    };

    public abstract Direction turnLeft();
}
Rover.java
1
2
3
4
5
6
7
8
9
10
11
12
public class Rover {

    private Direction direction;

    public Rover(Direction direction) {
        this.direction = direction;
    }

    public void turnLeft() {
        direction = direction.turnLeft();
    }
}

这样的代码看起来要舒服的多.

C#中的Enum

我也可以简单的讲解下c#中的枚举对象。在c#中声明枚举对象的关键字是enum。

1
2
3
4
 访问修辞符 enum 枚举名:基础类型
    {
        枚举成员
    }

基础类型必须能够表示该枚举中定义的所有枚举数值。枚举声明可以显式地声明 byte、sbyte、short、ushort、int、uint、long 或 ulong 类型作为对应的基础类型。没有显式地声明基础类型的枚举声明意味着所对应的基础类型是 int.

1
2
3
4
5
6
7
8
9
10
11
public enum Color

{

RED,

GREEN,

BLUE

}

所以我们可以直接将一个枚举类型强制转换成其对应的基础类型。

1
Int  num = (int)Color.Red;

Num的值为0.

我们也可以反向转换。

1
Color color = (Color)num;

甚至我们还可以对枚举类型进行与或运算。这些就不细说了,有兴趣的可以查阅相关资料。

http://www.lbrq.cn/news/1395919.html

相关文章:

  • 如何做微网站平台/网络服务提供者收集和使用个人信息应当符合的条件有
  • 怎么把本地wordpress上传到服务器/优化教程网官网
  • asp.net 网站压缩/友链交易交易平台
  • 网站建设实训室/推广app赚佣金平台有哪些
  • 太原建站模板搭建/陕西网站推广公司
  • 石家庄定制网站建设/seo就业前景
  • 溧阳网站制作/广州seo公司哪个比较好
  • 娱乐网站制作/海淀区seo搜索引擎优化企业
  • 手机动态网站制作/北京百度快照推广公司
  • 无锡网站建设哪家做的比较好/专业外贸网络推广
  • 赛多利斯科学仪器北京有限公司/网站优化推广怎么做
  • 柳州网站建设找哪家好/口碑seo推广公司
  • wordpress能导入多少产品/什么是搜索引擎优化的核心
  • 学校网站div css模板/广点通
  • 网站怎么做app/哪些平台可以免费打广告
  • 黑客网站免费盗号/宁波seo整站优化
  • 学做莱网站/今天中国新闻
  • jsp网站建设项目实战源代码/百度百科创建
  • 微网站开发视频/最新国际新闻事件今天
  • 怎么样做贷款网站/下载百度语音导航地图安装
  • 鄞州区住房和城乡建设委员网站/网络营销师是干什么的
  • 威海外贸网站建设电话/如何检测网站是否安全
  • 网站关键词数量多少好/优秀网站设计
  • mvc做网站前台代码/成都营销推广公司
  • 离石做网站的公司/2345网址导航 中国最
  • wordpress调用图片路径/北京seo优化费用
  • 腾讯 网站建设/公司的网站制作
  • 保定电子商务网站建设/网络营销做得好的品牌
  • 怎么把网站做的靠前/天津放心站内优化seo
  • php做自己的网站/网站百度不收录的原因
  • 【Java集合】List,Map,Set-详细讲解
  • 医疗信息化实战:引领医疗行业数字化转型实践
  • Model Context Protocol (MCP) - 尝试创建和使用一下MCP Client
  • KDD 2025 | CMA:一次训练,预测任意过去与未来!元学习+扩散模型颠覆时序预测!
  • JAVA:MyBatis 核心组件详解的技术指南
  • 卸载win10/win11系统里导致磁盘故障的补丁