栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java-this关键字和super关键字

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java-this关键字和super关键字

目录

1.this关键字

1-1使用this引用隐藏数据域

1-2使用this调用构造方法


1.this关键字

关键字this引用对象自身,它也可以在构造方法内部用于调用同一类的其他构造方法。

1.使用this关键字引用成员变量

2:使用this关键字在自身构造方法内部引用其它构造方法

3:使用this关键字代表自身类的对象

4:使用this关键字引用成员方法

5.不能在static中调用

6.当使用this调用构造方法的时候,this语句只能写在第一行 。

1-1使用this引用隐藏数据域

例如:

public class machine {
   private String name;
   private int money;

    public void method(  String name,int money){
        this.name=name;               
        this.money=money; 
}
 public static void main(String[] args) {
   machine tv=new machine();
        tv.method("tv",3000);
    }

}

1-2使用this调用构造方法

this可以用于调用 同一类的另一个构造方法。

public class machine {
   private String name;
   private int money;

   public  machine(){
       this("tv",300);
   }
    public  machine(  String name,int money){
        this.name=name;                
        this.money=money;             

    }

    public static void main(String[] args) {
         machine tv=new machine();
        System.out.println(tv.name+" "+  tv.money);
    }
2.super关键字

super指代父类,可以用于调用父类中的普通方法和构造方法。

用途:(1)调用父类的构造方法

           (2)调用父类的方法

2-1 调用父类的构造方法

super();或者super(parameters);

super调用父类的无参构造方法,super(parameters)调用与参数匹配的父类构造方法。super必须在子类的构造方法的第一行。

2-2调用父类的方法

super.方法名(参数);

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/490501.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号