十、java基础之this介绍

/*
一、this关键字:
  1.this什么?
this是一个引用类型
在堆中的每一个java对象上都有this
this保存的内存地址指向自身
  2.this能用在哪里?
第一:this可以用在成员方法中
第二:this可以用在构造方法中
      this(实参);
      通过一个构造方法去调用另一个构造方法
      目的:代码复用
      注意:this(实参)必须出现在构造方法中第一个语句;否则报:Error:(40, 13) java: 对this的调用必须是构造器中的第一个语句
   3.this可以用来区分成员变量和局部变量

   4.this不能用在静态方法中
    1.静态方法的执行根本不需要java对象的存在,直接使用,类型,的方式访问
    2.而this代表的是当前的对象,所以静态方法中根本没有this
 
 */

1.原始代码:
public class ThisTest06 {

    public static void main(String[] args){

        //创建对象
        MyDate t1=new MyDate(2018,8,23);
        System.out.println(t1.year+"年"+t1.mouth+"月"+t1.day+"日");

        //创建对象

        MyDate t2=new MyDate();
        System.out.println();
    }
}

class MyDate{
    //Field
    int year;
    int mouth;
    int day;

    //constructor

    MyDate(){}

    MyDate(int _year,int _mouth,int _day){
        year=_year;
        mouth=_mouth;
        day=_day;
    }
}
2.this可以用在成员方法中
public class ThisTest07 {

    public static void main(String[] args){
        Employee em=new Employee(1234,"chushujin");
        em.work();

        Employee em1=new Employee(12345,"chushujin1");
        em1.work();

    }
}

class Employee{

    //员工编号
    int empno;
    //员工名
    String ename;

    //Constructor
    Employee(){}

    Employee(int _empno,String _ename){
        empno=_empno;
        ename=_ename;

    }
    //提供一个员工工作方法
    //this用在成员方法中,谁去调用这个成员方法,this就代表谁
    //this指的就是当前对象
    public void work(){
        System.out.println(this.ename+"在工作!");
        //System.out.println(ename+"在工作!");//可以省略
    }

    public void m1(){
        this.m2();
    }
    public void m2(){
        System.out.println("THIS");
    }


}

2.this可以用在构造方法中

      this(实参);
      通过一个构造方法去调用另一个构造方法
      目的:代码复用
      注意:this(实参)必须出现在构造方法中第一个语句;否则报:Error:(40, 13) java: 对this的调用必须是构造器中的第一个语句
public class ThisTest10 {

    public static void main(String[] args){
        MyDatee MDe=new MyDatee();
        System.out.println(MDe.year+"年"+MDe.mouth+"月"+MDe.day+"日");

    }
}

class MyDatee{
    //Field
    int year;
    int mouth;
    int day;

    //constructor
    //默认1999-1-1日
    MyDatee(){

        this(1999,1,1);
        /*year=1999;
        mouth=1;
        day=1;
        */
        //this(1999,1,1);
    }

    MyDatee(int _year,int _mouth,int _day){
        year=_year;
        mouth=_mouth;
        day=_day;
    }
}

3.this可以用来区分成员变量和局部变量

public class ThisTest08 {

    public static void main(String[] args){

        Manager m1= new Manager("KING");

        Manager m2=new Manager("JOY");

        System.out.println("m1---->"+m1.getName());
        System.out.println("m2---->"+m2.getName());



    }
}

class Manager{
    //Field
    private String name;

    //Constructor
    Manager(String name){
        this.name=name;
    }
    //Method
    //成员方法
    public void setName(String name){
        this.name=name;

    }
    public String getName(){
        return this.name;
    }
}
4.this不能用在静态方法中
    1.静态方法的执行根本不需要java对象的存在,直接使用,类型,的方式访问
    2.而this代表的是当前的对象,所以静态方法中根本没有this

public class ThisTest09 {


    //没有static的就是成员变量,成员变量不能直接访问,引用.变量名
    String str;
    public static void main(String[] args){
        Person.m1();


        //System.out.println(str);Error:(13, 28) java: 无法从静态上下文中引用非静态 变量 str

        ThisTest09 tt=new ThisTest09();
        System.out.println(tt.str);//null


    }
}

class Person{

    //Field
    String name;
    //Constructor
    Person(){}

    Person(String name){
        this.name=name;
    }

    //静态方法
    public static void m1(){
        //System.out.println(this.name);

        //如果想要访问name
        Person p=new Person("kk");
        System.out.println(p.name);

    }
}
 














猜你喜欢

转载自www.cnblogs.com/chushujin/p/10049100.html
今日推荐