• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

XiaoXiaoli

  • 博客园
  • 联系
  • 订阅
  • 管理

公告

View Post

java_this指向

同一个方法在子类和父类都有。

 

1 谁调用指向谁。

 1 public Class Father{
 2   //service方法出现this指向调用service方法实例对象。
 3   public void service(String method){
 4     if("get".equals(method)){
 5       this.doGet(); //son.service() this指向子类对象。
 6      }else if("post".equals(method)){
 7        this.doPost();
 8      }else{
 9        System.out.println("error");
10      }
11   }
12   public void doGet(){}
13   public void doPost(){}
14 }
15         
16 public class Son extends Father{
17   public void doGet(){
18     System.out.println("Son doGet");
19   }
20   public void doPost(){
21     System.out.println("Son doGet");
22   }
23 }
24 
25 public class Girl extends Father{
26   public void doGet(){
27     System.out.println("Girl doGet");
28   }
29   public void doPost(){
30     System.out.println("Girl doPost");
31   }
32 }

 

 

posted on 2021-08-08 15:28  XiaoXiaoli  阅读(153)  评论(0)    收藏  举报

刷新页面返回顶部
 
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3