简体   繁体   English

Java意义上的“注释”和iPhone SDK意义上的“注释”之间有什么区别?

[英]What's the difference between “annotation” in the Java sense and in the iPhone SDK sense?

what is annotation class. 什么是注释类。 what is the use of it in java/android. 它在java / android中有什么用处。

In iphone Annotation is used to drop a pin on the map.. 在iphone中,Annotation用于在地图上放置一个图钉..

java has java.lang.Annotation package... what is the use of it? java有java.lang.Annotation包...有什么用呢? can i have a examples, tutorials,sample codes, etc? 我可以有示例,教程,示例代码等吗?

The two concepts are unrelated: 这两个概念是无关的:

  • In the iPhone SDK, an annotation is a sort of note that you can put on a map. 在iPhone SDK中,注释是一种可以放在地图上的注释。
  • In Java (and the Android SDK), annotations are a feature of the Java language used to add specific kinds of metadata to classes. 在Java(和Android SDK)中, 注释是Java语言的一个特性,用于向类添加特定类型的元数据。

In the Java world, annotations are compile-time tags which are attached to a piece of code. 在Java世界中,注释是附加到一段代码的编译时标记。 They add metadata to a snippet of code which tools or third parties (or even your own code) can then use later. 他们将元数据添加到代码片段中,然后工具或第三方(甚至您自己的代码)可以使用。 But they usually don't affect the way that code runs; 但它们通常不会影响代码的运行方式; that is, if you deleted the annotation, that snippet of code typically shouldn't behave any differently. 也就是说,如果删除了注释,那段代码通常不应该有任何不同的行为。

A simple example is marking a piece of code with a copyright notice. 一个简单的例子是用版权声明标记一段代码。 Here's an annotation that we might use for this: 这是我们可能用于此的注释:

public @interface Copyright {
    String value();
}

And here's how we'd attach that metadata to code: 以下是我们将元数据附加到代码的方式:

@Copyright(value = "2010, United States National Security Administration")
public class QuantumCryptographyDecoder { ... }

If there's only one annotation property named value you can omit the value = specifier as a shortcut: 如果只有一个名为value注释属性,则可以省略value =说明符作为快捷方式:

@Copyright("2010, United States National Security Administration")
public class QuantumCryptographyDecoder { ... }

Otherwise you have to specify it: 否则你必须指定它:

@Created(year = 2010)
public class ShinyNewClass { ... }

If we removed the @Copyright annotation, the QuantumCryptographyDecoder would still work the same as it did before. 如果我们删除了@Copyright批注,那么QuantumCryptographyDecoder仍然可以像以前一样工作。 There just wouldn't be any Copyright metadata attached to it. 不会附加任何Copyright元数据。 But a third-party tool which did some kind of validation (for instance, requiring that all classes whose names included "Quantum" have a @Copyright notice attached) could alert you to the missing @Copyright or take some other useful action. 但是第三方工具进行了某种验证(例如,要求所有名为“Quantum”的类都附有@Copyright通知)可以提醒您缺少@Copyright或采取其他一些有用的操作。

Annotaions in Java are really quite powerfull concept. Java中的注释实际上是非常强大的概念。 Since annotaion can be introspected with the reflectioon API it comes very handy in doing different jobs. 由于可以使用reflectioon API对注释进行反思,因此在执行不同的工作时非常方便。 I think the good and simple example is a JAXB API. 我认为好的和简单的例子是JAXB API。 It allows you to write/read XML files just working with POJOs. 它允许您编写/读取仅与POJO一起使用的XML文件。 Not need to manually marshall/unmarshal XML tree. 不需要手动编组/解组XML树。 All you need just to "mark" interested variables that you want to be included in xml with annotations. 您只需要“标记”您希望包含在带注释的xml中的感兴趣变量。 So it said that this variables holding metadata allowing JAXB API to find out what variables you're interested in and write/read their values to/from XML file. 所以它说这个变量包含元数据,允许JAXB API找出你感兴趣的变量,并在XML文件中写入/读取它们的值。

Don't be disillusioned that annotations just play some "decoration" role like holding author or version info or so. 不要幻想破解注释只是扮演一些“装饰”角色,如持有作者或版本信息等。 They are much more powerfull... 他们更强大......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM