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);
}
}