我又来送分了!!这次关于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???
...全文
152 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
内容概要:本文围绕“新型电力系统下多分布式电源接入配电网承载力评估方法”的研究,系统性地介绍了基于Matlab的仿真建模与代码实现方案,旨在评估高比例分布式电源(如光伏、风电等)接入背景下配电网的接纳能力。研究融合了智能优化算法(如蜣螂优化、灰狼优化、遗传算法)、多目标优化、鲁棒优化及双层优化模型,结合潮流计算、稳定性分析与故障仿真,构建了完整的承载力评估体系。文档不仅提供核心算法实现,还拓展至微电网调度、储能配置、电氢耦合系统、电动汽车协同等前沿方向,强调“复现+创新”相结合的科研路径,助力研究者快速掌握高水平论文复现技巧并激发原创思路。; 适合人群:具备电力系统、自动化或相关专业背景,熟悉Matlab/Simulink仿真环境,正在从事科研或工程应用的研究生及初级科研人员(工作1-3年);; 使用场景及目标:①复现高水平期刊中关于配电网承载力的优化模型;②开展高比例可再生能源接入下的配电网规划与运行研究;③学习并应用智能优化算法解决复杂电力系统问题;④获取完整科研资源包以加速课题进展与论文撰写; 阅读建议:建议读者关注公众号“荔枝科研社”获取网盘资源,下载全套代码与模型文件,按照文档结构循序渐进学习,重点理解算法设计逻辑与仿真建模细节,结合所提供的复现案例深化对优化模型与工程应用场景的理解,提升科研效率与创新能力。

65,211

社区成员

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

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