/*使用类成员*/
public class MyClass { //构造类
static int statInt=4; //定义静态变量
static double statDouble=16.0;
int instInt; //定义变量
double instDouble;
public static void staticMethod(){
System.out.println("statInt="+statInt+":statdouble="+statDouble);//打印出静态变量和变量
}
public void instMethod(){
System.out.println("instInt="+instInt+";instdouble="+instDouble);//打印出静态变量和变量
}
public MyClass(int intArg,double doubleArg){
instInt=intArg;
instDouble=doubleArg;
}
public static void changestatic(int newInt,double newDouble){
statInt=newInt; statDouble=newDouble;
}
public static void main (String args[]){
MyClass instance1=new MyClass(1,2);//实例化
MyClass instance2=new MyClass(3,4.0);
instance1.instMethod();//调用实例方法
instance2.instMethod();//
MyClass.staticMethod();//调用静态方法
instance1.staticMethod();
instance2.staticMethod();
instance1.changestatic(8, 8.0);//改变静态变量的值
instance2.staticMethod();//再次输出静态变量的值
MyClass.staticMethod();
}
}
public class MyClass { //构造类
static int statInt=4; //定义静态变量
static double statDouble=16.0;
int instInt; //定义变量
double instDouble;
public static void staticMethod(){
System.out.println("statInt="+statInt+":statdouble="+statDouble);//打印出静态变量和变量
}
public void instMethod(){
System.out.println("instInt="+instInt+";instdouble="+instDouble);//打印出静态变量和变量
}
public MyClass(int intArg,double doubleArg){
instInt=intArg;
instDouble=doubleArg;
}
public static void changestatic(int newInt,double newDouble){
statInt=newInt; statDouble=newDouble;
}
public static void main (String args[]){
MyClass instance1=new MyClass(1,2);//实例化
MyClass instance2=new MyClass(3,4.0);
instance1.instMethod();//调用实例方法
instance2.instMethod();//
MyClass.staticMethod();//调用静态方法
instance1.staticMethod();
instance2.staticMethod();
instance1.changestatic(8, 8.0);//改变静态变量的值
instance2.staticMethod();//再次输出静态变量的值
MyClass.staticMethod();
}
}