(邓爱萍)类对象-this关键字

  1. this.域名(成员变量

当一般方法的 参数名------对象的域名(成员变量名),,在一般方法内部使用 this.域名

public class Rectangle {

    double x;------------------------------ 域(成员变量)

    double y;------------------------------

    Rectangle(double a,double b){

          x=a;

          y=b;

    }

    double area(){

          double s;  局部变量s

          s=x*y;    //  类 的 域 x,y 的乘积

          return s;

    }

    double multiply(double x,double y){      

//multiply()方法的参数名 ---所在Rectangle类 的域名--相同              

                //在方法内部,参数 将隐藏 同名的域

          double s;  //局部变量s  (与第9行不同)

          s=x*y;        // 方法 的 参数名 x,y      

    

          return s;                  

         

    }


    public static void main(String[] args) {
        Rectangle r=new Rectangle(3,4);=========对象r
        System.out.println("面积为:"+r.area());

        System.out.println("multiply方法的调用结果为:"+r.multiply(10,20));

    }

}

public class Rectangle {

    double x;------------------------------ 域(成员变量)

    double y;------------------------------

    Rectangle(double a,double b){

          x=a;

          y=b;

    }

    double area(){

          double s;  局部变量s

          s=x*y;    //  xy 的乘积

          return s;

    }

    double multiply(double x,double y){      

//multiply()方法的参数名 ---所在Rectangle 域名--相同              

                //在方法内部,参数 将隐藏 同名的域

          double s;  //局部变量(与第9行不同)

          s=x*y;        // 方法 参数名 xy      

    

          return s;                  

         

    }

 

    public static void main(String[] args) {

        Rectangle r=new Rectangle(3,4);=========对象r

        System.out.println("面积为:"+r.area());

        System.out.println("multiply方法的调用结果为:"+r.multiply(10,20));

    }

}

 

使用this

public class Rectangle {

    double x;------------------------------ 域(成员变量)

    double y;------------------------------

    Rectangle(double a,double b){

           x=a;

           y=b;

    }

    double area(){

           double s;

           s=x*y;   域 x,y 的乘积

           return s;

    }

    double multiply(double x,double y){   // 方法 的 参数名 x,y

           double s;

           //s=x*y;

           s=this.x*this.y;   ======== //this 表示当前类对象 r

           return s;                   // this.x this.y对象 的域 x,y        

    }



    public static void main(String[] args) {

        Rectangle r=new Rectangle(3,4);=========对象r

        System.out.println("面积为:"+r.area());

        System.out.println("multiply方法的调用结果为:"+r.multiply(10,20));

    }

}

public class Rectangle {

    double x;------------------------------ 域(成员变量)

    double y;------------------------------

    Rectangle(double a,double b){

           x=a;

           y=b;

    }

    double area(){

           double s;

           s=x*y;   域 x,y 的乘积

           return s;

    }

    double multiply(double x,double y){   // 方法 的 参数名 x,y

           double s;

           //s=x*y;

           s=this.x*this.y;   ======== //this 表示当前类对象 r

           return s;                   // this.x this.y对象 的域 x,y        

    }

 

    public static void main(String[] args) {

        Rectangle r=new Rectangle(3,4);=========对象r

        System.out.println("面积为:"+r.area());

        System.out.println("multiply方法的调用结果为:"+r.multiply(10,20));

    }

}

2.this.域名全局变量)

对象的域名-成员变量(全局变量)  方法内的局部变量名 相同

1)局部变量 将隐藏 同名的全局变量      个人书库  图书馆大库

2)使用关键字 this,,表明 全局变量

public class EqualVarible {

    int i=2;

 

    void move(){

       int i=6;

    System.out.println(i);                      //局部变量

    System.out.println(this.i);                    //全局变量

    }

    public static void main(String[] args) {

       // TODO Auto-generated method stub

EqualVarible r=new EqualVarible();

 r.move();

    }

}

 

3.this.方法()

类的内部,如果一个方法需要调用另一个方法。。调用当前对象的 其他方法

 

4.重载的构造方法中  this.(参数表)

public class ThisApp{

          int a;

          int b;

          int c;

          ThisApp(){}

          ThisApp(int x){ a=x;}

          ThisApp(int x,int y){

             this(x);

             b=y;

          }

          ThisApp(int x,int y,int z){

             this(x,y);

             c=z;

          }

 

对this的调用,,必须是 构造器的 第一个语句

5555

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wangchuang2017

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值