this和static的用法

this:

this是一个系统隐含的指针,被自动附加在非静态成员函数参数列表中


this出现在构造函数里(静态函数内部,没有this指针),代表当前时刻正在创建的对象;this出现在普通函数里,代表当前时刻正在调用该函数的对象,此时this可以省略。


下面举个例子

例1:

class A {
	public int i;

	public A(int j) {
		i = j;
	}

	public void show() {
		System.out.println("i = " + i);
	}
}

class Test {
	public static void main(String[] args) {
		A a1 = new A(5);
		A a2 = new A(8);
		a1.show();
		a2.show();
	}
}

输出结果为:

i = 5

i = 8


在本程序中,a1a2在内存中分别有各自的数据成员i,但是a1a2共用show()方法,this表示当前时刻正在调用show方法的对象

再举一个例子

例2:

class A
{
	private int i = 99;
	public A(int i)
	{
		this.i = i;  //将形参 i 赋给该构造方法本次运行所创建的那个新对象的i数据成员
	}
	public void show(){
		System.out.println("i = " + this.i);
			//this表示当前时刻正在调用show方法的对象
			//this可以省略
	}
}

public class Test
{
	public static void main(String[] args){
		A aa1 = new A(100);
		aa1.show();
		
		A aa2 = new A(200);
		aa2.show();
	}	
}

输出结果为:

i = 100

i = 200


i = 99的值是不会输出的

另外,使用this还有一个好处,即在使用第三方编译器(如eclipse)时,在this后面加一个 . ,自会出现相应的变量


static(静态)

       static修饰的类的变量属性和函数方法都属于类本身,而不是属于对象,即使不创建对象,也可以使用类本身的静态成员

下面举个例子

例3:

class A {
	public static int i = 10;

	public static void f() {
		System.out.printf("2014年6月29日19:09:51");
	}
}

class Test {
	public static void main(String[] args) {
		System.out.printf("i = %d\n", A.i);
		A.f();
	}
}

输出结果为:

i = 10

2014年6月29日19:09:51

本程序证明了: static属性i和static方法f是属于类本身,或者讲:没有对象,我们仍然可以直接通过类名的方式访问该类内部的static属性和static方法


静态成员分为

静态数据成员

静态方法成员

使用静态成员的两种方法

类名.静态成员名

类对象名.静态成员名


    一个类的不同对象的变量属性是不同的,但是一个类的不同对象的static变量属性是通用唯一的。下面举个例子

例4:

class A {
	public static int i = 10;

	public void show() {
		System.out.printf("i = %d\n", i);
	}
}

class Test {
	public static void main(String[] args) {
		A a1 = new A();
		A a2 = new A();
		A a3 = new A();

		a1.i = 20;
		a2.show();
		System.out.printf("i = %d\n", a3.i);
	}
}

输出结果为:

i = 20

i = 20


本程序证明了: A类的多个对象公用一个static属性i


当然,static属性和方法虽然属于类本身,虽然可以通过类名的方式访问,但是static属性和方法很明显也属于类对象,当然也可以通过类对象名的方式访问,下面的例子证明了此点

例5:

class A
{
	public static int i = 10;

	public static void f()
	{
		System.out.printf("2014年6月29日19:19:55\n");
	}
}

class Test
{
	public static void main(String[] args)
	{
		A aa = new A();
		//A.f();
		aa.f();
		System.out.printf("i = %d\n", aa.i);
	}
}

输出结果为:

2014年6月29日19:19:55

i = 10


另外,还有一点,只有非privatestatic成员才可以通过类名的方式访问

static只是表明了该成员具有了可以通过类名访问的潜在特征

但是否可以通过类名访问,还必须满足一个条件: 该成员必须是非private 

6

class A
{
	private static int i = 10;

	private static void f()
	{
		System.out.printf("2009年5月29日15:15:50\n");
	}
}

class Test
{
	public static void main(String[] args)
	{
		A.f();
		A.i = 22;
	}
}

本程序的第1415行是有错误的,理由如上所述

总之

只有非private的static成员才可以通过类名的方式访问。

Static的函数不能调用非static的函数和属性变量。

非static的函数可以访问static的函数和属性变量。


还有,一个类的属性可以是个类对象,下面举两个例子

例7.1:

class A {
	public void f() {
		System.out.printf("今天到晚上还是没有下雨!\n");
	}
}

class C {
	public int i = 10;
	public A aa = new A(); // aa一定是个对象, 但同时aa也是C的属性

	public void g() {
		aa.f();
	}
}

class Test {
	public static void main(String[] args) {
		C cc = new C();
		cc.g();
	}
}

输出结果为:

今天到晚上还是没有下雨!


例7.2:

class A {
	public int i = 20;
	private static A aa = new A(); // aa是A对象的属性

	private A() {
	}

	public static A getA() { // static 一定是不能省略的
		return aa;
	}
}

public class Test {
	public static void main(String[] args) {
		A aa1 = A.getA();
		A aa2 = A.getA();

		aa1.i = 99;
		System.out.printf("%d\n", aa2.i);

		if (aa1 == aa2)
			System.out.printf("aa1 和 aa2相等\n");
		else
			System.out.printf("aa1 和 aa2不相等\n");

		// A aa1 = new A(); // error 如果A类的构造方法是private的,则new A()就会报错!
		// A aa2 = new A(); //同上
	}
}

输出结果为:

99

aa1 和 aa2相等


下面再举个例子,目的是使用静态变量统计一个类生成的对象的个数

8

class A {
	private int i;
	private static int cnt = 0;

	public A() {
		++cnt;
	}

	public A(int i) {
		this.i = i;
		++cnt;
	}

	public static int getCnt() {
		// return 返回的是A对象的个数;
		return cnt;
	}
}

public class Test {
	public static void main(String[] args) {
		System.out.printf("当前时刻A对象的个数是: %d\n", A.getCnt());
		A aa1 = new A();
		System.out.printf("当前时刻A对象的个数是: %d\n", A.getCnt());
		A aa2 = new A();
		System.out.printf("当前时刻A对象的个数是: %d\n", A.getCnt());

	}
}

输出结果为:

当前时刻A对象的个数是: 0

当前时刻A对象的个数是: 1

当前时刻A对象的个数是: 2


最后,

    静态方法不能以任何形式引用this和super关键字

    静态方法只能访问类的静态成员

   但非静态方法却可以访问类中所有成员,包括静态成员


示例如下

例9:

class A {
	private static int i = 10;
	public int j = 99;

	public static void f() {
		// g(); //error 静态方法不能访问非静态成员
		// j = 22; //error
		System.out.printf("FFFF\n");
	}

	public void g() {
		// f(); //OK 说明非静态方法可以访问静态成员
		System.out.printf("GGGG\n");
		System.out.printf("%d\n", i);
	}
}

class Test {
	public static void main(String[] args) {
		A a = new A();
		a.g();
		a.f();

		// A.g(); //error
	}
}

输出结果为:

GGGG

10

FFFF


【所有代码均在windows系统下eclipse环境JDK 1.7下运行通过】

(如有错误,敬请指正)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值