静态(初学)

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值