我又来送分了!!这次关于enum的

ahytufc 2009-02-10 10:50:01
// enum-ex2.jsl
// Using FlagsAttribute with enum.

import System.*;

/** @attribute Flags() */
public enum CarOptions
{
SunRoof(0x01),
Spoiler(0x02),
FogLights(0x04),
TintedWindows(0x08)
}

public class flagtest
{
public static void main(String args[])
{
CarOptions options = CarOptions.SunRoof | CarOptions.FogLights;
System.out.println((int)options);
}
}


输出
5
如果移除 FlagsAttribute 属性输出:

5
5

为什么输出5?为什么如果移除 FlagsAttribute 属性输出5 5???
...全文
151 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
ahytufc 2009-02-12
  • 打赏
  • 举报
回复
newstudent_never 啊!不好意思!粗心了,//@attribute flag() 输出 大意了…… 我把5 5当成System.out.println((int)options);自己输出的了……


在位上 options=0x01|0x04==0x05===(int)5
这里就是1+4??
deerwin1986 2009-02-10
  • 打赏
  • 举报
回复
移除 FlagsAttribute
什么意思。。。
waizqfor 2009-02-10
  • 打赏
  • 举报
回复
[Quote=引用楼主 ahytufc 的帖子:]
// enum-ex2.jsl
// Using FlagsAttribute with enum.

import System.*;

/** @attribute Flags() */
public enum CarOptions
{
SunRoof(0x01),
Spoiler(0x02),
FogLights(0x04),
TintedWindows(0x08)
}

public class flagtest
{
public static void main(String args[])
{
CarOptions options = CarOptions.SunRoof | CarOptions.FogLights;
Sys…
[/Quote]
的确是JAVA的 上学的时候就很排斥
帮顶!~
hhyttppd 2009-02-10
  • 打赏
  • 举报
回复
public class flagtest
{
public static void main(String args[])
{
CarOptions options = CarOptions.SunRoof | CarOptions.FogLights;
System.out.println(options); //FlagAttribute可以解析 5 = CarOptions.SunRoof | CarOptions.FogLights
System.out.println((int)options);
}
}

而非位域只是当一个整数保存。


另外位域的输出结果是:
SunRoof, FogLights
5

不要搞错了
ahytufc 2009-02-10
  • 打赏
  • 举报
回复
MSDN上的,http://msdn.microsoft.com/zh-cn/library/ms241062(VS.80).aspx
最后边的例子,
没明白怎么得到的结果
HelloDan 2009-02-10
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 hhyttppd 的回复:]
你这是java吧?

这个不可能吧。。。
[/Quote]

真的,我看不懂,应该不是C++
hhyttppd 2009-02-10
  • 打赏
  • 举报
回复
你这是java吧?

这个不可能吧。。。
sagegz 2009-02-10
  • 打赏
  • 举报
回复
JAVA下学期才学
xmu_才盛 2009-02-10
  • 打赏
  • 举报
回复
你代码删除东西了吧? 不要乱编代码好不好啊,去掉最关键的部分,莫名其妙的!

源代码应该是这样子的吧?
System.out.println(options);
System.out.println((int)options);


然后
@attribute flag()
输出
SunRoof, FogLights
5

//@attribute flag()
输出
5
5

attribute flag 允许位域处理

在 一般情况下
CarOptions options = CarOptions.SunRoof | CarOptions.FogLights;
使得在位上 options=0x01|0x04==0x05===(int)5
而字符值上没有 0x05的字符值,故直接等于5

所以是两个5

在 attribute flag 情况下

CarOptions options = CarOptions.SunRoof | CarOptions.FogLights;
同样在位上options==5
但是在字符值上options==SunRoof和FogLights


也就是说 attribute flag 允许枚举的值进行 位域操作




xmu_才盛 2009-02-10
  • 打赏
  • 举报
回复

看看这个对你有没有帮助 ,flag Attribute定义符号位,貌似可以使枚举值进行&,|,!操作
// Example of the FlagsAttribute attribute.
using namespace System;

// Define an Enum without FlagsAttribute.
enum class SingleHue : short
{
Black = 0,
Red = 1,
Green = 2,
Blue = 4
};


// Define an Enum with FlagsAttribute.

[FlagsAttribute]
enum class MultiHue : short
{
Black = 0,
Red = 1,
Green = 2,
Blue = 4
};

int main()
{
Console::WriteLine( "This example of the FlagsAttribute attribute \n"
"generates the following output." );
Console::WriteLine( "\nAll possible combinations of values of an \n"
"Enum without FlagsAttribute:\n" );

// Display all possible combinations of values.
for ( int val = 0; val <= 8; val++ )
Console::WriteLine( "{0,3} - {1}", val, ((SingleHue)val).ToString() );
Console::WriteLine( "\nAll possible combinations of values of an \n"
"Enum with FlagsAttribute:\n" );

// Display all possible combinations of values.
// Also display an invalid value.
for ( int val = 0; val <= 8; val++ )
Console::WriteLine( "{0,3} - {1}", val, ((MultiHue)val).ToString() );
}

/*
This example of the FlagsAttribute attribute
generates the following output.

All possible combinations of values of an
Enum without FlagsAttribute:

0 - Black
1 - Red
2 - Green
3 - 3
4 - Blue
5 - 5
6 - 6
7 - 7
8 - 8

All possible combinations of values of an
Enum with FlagsAttribute:

0 - Black
1 - Red
2 - Green
3 - Red, Green
4 - Blue
5 - Red, Blue
6 - Green, Blue
7 - Red, Green, Blue
8 - 8
*/

lzh9955 2009-02-10
  • 打赏
  • 举报
回复
up
herman~~ 2009-02-10
  • 打赏
  • 举报
回复
看不懂 mark

65,211

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧