int anim abc_fade_in 0x7f010000
int anim abc_fade_out 0x7f010001
int anim abc_grow_fade_in_from_bottom 0x7f010002
int anim abc_popup_enter 0x7f010003
int anim abc_popup_exit 0x7f010004
int anim abc_shrink_fade_out_from_bottom 0x7f010005
int anim abc_slide_in_bottom 0x7f010006
int anim abc_slide_in_top 0x7f010007
int anim abc_slide_out_bottom 0x7f010008
int anim abc_slide_out_top 0x7f010009
int anim abc_tooltip_enter 0x7f01000a
int anim abc_tooltip_exit 0x7f01000b
int anim btn_checkbox_to_checked_box_inner_merged_animation 0x7f01000c
int anim btn_checkbox_to_checked_box_outer_merged_animation 0x7f01000d
int anim btn_checkbox_to_checked_icon_null_animation 0x7f01000e
int anim btn_checkbox_to_unchecked_box_inner_merged_animation 0x7f01000f
int anim btn_checkbox_to_unchecked_check_path_merged_animation 0x7f010010
int anim btn_checkbox_to_unchecked_icon_null_animation 0x7f010011
int anim btn_radio_to_off_mtrl_dot_group_animation 0x7f010012
int anim btn_radio_to_off_mtrl_ring_outer_animation 0x7f010013
int anim btn_radio_to_off_mtrl_ring_outer_path_animation 0x7f010014
int anim btn_radio_to_on_mtrl_dot_group_animation 0x7f010015
int anim btn_radio_to_on_mtrl_ring_outer_animation 0x7f010016
int anim btn_radio_to_on_mtrl_ring_outer_path_animation 0x7f010017
int anim design_bottom_sheet_slide_in 0x7f010018
int anim design_bottom_sheet_slide_out 0x7f010019
int anim design_snackbar_in 0x7f01001a
int anim design_snackbar_out 0x7f01001b
int anim fragment_fast_out_extra_slow_in 0x7f01001c
int anim mtrl_bottom_sheet_slide_in 0x7f01001d
int anim mtrl_bottom_sheet_slide_out 0x7f01001e
int anim mtrl_card_lowers_interpolator 0x7f01001f
int animator design_appbar_state_list_animator 0x7f020000
int animator design_fab_hide_motion_spec 0x7f020001
int animator design_fab_show_motion_spec 0x7f020002
int animator fragment_close_enter 0x7f020003
int animator fragment_close_exit 0x7f020004
int animator fragment_fade_enter 0x7f020005
int animator fragment_fade_exit 0x7f020006
int animator fragment_open_enter 0x7f020007
int animator fragment_open_exit 0x7f020008
int animator linear_indeterminate_line1_head_interpolator 0x7f020009
int animator linear_indeterminate_line1_tail_interpolator 0x7f02000a
int animator linear_indeterminate_line2_head_interpolator 0x7f02000b
int animator linear_indeterminate_line2_tail_interpolator 0x7f02000c
int animator m3_btn_elevated_btn_state_list_anim 0x7f02000d
int animator m3_btn_state_list_anim 0x7f02000e
int animator m3_card_elevated_state_list_anim 0x7f02000f
int animator m3_card_state_list_anim 0x7f020010
int animator m3_chip_state_list_anim 0x7f020011
int animator m3_elevated_chip_state_list_anim 0x7f020012
int animator mtrl_btn_state_list_anim 0x7f020013
int animator mtrl_btn_unelevated_state_list_anim 0x7f020014
int animator mtrl_card_state_list_anim 0x7f020015
int animator mtrl_chip_state_list_anim 0x7f020016
int animator mtrl_extended_fab_change_size_collapse_motion_spec 0x7f020017
int animator mtrl_extended_fab_change_size_expand_motion_spec 0x7f020018
int animator mtrl_extended_fab_hide_motion_spec 0x7f020019
int animator mtrl_extended_fab_show_motion_spec 0x7f02001a
int animator mtrl_extended_fab_state_list_animator 0x7f02001b
int animator mtrl_fab_hide_motion_spec 0x7f02001c
int animator mtrl_fab_show_motion_spec 0x7f02001d
int animator mtrl_fab_transformation_sheet_collapse_spec 0x7f02001e
int animator mtrl_fab_transformation_sheet_expand_spec 0x7f02001f
int attr SharedValue 0x7f030000
int attr SharedValueId 0x7f030001
int attr actionBarDivider 0x7f030002
int attr actionBarItemBackground 0x7f030003
int attr actionBarPopupTheme 0x7f030004
int attr actionBarSize 0x7f030005
int attr actionBarSplitStyle 0x7f030006
int attr actionBarStyle 0x7f030007
int attr actionBarTabBarStyle 0x7f030008
int attr actionBarTabStyle 0x7f030009
int attr actionBarTabTextStyle 0x7f03000a
int attr actionBarTheme 0x7f03000b
int attr actionBarWidgetTheme 0x7f03000c
int attr actionButtonStyle 0x7f03000d
int attr actionDropDownStyle 0x7f03000e
int attr actionLayout 0x7f03000f
int attr actionMenuTextAppearance 0x7f030010
int attr actionMenuTextColor 0x7f030011
int attr actionModeBackground 0x7f030012
int attr actionModeCloseButtonStyle 0x7f030013
int attr actionModeCloseContentDescription 0x7f030014
int attr actionModeCloseDrawable 0x7f030015
int attr actionModeCopyDrawable 0x7f030016
int attr actionModeCutDrawable 0x7f030017
int attr actionModeFindDrawable 0x7f030018
int attr actionModePasteDrawable 0x7f030019
int attr actionModePopupWindowStyle 0x7f03001a
int attr actionModeSelectAllDrawable 0x7f03001b
int attr actionModeShareDrawable 0x7f03001c
int attr actionModeSplitBackground 0x7f03001d
int attr actionModeStyle 0x7f03001e
int attr actionModeTheme 0x7f03001f
int attr actionModeWebSearchDrawable 0x7f030020
int attr actionOverflowButtonStyle 0x7f030021
int attr actionOverflowMenuStyle 0x7f030022
int attr actionProviderClass 0x7f030023
int attr actionTextColorAlpha 0x7f030024
int attr actionViewClass 0x7f030025
int attr activityChooserViewStyle 0x7f030026
int attr alertDialogButtonGroupStyle 0x7f030027
int attr alertDialogCenterButtons 0x7f030028
int attr alertDialogStyle 0x7f030029
int attr alertDialogTheme 0x7f03002a
int attr allowStacking 0x7f03002b
int attr alpha 0x7f03002c
int attr alphabeticModifiers 0x7f03002d
int attr altSrc 0x7f03002e
int attr animateCircleAngleTo 0x7f03002f
int attr animateRelativeTo 0x7f030030
int attr animationMode 0x7f030031
int attr appBarLayoutStyle 0x7f030032
int attr applyMotionScene 0x7f030033
int attr arcMode 0x7f030034
int attr arrowHeadLength 0x7f030035
int attr arrowShaftLength 0x7f030036
int attr attributeName 0x7f030037
int attr autoCompleteMode 0x7f030038
int attr autoCompleteTextViewStyle 0x7f030039
int attr autoSizeMaxTextSize 0x7f03003a
int attr autoSizeMinTextSize 0x7f03003b
int attr autoSizePresetSizes 0x7f03003c
int attr autoSizeStepGranularity 0x7f03003d
int attr autoSizeTextType 0x7f03003e
int attr autoTransition 0x7f03003f
int attr background 0x7f030040
int attr backgroundColor 0x7f030041
int attr backgroundInsetBottom 0x7f030042
int attr backgroundInsetEnd 0x7f030043
int attr backgroundInsetStart 0x7f030044
int attr backgroundInsetTop 0x7f030045
int attr backgroundOverlayColorAlpha 0x7f030046
int attr backgroundSplit 0x7f030047
int attr backgroundStacked 0x7f030048
int attr backgroundTint 0x7f030049
int attr backgroundTintMode 0x7f03004a
int attr badgeGravity 0x7f03004b
int attr badgeRadius 0x7f03004c
int attr badgeStyle 0x7f03004d
int attr badgeTextColor 0x7f03004e
int attr badgeWidePadding 0x7f03004f
int attr badgeWithTextRadius 0x7f030050
int attr barLength 0x7f030051
int attr barrierAllowsGoneWidgets 0x7f030052
int attr barrierDirection 0x7f030053
int attr barrierMargin 0x7f030054
int attr behavior_autoHide 0x7f030055
int attr behavior_autoShrink 0x7f030056
int attr behavior_draggable 0x7f030057
int attr behavior_expandedOffset 0x7f030058
int attr behavior_fitToContents 0x7f030059
int attr behavior_halfExpandedRatio 0x7f03005a
int attr behavior_hideable 0x7f03005b
int attr behavior_overlapTop 0x7f03005c
int attr behavior_peekHeight 0x7f03005d
int attr behavior_saveFlags 0x7f03005e
int attr behavior_skipCollapsed 0x7f03005f
int attr blendSrc 0x7f030060
int attr borderRound 0x7f030061
int attr borderRoundPercent 0x7f030062
int attr borderWidth 0x7f030063
int attr borderlessButtonStyle 0x7f030064
int attr bottomAppBarStyle 0x7f030065
int attr bottomInsetScrimEnabled 0x7f030066
int attr bottomNavigationStyle 0x7f030067
int attr bottomSheetDialogTheme 0x7f030068
int attr bottomSheetStyle 0x7f030069
int attr boxBackgroundColor 0x7f03006a
int attr boxBackgroundMode 0x7f03006b
int attr boxCollapsedPaddingTop 0x7f03006c
int attr boxCornerRadiusBottomEnd 0x7f03006d
int attr boxCornerRadiusBottomStart 0x7f03006e
int attr boxCornerRadiusTopEnd 0x7f03006f
int attr boxCornerRadiusTopStart 0x7f030070
int attr boxStrokeColor 0x7f030071
int attr boxStrokeErrorColor 0x7f030072
int attr boxStrokeWidth 0x7f030073
int attr box
注解+ 反射实例demo
需积分: 0 29 浏览量
更新于2022-11-29
收藏 13.27MB ZIP 举报
在Java编程语言中,注解(Annotation)和反射(Reflection)是两个强大的特性,它们极大地扩展了代码的可读性和动态性。注解是一种元数据,可以为编译器、工具或运行时系统提供信息,而反射则允许程序在运行时检查类、接口、字段和方法的信息,并能动态地调用方法或访问属性。下面我们将详细探讨这两个概念以及它们的实例应用。
### 注解
#### 1. 注解定义
注解是Java中的一种特殊类型,以`@`符号开头,用于标记代码元素(如类、方法、变量等)。注解不会直接影响程序的执行,但可以在编译时或运行时被解析,以执行特定操作。常见的注解包括`@Override`、`@Deprecated`、`@SuppressWarnings`等。
#### 2. 自定义注解
开发者可以通过创建自定义注解来满足特定需求。自定义注解需要定义一个接口,该接口继承自`java.lang.annotation.Annotation`。例如:
```java
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Loggable {
String value() default "";
}
```
此示例定义了一个名为`Loggable`的注解,表示某个方法需要记录日志。
#### 3. 处理注解
处理注解通常在编译时或运行时进行。Java提供了两种方式来处理注解:编译器处理(通过`@Retention(RetentionPolicy.SOURCE)`保留到源码级别)和运行时处理(通过`@Retention(RetentionPolicy.RUNTIME)`保留到运行时)。处理注解通常通过注解处理器(Annotation Processor)实现。
### 反射
#### 1. 反射基础
反射允许程序在运行时动态地获取类的信息并操作类的对象。通过`Class`类,我们可以获取到类、接口、数组和基本类型的相关信息。例如:
```java
Class<?> clazz = Class.forName("java.util.ArrayList");
```
#### 2. 创建对象
反射可以让我们在不知道具体类名的情况下创建对象,这在多态或插件化开发中非常有用:
```java
Object obj = clazz.newInstance();
```
#### 3. 访问和调用成员
反射还可以用来访问和调用类的字段和方法:
```java
Method method = clazz.getMethod("add", Object.class);
method.invoke(obj, "Item");
```
这个例子展示了如何调用`ArrayList`的`add`方法,并传入一个参数。
#### 4. 获取注解信息
通过反射,我们还可以获取到类、方法或字段上的注解:
```java
Loggable loggable = (Loggable) clazz.getAnnotation(Loggable.class);
if (loggable != null) {
System.out.println("Log level: " + loggable.value());
}
```
这段代码检查`clazz`是否带有`Loggable`注解,并打印出注解的值。
### 结合注解与反射的实例
在`ReflectDemo`中,可能会包含一个使用自定义注解`@Loggable`的方法,通过反射来检测这个注解并在运行时执行相应的日志记录。例如:
```java
public class ReflectDemo {
@Loggable("Info")
public void doSomething() {
// ...
}
}
// 反射处理
for (Method method : ReflectDemo.class.getDeclaredMethods()) {
if (method.isAnnotationPresent(Loggable.class)) {
Loggable loggable = method.getAnnotation(Loggable.class);
System.out.println("Method " + method.getName() + " is loggable with level: " + loggable.value());
// 实现日志记录逻辑
}
}
```
这个例子展示了如何结合注解和反射,实现对具有特定注解的方法进行日志记录的功能。
注解和反射是Java中非常重要的工具,它们提供了代码的元数据信息和运行时的灵活性,能够帮助我们编写更加灵活、可维护的代码。通过理解和掌握这两个特性,开发者可以更好地应对复杂的需求和场景。