- 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; // 类 的 域 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));
}
}
使用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