温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Java中this方法怎么使用

发布时间:2022-08-15 16:10:17 来源:亿速云 阅读:131 作者:iii 栏目:开发技术

今天小编给大家分享一下Java中this方法怎么使用的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。

    一、this关键字

    1.this的类型:哪个对象调用就是哪个对象的引用类型

    Java中this方法怎么使用

    二、用法总结

    1.this.data; //访问属性

    2.this.func(); //访问方法

    3.this(); //调用本类中其他构造方法

    三、解释用法

    1.this.data

    这种是在成员方法中使用

    让我们来看看不加this会出现什么样的状况

    class MyDate{
        public int year;
        public int month;
        public int day;
     
        public void setDate(int year, int month,int day){
            year = year;//这里没有加this
            month = month;//这里没有加this
            day = day;//这里没有加this
        }
        public void PrintDate(){
            System.out.println(year+"年 "+month+"月 "+day+"日 ");
        }
    }
    public class TestDemo {
        public static void main(String[] args) {
            MyDate myDate = new MyDate();
            myDate.setDate(2000,9,25);
            myDate.PrintDate();
            MyDate myDate1 = new MyDate();
            myDate1.setDate(2002,7,14);
            myDate1.PrintDate();
        }
    }

    我们想要达到的预期是分别输出2000年9月25日,2002年7月14日。

    而实际输出的结果是

    Java中this方法怎么使用

    而当我们加上this时

    class MyDate{
        public int year;
        public int month;
        public int day;
     
        public void setDate(int year, int month,int day){
           this.year = year;
           this.month = month;
           this.day = day;
        }
        public void PrintDate(){
            System.out.println(this.year+"年 "+this.month+"月 "+this.day+"日 ");
        }
    }
    public class TestDemo {
        public static void main(String[] args) {
            MyDate myDate = new MyDate();
            myDate.setDate(2000,9,25);
            myDate.PrintDate();
            MyDate myDate1 = new MyDate();
            myDate1.setDate(2002,7,14);
            myDate1.PrintDate();
        }
    }

    Java中this方法怎么使用

     就实现了赋值的功能,为了避免出现差错,我们建议尽量带上this

    2.this.func()

    这种是指在普通成员方法中使用this调用另一个成员方法

    class Student{
        public String name;
        public void doClass(){
            System.out.println(name+"上课");
            this.doHomeWork();
        }
        public void doHomeWork(){
            System.out.println(name+"正在写作业");
        }
    }
    public class TestDemo2 {
        public static void main(String[] args) {
            Student student = new Student();
            student.name = "小明";
            student.doClass();
        }
    }

    运行结果:

    Java中this方法怎么使用

     (3)this()

    这种指在构造方法中使用this调用本类其他的构造方法

    这种this的使用注意以下几点

    1.this只能在构造方法中调用其他构造方法

    2.this要放在第一行

    3.一个构造方法中只能调用一个构造方法

    Java中this方法怎么使用

    Java中this方法怎么使用

    运行结果

    Java中this方法怎么使用

    以上就是“Java中this方法怎么使用”这篇文章的所有内容,感谢各位的阅读!相信大家阅读完这篇文章都有很大的收获,小编每天都会为大家更新不同的知识,如果还想学习更多的知识,请关注亿速云行业资讯频道。

    向AI问一下细节

    免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

    AI