zoukankan      html  css  js  c++  java
  • java中this总结(转载请注明出处)

    1:this在构造方法中:this可以进行构造方法中的相互调用,this(参数);

    2:this调用方法中,代表调用该方法的对象的地址,例如下面的代码比较

     1 package thisTest;
     2 
     3 public class Student {
     4     
     5     public static void main(String[] args) {
     6         Student student = new Student();
     7         System.out.println(student);
     8         student.run();
     9     }
    10     public void run() {
    11         System.out.println(this);//this代表当前调用这个方法的对象的地址
    12     }
    13 }

    输出结果是:thisTest.Student@52e922
          thisTest.Student@52e922   所以可以明显看到this和创建的student是相同的东西,都代表创建对象所在栈的地址。

    还有一个就是,在另外一个方法中调用另一个方法,我们知道直接可以进行方法名(实参列表)来调用,其实实际上是this.方法名(实参列表)  理解:this代表调用这个方法的一个实例对象,那么this.方法名(实参列表)就是对象调用了一个方法,很容易理解啦,但是和构造方法不同的是:方法直接的调用是this.   来相互调用的,所用this可以 省略,但是在构造方法中是用this(参数列表)来进行多个构造方法中的相互调用,所以this无法省略,更重要的区别是:构造方法this只能用一次,因为只能放在有效代码的首句,首句只有一个,所以就只能来一次,而普通方法中的可以N多次。

    3:this调用属性:在一个方法中如果存在了一个和类中的成员变量同名的局部变量,这个时候如果要用全局变量的值则需要用this来调用,原因:局部变量会对全局变量进行覆盖,this代表调用这个方法时的当前实例对象,那么this.name就是全局变量了,很好理解吧。

    注意:::::::!!!!this不能使用在静态方法中,原因:this是代表实例对象,static修饰的静态方法是类加载时就分配了方法的入口地址,

    所以不可能在加载时有实例对象的

    下面有几个考察this的代码,试着区分一下, 检查自己的理解成果:

     1 public class Student {
     2 
     3     String name;
     4     
     5     void doHomework(){
     6         System.out.println(this.name+"正在做作业.......");
     7     }
     8 
     9     void speak(){
    10         new Student().doHomework();
    11         System.out.println(name+"正在说话......");
    12     }
    13     
    14     public static void main(String[] args) {
    15         Student student = new Student();
    16         student.speak();
    17     }
    18 }
     1 public class Student {
     2 
     3     String name;
     4     
     5     void doHomework(){
     6         System.out.println(this.name+"正在做作业.......");
     7     }
     8 
     9     public static void main(String[] args) {
    10         Student student = new Student();
    11         student.name="王五";
    12         student.doHomework();
    13     }
    14 }
     1 public class Student {
     2 
     3     String name;
     4     
     5     void doHomework(){
     6         System.out.println(this.name+"正在做作业.......");
     7         name="刘颖";
     8     }
     9     
    10     void speak(){
    11         System.out.println(name+"正在说话......");
    12     }
    13     
    14     public static void main(String[] args) {
    15         Student student = new Student();
    16         student.doHomework();
    17         student.speak();
    18     }
    19 }
     1 public class Student {
     2 
     3     int age;
     4     String name;
     5 
     6     public Student(int age) {
     7         this.age = age;
     8     }
     9     public Student(String name) {
    10         this.name = name;
    11     }
    12     public Student(int age, String name) {
    13         this(age);
    14         new Student(name);
    15     }
    16     public static void main(String[] args) {
    17         new Student(12, "王五");
    18     }
    19 }
     1 public class Student {
     2 
     3     int age;
     4     String name;
     5 
     6     public Student(int age) {
     7         this.age = age;
     8     }
     9     public Student(String name) {
    10         this.name = name;
    11     }
    12     public Student(int age, String name) {
    13         this(age);
    14         new Student(name);
    15     }
    16     public static void main(String[] args) {
    17         new Student(12, "王五");
    18     }
    19 }
     1 public class Student {
     2 
     3     String name;
     4 
     5     public static void print() {
     6         System.out.println(this.name);
     7     }
     8     public static void main(String[] args) {
     9         print();
    10     }
    11 }
  • 相关阅读:
    使用正则表达式,取得点击次数,函数抽离
    爬取校园新闻首页的新闻
    网络爬虫基础练习
    Hadoop大作业
    Hive基本操作与应用
    熟悉HBase基本操作
    爬虫大作业
    熟悉常用的HDFS操作
    数据结构化与保存
    使用正则表达式,取得点击次数,函数抽离
  • 原文地址:https://www.cnblogs.com/lyxcode/p/9004923.html
Copyright © 2011-2022 走看看