静态(初学)

package day01;

public class Star {
static String xiaoshuo; //静态变量
String name;
int age;
static String zuozhe;

static {zuozhe="jing";}//静态代码块
//静态方法只能访问静态变量 非静态方法都可以访问(静态非静态)
//不可以用this. 因为静态与对象没关系。
public static String getXiaoshuo() {//静态方法
return xiaoshuo;
}

public static void setXiaoshuo(String xiaoshuo) {
Star.xiaoshuo = xiaoshuo;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}
}

package day01;

public class Case11 {
public static void main(String[] args) {
Star one=new Star();
//one.xiaoshuo="天龙八部";//如果用one.小说,那么这个数就是通用的 two也是这个
Star.xiaoshuo="天龙八部";
Star two=new Star();
System.out.println(two.xiaoshuo);
System.out.println(two.zuozhe);



}
}



原文地址:https://www.cnblogs.com/worldof/p/10671458.html