zoukankan      html  css  js  c++  java
  • Java this关键字的使用场景

    this的概念:

      this保存当前对象的内存地址,指向自身;

      this保存在堆内存的对象中;

      this用来访问实例变量 ,表示当前对象,所以this不使用于静态方法中,可以省略;

    public class ThisTest02{
        public static void main(String[] args){
            Student s1 = new Student();
            s1.setName("wang");
            s1.setNum(2222);
            System.out.println(s1.getNum());
            System.out.println(s1.getName());
            
            Student s2 = new Student(111,"li");
            System.out.println(s2.getNum());
            System.out.println(s2.getName());   
        }
    }
    
    class Student{
        private int num;
        private String name;
        
        public Student(){  //无参构造方法
            
        }
        
        /*
        public Student(int nu, String na){       //有参数的构造方法,可行
            num = nu;
            name = na;
        }
        */
        
        public Student(int num, String name){     //上面的构造方法可以写成这种形式,增强可读性
            this.num = num;        //构造方法中this不能省略
            this.name = name;
        }
        //setter and getter方法
        /*
        public void setNum(int nu){      //添加设置num和name的设置方法
            num = nu;
        }
        public void setName(String na){
            name = na;
        }
        */
        
        /*
        public void setName(String name){
            name = name;   //如果此处不加this,此处两个name都为局部变量,和实例变量没有关系;
        }
        public void setNum(int num){
            num = num;
        }
        */
        
        public void setNum(int num){
            this.num = num;      //实例方法中this不能省略
        }
        public void setName(String name){
            this.name = name;
        }
        
        public int getNum(){
            //return this.num;
            return num;    //访问实例变量,通过this访问,this可以省略
        }
        
        public String getName(){
            return this.name;
        }
        
    }

    结论:

      this不能省略的场景:
      this用在实例方法和构造方法中,用来区分实例变量和局部变量时,不能省略

    this的另一种使用场景:

      在构造方法1中通过this(实际参数列表)的语法结构调用构造方法2;

      示例

    public class ThisTest03{
        public static void main(String[] args){
            Date d1 = new Date();       //如果调用无参构造函数,可以给一个默认值
            d1.detail();
            
            Date d2 = new Date(2020,12,28);
            d2.detail();
            
            Date d3 = new Date();
            d3.setYear(2020);
            d3.setMonth(12);
            d3.setDay(22);
            System.out.println(d3.getYear());
            System.out.println(d3.getMonth());
            System.out.println(d3.getDay());
        }
    }
    
    class Date{
        private int year;
        private int month;
        private int day;
        
        public Date(){
        //    System.out.println(11);    //错误: 对this的调用必须是构造器中的第一个语句,如果通过this实现,则this必须第一
        //    this.year = 1970;
        //    this.month = 1;
        //    this.day = 1;             //如果调用无参构造函数,可以给一个默认值,可以这样实现;
        
            this(1970,1,1);  //在构造方法1中使用this调用构造方法2,做到代码复用;
        }
        
        public Date(int year, int month, int day){
            this.year = year;
            this.month = month;
            this.day = day;
        }
        
        public void detail(){
            System.out.println(this.year + "年" + this.month + "月" + this.day + "日");
        }
        //setter and getter
        public void setYear(int year){
            this.year = year;
        }
        public int getYear(){
            return year;
        }
        
        public void setMonth(int month){
            this.month = month;
        }
        public int getMonth(){
            return month;
        }
        
        public void setDay(int day){
            this.day = day;
        }
        public int getDay(){
            return day;
        }
    }

      结论:

      可以在构造方法1中通过this调用构造方法2,做到代码复用,但是这种方式,this语句必须是构造方法中的第一条语句;

  • 相关阅读:
    Aurora 数据库支持多达五个跨区域只读副本
    Amazon RDS 的 Oracle 只读副本
    Amazon EC2 密钥对
    DynamoDB 读取请求单位和写入请求单位
    使用 EBS 优化的实例或 10 Gb 网络实例
    启动 LAMP 堆栈 Web 应用程序
    AWS 中的错误重试和指数退避 Error Retries and Exponential Backoff in AWS
    使用 Amazon S3 阻止公有访问
    路由表 Router Table
    使用MySQLAdmin工具查看QPS
  • 原文地址:https://www.cnblogs.com/homle/p/14199414.html
Copyright © 2011-2022 走看看