1、this用于普通方法和构造方法中
(1)用于构造方法中,可以通过this.i=i,this为当前正在创建的对象。这样通过参数i,对对象中的成员变量值进行重新赋值。下面知道静态属性k,属于类的,不能用this.k。
(2)用于普通方法中,对于this.i,this表示当前正在调用对象的成员变量值,就是为了改变初始的类中的属性变量值
http://www.cnblogs.com/hasse/p/5023392.html参看
package java_part;
class A1{
public int i=99;
public A1(int i){
this.i=i;
System.out.printf("%d\n",i);
}
public void show(){
System.out.printf("%d\n",this.i);
}
}
public class ClassThis {
public static void main(String[] args) {
// TODO Auto-generated method stub
A1 aa1=new A1(2);
System.out.printf("%d",aa1.i);
}
}
输出2/2
**static用处,Static修饰的属性和普通方法,不能修饰构造方法
**
查看用类创建多少对象
(1)在某一个属性定义前加static意味着属性不再是某一个对象的,n个对象共用,则分配空间不再一一分配。静态变量在data segment,数据区拷贝。
属性成员属于对象还是属于类的?类本身,当然也属于类对象。
/*
本程序证明了: static属性i是属于类本身,或者讲:没有对象,我们仍然可以直接通过类名的方式访问该类内部的static属性
static方法g同理
*/
class A
{
public static int i = 10;
public static void f()
{
System.out.printf("2019年");
}
}
class TestStatic_2
{
public static void main(String[] args)
{
System.out.printf("%d\n", A.i);//
A.f();//属性方法均可以用类名点访问
}
}
10
2019年
(2)通过对象访问静态函数ok
class A
{
public static int i = 10;
public static void f()
{
System.out.printf("2019年");
}
}
class TestStatic_3
{
public static void main(String[] args)
{ A aa=new A();
System.out.printf("%d\n", aa.i);//
aa.f();//属性方法均可以用类名点访问,对象属于类,类中的函数可以调用
}
}
(3)static不属于控制符,是修饰符
/*
本程序证明了:
只有非private的static成员才可以通过类名的方式访问
static只是表明了该成员具有了可以通过类名访问的潜在特征
但是否可以通过类名访问,还必须满足一个条件: **该成员必须是非private**
*/
class A
{
private static int i = 10;
private static void f()
{
System.out.printf("2019年\n");
}
}
class TestStatic_4
{
public static void main(String[] args)
{
//A.f();
A.i = 22;//error
}
}
(4)非静态的函数g(),只能通过类对象访问;静态的函数f()属于类本身,可以通过类.f()访问。非静态的能否访问静态的f()方法,可以! 能访问g(),一定有对象,则一定有f()。但是静态的不能访问非静态的!
/*
2009年5月29日15:14:29
本程序证明了:
静态方法不能访问非静态成员
非静态方法可以访问静态成员
*/
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 TestStatic_5
{
public static void main(String[] args)
{
A aa = new A();
//aa.g();
aa.f();
//A.g(); //error
}
}
static的用处—
1、多个对象,计算设计了多少个对象
2、单态设计模式,一个对象。将构造方法设计成static
class A
{
private int i;
private static int cnt = 0;//内部的,static共有的变量cnt
public A()
{
++cnt;
}
public A(int i) **//构造函数public A(),修饰符没有**
{
this.i = i;
++cnt;
}
public static int getCnt() //静态的函数通过类.函数访问
{
//return 返回的是A对象的个数;
return cnt;
}
}
public class TestStatic_1
{
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());
}
}
2、单态设计模式,一个对象。将构造方法设计成static(构造方法先禁止,private修饰A(),但通过其他方法造出一个事物)
注意:一个类的属性可以是类的对象。
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 M
{
public static void main(String[] args)
{
C cc = new C();
cc.g();
}
}
通过创建对象作为另一个类的属性,将在自己本身类中创建对象,并设置为私有的static,将只有一个
class A
{
public int i = 20;
private static A aa = new A(); //aa是否是A对象的属性,静态的不能访问非静态的,所以static保持
private A()
{
}
public static A getA() //static 一定是不能省略的,必须通过类来访问,因为外部不能创建对象
{
return aa;
}
}
public class TestStatic_2
{
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,相等,说明只有一个对象
}
}