浅谈Static关键字

1.使用static关键字声明的属性为全局属性

  未使用static关键字指定city之前,如果需要将Tom,Jack,Mary三人的城市均改成Beijing,需要再次声明三次对象的city为Beijing

 package packageone;
class People{
String name;
String city = "Shanghai";
public People(String name) {
this.name = name;
}
public void showInfo() {
System.out.println("姓名:"+name+","+"城市:"+city);
}
}
public class StaticDemo { public static void main(String[] args) {
People p1 = new People("Tom");
p1.city = "Beijing";
p1.showInfo();
People p2 = new People("Jack");
p2.city = "Beijing";
p2.showInfo();
People p3 = new People("Mary");
//未声明p3对象的city属性为Beijing,则其city仍为Shanghai。
p3.showInfo();
}
}

使用static关键字指定city后,只需设置city = “Beijing”一次,即可实现三个人城市的更改

 package packageone;
class People{
String name;
static String city = "Shanghai";
public People(String name) {
this.name = name;
}
public void showInfo() {
System.out.println("姓名:"+name+","+"城市:"+city);
}
}
public class StaticDemo { public static void main(String[] args) {
People p1 = new People("Tom");
p1.city = "Beijing";
p1.showInfo();
People p2 = new People("Jack");
p2.showInfo();
People p3 = new People("Mary");
//未声明p3对象的city属性为Beijing,但其city变为Beijing。
p3.showInfo();
}
}

2.使用static关键字声明的属性和方法可直接通过类名来调用(代码作为对该static应用的解释有点复杂了,同时是接口的简单使用)

 package packageone;

 interface USB {
void start();
void stop();
} class C {
public static void work(USB u) {
u.start();
System.out.println("工作中");
u.stop();
}
} class USBDisk implements USB {
@Override
public void start() {
System.out.println("U盘开始工作");
} @Override
public void stop() {
System.out.println("U盘停止工作");
}
} class Printer implements USB {
@Override
public void start() {
System.out.println("打印机开始工作");
} @Override
public void stop() {
System.out.println("打印机停止工作");
}
} public class interfacetest {
public static void main(String[] args) {
//直接通过类名来调用work方法
C.work(new USBDisk());
C.work(new Printer());
} }

3.注意:】使用static方法的时候,只能访问static声明的属性和方法,而非static声明的属性和方法是不能访问的。而非static声明的方法是可以去调用static声明的属性或方法

 package packageone;
//由于博主我水平有限参考了别人的代码案例,但由于他的代码有较大错误,经调试成功后援引,算是自己的代码了吧~嘿嘿
class People {
private String name;
private int age; 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;
} // 使用static定义country属性
private static String country = "China"; // 定义static方法,修改static属性
public static void setCountry(String c) {
country = c;
} // 取得static属性
public static String getCountry() {
return country;
} // 通过构造方法为属性赋值(初始化操作)
public People(String name, int age) {
this.name = name;
this.age = age;
} public void info() {
System.out.println("姓名:"+ getName()+"年龄:"+getAge()+"城市:"+country);
}
} public class StaticDemo {
public static void main(String args[]) {
People per1 = new People("张三", 20);
People per2 = new People("李四", 21);
People per3 = new People("王五", 23);
System.out.println("--------- 修改前-----------");
per1.info();
per2.info();
per3.info();
System.out.println("--------- 修改后-----------");
// 直接使用类名称调用方法来修改static属性的内容,正是因为country为static全局变量,才不需要每个人都去修改国籍
People.setCountry("USA");
per1.info();
per2.info();
per3.info();
}
}
上一篇:PHP static静态属性和静态方法


下一篇:Linux下MySQL5.7.19