this的含义,static的用法

java的类中的方法,都隐含了this这个指针

this是指向调用他的函数,以及正在创建的函数


class A
{
int i;

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

void show(A * this)                               //解决办法是,方法里隐藏了一个this指针,指向调用他的对象
{
System.out.printf("i = %d\n", this.i);          //这样,输出的就是当前调用他的对象里的i了
}
}
class test
{
public static void main(String[] args)
{
A aa1 = new A(10);
A aa2 = new A(20);

aa1.show();                       //aa1.show与aa2.show都是同一个show方法,这样的话,就必须区分输出的i是aa1的还是aa2的
aa2.show();


}
}


class A
{
int i;

public A (int i)
{
this.i = i;                             //这个this表示正在创建的对象
}

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

A aa = new A(5);
aa.show();

}
}



static修饰的变量储存在数据区

所以,不同的对象都是共用这一个变量

static中的变量存储在数据区,所有对象都共用这一个
这里,只要一个对象.i的值变了,其他的都变了

class A
{
public static int i;

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

}


class test
{
public static void main(String[] args)
{

A aa = new A();
A bb = new A();
aa.i = 10;
bb.i = 20;
aa.show();
bb.show();

}
}


static可以不用创建对象,直接类名访问

class A
{
public static int i = 20;

private static void show()
{
System.out.printf("2014年9月26日19:21:59");
}


}


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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值