this和区别
前景
当我学到this和target的时候,网上看了很多解释和资料,总是一头雾水,都不是很清晰,说的也不是很明白,唯一看到了点有理解的就是这篇帖子辅助帖子,但是感觉描述的也不是很准确,特此标注我自己的理解
说明
间接选择Join Point | 说明 |
---|---|
this(Type or Id) | Join Point 所属的 this 对象是否 instanceOf Type 或者 Id 的类型 |
target(Type or Id) | Join Point 所在的对象(例如 call 或 execution 操作符应用的对象)是否 instanceOf Type 或者 Id 的类型 |
this理解
Join Point 所属的 this 对象我的理解是代码插入点的this对象是否是Type类型的子类或者其本身(注意:补充下我发现内部类也是可以的哦)
target 理解
==Join Point 所在的对象(例如 call 或 execution 操作符应用的对象)==我的理解是,调用此方法的对象是否是Type类型的子类或者其本身
测试用例
源代码
定义接口
public interface PeopInterface {
void startRun();
}
定义实现类
public class MalePeop implements PeopInterface {
@Override
public void startRun() {
Log.d("wyz", "男人run");
}
}
定义调用代码
public class TargetThisActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_target_this);