Java方法重载的使用、方法中调用this关键字

方法重载:

1、Java中行为的多态性就是方法的重载,而行为是通过类中方法来体现的;

2、方法重载指:一个类中可以有多个方法具有相同的名字,但是这些方法的参数必须不同;

参数不同指:

1、参数的个数不同 ;

2、参数个数相同,但是对于的某个参数的类型不同 ;

如下代码演示:

void test ( int x ){ }
void test ( int x,int y ){ }
void test (double x,int y ){ }

需要注意的是:

相对于参数名来讲:参数个数相同,类型相同,但参数名字不同 ; 是错误的,如下:

	void test(double x,int y){}
	void test(double xx,int yy){}
//错误:Duplicate method test(double, int) in type Method_heavyLoad

相对于方法类型来讲:参数个数相同,类型相同,参数名字相同 ,但方法类型不同; 是错误的,如下:

	void test(double x,int y){}
	float test(double x,int y){
		return x+y;
	}
//错误:Duplicate method test(double, int) in type Method_heavyLoad

总结:在进行方法重载时,要记住参数不同的两个条件;方法类型 和 参数名字 不参与重载的判断;

this关键字的定义:

1、this关键字表示某个对象;

2、this可以在 实例方法 与 构造方法 中使用,但是不能在 类方法 中使用 ,因为程序加载时,类方法就被分配了入口地址,而此时类对象还没有创建,又 this 关键字是表示某个对象的,所有 this 不能在类方法中进行使用;

在方法中调用this关键字:

一、在构造方法中调用this

1、this关键字在类中某个构造方法时,代表这个this使用该构造方法创建的对象;

如下代码演示:

public class Method_heavyLoad {
	
	//创建实例变量,类变量,实例方法
	int caseVariable = 11;
	static int classVariable = 111;
	void caseMethod (){
		System.out.println("实例变量的值="+caseVariable);
	}
	//构造方法中使用this
	Method_heavyLoad(){
		this.classVariable -= caseVariable ; //操作类变量减去实例变量
		this.caseVariable += classVariable ; //操作实例变量加上类变量
		System.out.println("对象mhtest 输出类变量的值="+classVariable);
		System.out.println("对象mhtest 输出实例变量的值="+caseVariable);
		this.caseMethod();  //操作实例方法,相当于:mhtests.caseMethod();
	}
	// 构造方法中使用this
	Method_heavyLoad(int x, int y) {
		this.classVariable = x; // 操作类变量
		this.caseVariable = y; // 操作实例变量
		System.out.println("对象tests 输出类变量的值=" + classVariable);
		System.out.println("对象tests 输出实例变量的值=" + caseVariable);
		this.caseMethod(); // 操作实例方法,相当于:tests.caseMethod();
	}
	
	public static void main(String[] args) {
		//创建对象 
		Method_heavyLoad mhtest = new Method_heavyLoad();
		Method_heavyLoad tests = new Method_heavyLoad(1,11);
                System.out.println(mhtest.caseVariable);
		System.out.println(tests.caseVariable);
	}

}

代码输出结果:

对象mhtest 输出类变量的值=100
对象mhtest 输出实例变量的值=111
实例变量的值=111
对象tests 输出类变量的值=1
对象tests 输出实例变量的值=11
实例变量的值=11

111
11

在以上代码中,两个构造方法中的 this 分别对应自己的对象,互不影响;

二、在实例方法中调用this

1、实例方法只能通过对象来调用,不能通过类名来调用;

2、this关键字在类中某个实例方法时,这个this就代表使用该实例方法的对象;也就是,对象调用实例方法,则该实例方法中的 this 就代表该对象 ;

如下代码演示:

public class Method_heavyLoad {
	
	//在实例方法中,通过this,可以操作类变量吗? 可以进行操作
	//创建实例变量、类变量
	double testx = 11.111 ;
	static double tests = 111.1111 ;
	//创建实例方法
	void caseMethods (double x,double y){
		this.testx = x+y;      //相当于 mhtest.testx = x+y ;
		this.tests = x * y ;  //相当于 mhtest.tests = x * y ;
		System.out.println("对象mhtest实例变量的值="+this.testx);
		System.out.println("对象mhtest类变量的值="+this.tests);
	}
	
	public static void main(String[] args) {
		//创建对象 ,在构造方法中调用this操作实例变量,类变量,实例方法
		Method_heavyLoad mhtest = new Method_heavyLoad();
		Method_heavyLoad mhtests = new Method_heavyLoad();
		mhtest.caseMethods(1, 10.1);
		System.out.println("对象mhtests实例变量的值="+mhtests.testx);
		System.out.println("对象mhtests类变量的值="+mhtests.tests);
		
	}
}

代码输出结果:

对象mhtest实例变量的值=11.1
对象mhtest类变量的值=10.1
对象mhtests实例变量的值=11.111
对象mhtests类变量的值=10.1

以上代码中:

对象共享类变量,使用当对象 mhtest 改变类变量 tests 的值时,对象mhtests 类变量的值也会改变;

总结 this 关键字:

1、this 关键字 就表示 某个对象;当某个对象调用 构造方法 或者 实例方法 时,方法中 this 就是 调用方法的 对象 ;

2、this 不能在 类方法中进行调用;

3、this 关键字 可以操作 实例变量,类变量,实例方法,构造方法;

如下演示:

public class Method_heavyLoad {
	//this 关键字 可以操作 实例变量,类变量,实例方法,构造方法;
	double caseone = 11.11;     //实例变量
	static double classone = 111.111;  //类变量

        //在构造方法中 操作 实例变量 与 类变量,调用实例方法
	Method_heavyLoad(){
		this.caseone += 11  ;
		this.classone += 11 ;
		System.out.println("构造方法中 操作 实例变量 与 类变量:");
System.out.println("实例变量caseone的值="+this.caseone+" "+"类变量classone的值="+this.classone);
		System.out.println("构造方法中 调用实例方法:");
		this.testMethod(1.1, 11.11);   //相当于 test.testMethod(1.1, 11.11);
	}

	//在实例方法中 操作 实例变量 与 类变量
	void testMethod(double x,double y){
		this.caseone += x  ;
		this.classone += y ;
		System.out.println("实例方法中 操作 实例变量 与 类变量:");
System.out.println("实例变量caseone的值="+this.caseone+" "+"类变量classone的值="+this.classone);
	}

	
	
	public static void main(String[] args) {
		
		//this 关键字 可以操作 实例变量,类变量,实例方法,构造方法;
		Method_heavyLoad test = new Method_heavyLoad();
		
	}
}

代码输出结果:

构造方法中 操作 实例变量 与 类变量:
实例变量caseone的值=22.11 类变量classone的值=122.111
构造方法中 调用实例方法:
实例方法中 操作 实例变量 与 类变量:
实例变量caseone的值=23.21 类变量classone的值=133.221

发布了57 篇原创文章 · 获赞 10 · 访问量 7548

猜你喜欢

转载自blog.csdn.net/LagerSwan/article/details/104154813
今日推荐