• 面向对象(this关键字)


     1 package com_package2;
     2 
     3 public class Person3 {
     4     private int age;
     5     private String name;
     6     public int getAge() {
     7         return age;
     8     }
     9     public void setAge(int age) {
    10         this.age = age;
    11     }
    12     public String getName() {
    13         return name;
    14     }
    15     public void setName(String name) {
    16         this.name = name;
    17     }
    18     Person3()
    19     {
    20         System.out.println("hahahaa");
    21         
    22     }
    23     Person3(String n,int a)
    24     {
    25         name = n;
    26         age = a;
    27         
    28     }
    29     Person3(String n)
    30     {
    31         name = n;
    32         
    33     }
    34     Person3(int a)
    35     {
    36         
    37         age = a;
    38     }
    39     public void speak()
    40     {
    41         System.out.println("name = "+name+" "+"age="+age);
    42         
    43     }
    44 }
     1 package com_package2;
     2 
     3 public class Person3Demo {
     4     public static void main(String[]args)
     5     {
     6         Person3 p = new Person3();
     7         p.speak();
     8         Person3 p1 = new Person3("hejinjin",23);
     9         p1.speak();
    10         Person3 p2 = new Person3("hejinjin");
    11         p2.speak();
    12         Person3 p3 = new Person3(23);
    13         p3.speak();
    14         show();
    15             
    16     }
    17     public static void show()
    18     {
    19         System.out.println("haha");
    20         
    21     }
    22 
    23 }

    hahahaa
    name = null age=0
    name = hejinjin age=23
    name = hejinjin age=0
    name = null age=23
    haha

    改成this关键字后:

    package com_package2;
    
    public class Person3 {
        private int age;
        private String name;
        public int getAge() {
            return age;
        }
        public void setAge(int age) {
            this.age = age;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        Person3()
        {
            System.out.println("hahahaa");
            
        }
        Person3(String name,int age)
        {
            this.name = name;
            this.age = age;
            
        }
        Person3(String name)
        {
            this.name = name;
            
        }
        Person3(int age)
        {
            
            this.age = age;
        }
        public void speak()
        {
            System.out.println("name = "+this.name+" "+"age="+this.age);
            
        }
    }

    注意this关键字的格式:this.类中的属性 = 参数中含有的变量

    当成员变量和局部变量同名的时候,为了区分,可以使用一个关键字来完成。关键字:this

    this:代表的是一个对象,哪个对象调用this所在的函数,this就代表哪个对象,this就是当前对象的一个引用 

    this什么用:当定义功能的时候,功能内部使用到了调用该功能的对象,这是就用this来表示

    this的表现形式一:当局部变量和成员变量同名的时候,可以用this来区分

    this的表现形式二:构造函数之间的调用可以使用this关键字,后面跟上小括号,指定其具体的实参即可明确要调用的构造函数

    用this关键字进行一般函数调用一般函数

    package com_package2;
    
    public class Person3 {
        private int age;
        private String name;
        public int getAge() {
            return age;
        }
        public void setAge(int age) {
            this.age = age;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        Person3()
        {
            System.out.println("hahahaa");
            
        }
        Person3(String name,int age)
        {   this(name);
            this.name = name;
            this.age = age;
            
        }
        Person3(String n)
        {
            this.name = name;
            
        }
        Person3(int age)
        {
            
            this.age = age;
        }
        public void speak()
        {
            System.out.println("name = "+this.name+" "+"age="+this.age);
            
            
        }
        public void show()
        {
            this.speak();
            
        }
    }
    package com_package2;
    
    public class Person3Demo {
        public static void main(String[]args)
        {
            Person3 p = new Person3();
            p.speak();
            Person3 p1 = new Person3("hejinjin",23);
            p1.speak();
            Person3 p2 = new Person3("hejinjin");
            p2.speak();
            Person3 p3 = new Person3(23);
            p3.speak();
            p1.show();
                
        }
        
    
    }

    利用this关键字进行构造函数和构造函数之间的调用:

    package com_package2;
    
    public class Person3 {
        private int age;
        private String name;
        public int getAge() {
            return age;
        }
        public void setAge(int age) {
            this.age = age;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        Person3()
        {  
            System.out.println("hahahaa");
             
        }
        Person3(String name,int age)
        {  // this();
            this.name = name;
            this.age = age;
            
        }
        Person3(String name)
        {
            //this();
            this.name = name;
            
        }
        Person3(int age)
        {
            this("hejinjin",342);
            this.age = age;
        }
        public void speak()
        {
            System.out.println("name = "+this.name+" "+"age="+this.age);
            
            
        }
        public void show()
        {
            this.speak();
            
        }
    }
    package com_package2;
    
    public class Person3Demo {
        public static void main(String[]args)
        {
            Person3 p = new Person3();
            p.speak();
            Person3 p1 = new Person3("hejinjin",23);
            p1.speak();
            Person3 p2 = new Person3("hejinjin");
            p2.speak();
            Person3 p3 = new Person3(238);
            p3.speak();
            p1.show();
                
        }
        
    
    }

    hahahaa
    name = null age=0
    name = hejinjin age=23
    name = hejinjin age=0
    name = hejinjin age=238
    name = hejinjin age=23

     

    特殊情况:注意:调用本类中的构造函数的this语句必须定义在构造函数的第一行,因为初始化动作要先完成

  • 相关阅读:
    第一章 经济基础知识
    105_实例
    SQL开窗函数
    Docker教程:Docker入门实践
    102_HDFS分布式文件系统
    [转]远程连接出现身份验证错误,要求的函数不受支持
    flutter创建工程指定iOS及Android开发语言
    架构师学习之路:康威定律
    智能识别客户收货地址信息
    深入理解Apache Dubbo与实战 pdf
  • 原文地址:https://www.cnblogs.com/mmlovejj/p/4434721.html
Copyright © 2020-2023  润新知