zoukankan      html  css  js  c++  java
  • 对象的this引用

      Java中的this关键字总是指向调用该方法的对象。根据this出现位置的不同,this作为对象的默认引用有两个功能:

    1.构造器中引用该构造器正在初始化的对象。

    2.在方法中引用调用该方法的对象。

      this关键字最大的作用就是让类中一个方法,访问该类里的另一个方法或实例变量。假设定义了一个Student类,这个student对象的记录成绩的方法(scores())需要调用姓名属性,那么该如何做呢?请看下面的例子:

    public class Student {
        private int scores;
        private String names;
        public void name(String name) {
            this.names = name;
        }
        public void score(int score) {
            this.scores = score;
            System.out.println(this.names +"的成绩是:"+this.scores);
        }
    }

    在方法的重构当中,this也可以调用构造方法,需要注意的是:

    this()只能写在本类构造方法里面,不能在其他任何非构造方法里用;
    this()只能写在构造方法里的第一句。
    this()的括号里可以放形参,调用本类的其他构造方法。
    this.***或者this.***()代表每个对象本身的this引用
  • 相关阅读:
    Search Insert Position
    Sum Root to Leaf Numbers
    String to Integer (atoi)
    Populating Next Right Pointers in Each Node
    Triangle
    Pascal's Triangle II
    Longest Consecutive Sequence
    属性透明度
    ul的列表符号在IE6下显示不全
    table的属性border-collapse 设置边框是否合并
  • 原文地址:https://www.cnblogs.com/maopao55555/p/6131004.html
Copyright © 2011-2022 走看看