super()和this()

Super()和this()的作用

super的作用:

super的作用是在子类函数中引用父类中的方法;
static class father{
        String name = " i am father";
        father(){
            System.out.println("how are you");
        }
        father(String name){
            System.out.println(name);
        }
        public void getName() {
            System.out.println("who are you");
        }
    }
    static class son extends father{
        son(){
            super();
            super.getName();
        }
        son(String name){
            super(name);
        }
    }

    public static void main(String[] args) {
        son s = new son();
        s = new son("i am zhangsan");
    }

结果:how are you /who are you/i am zhangsan

从结果可以看出,super作用于子类函数中,来调用父类函数的方法;当无参时即super()时,调用父类无参构造函数即father();有参时super(str)调用有相同参数个数的父类函数father(str);调用具体方法即super.method();

this()的作用:

this的作用是在函数中调用本类中其他的函数或参数;

public class This {
    String name = "zhangsan";
    String passwd = "123456";
    This(String name,String passwd){
        this.name = name;
        this.passwd = passwd;
        System.out.println(this.name+"   "+this.passwd);
    }
    This(){
        this.prt();
    }
    public void prt(){

        System.out.println("我是被调用的函数");
        System.out.println(this.name+"    "+this.passwd);

    }


    public static void main(String[] args) {
        This t = new This("lisi","987654");
        This t1 = new This();
    }
}
结果:
lisi   987654
我是被调用的函数
zhangsan    123456

从结果可以看出:this可以调用本类中的参数和函数;

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
super和this是Java中的关键字,用于在类中引用父类和当前对象。 super关键字用于引用父类对象。它可以通过super.属性来引用父类中的成员变量,通过super.方法()来调用父类中定义的方法,还可以使用super()来调用父类的构造方法。super()通常用于子类构造方法的第一行,可以指定参数来调用父类的特定构造方法。super关键字只能在非静态修饰的代码中使用。 this关键字用于引用当前对象。它可以通过this.属性来引用当前对象中的成员变量,通过this.()来调用本类的某个方法,还可以使用this()来调用本类的构造方法。this()通常用于构造方法的第一行,用于调用本类的其他构造方法。this关键字只能在非静态修饰的代码中使用。 super和this的使用注意事项包括: 1. 在构造方法中,this和super只能出现一个,且只能出现在方法体的第一行。 2. this和super不能在静态方法和静态代码块中使用。 3. this可以指代当前对象,而super不能。 4. 在子类覆盖父类方法时,可以使用super调用父类的同名方法。 综上所述,super和this是Java中用于引用父类和当前对象的关键字,它们在类的成员变量、方法和构造方法中有不同的用法和限制。 #### 引用[.reference_title] - *1* *3* [this和super关键字区别](https://blog.csdn.net/qq_44897733/article/details/121580435)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [super()和this()](https://blog.csdn.net/poppy_poppy/article/details/120560732)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值