static
package com.itheima_01;
//测试类
public class StaticDemo {
public static void main(String[] args) {
//只需要给一次university的值就好了,全部都能用
Student.university = "西安电子科技大学";//提供类名访问,这种情况下,用此方法的多一些
Student s1 = new Student();
//这里可以直接通过对象.的方式赋值,因为Student用的是public修饰的,可以直接访问到
s1.name = "大兴";
s1.age = 23;
//s1.university="西安电子科技大学";通过对象名访问
s1.show();
System.out.println("☆.。.:*・°☆.。.:*・°☆.。.:*・°☆.。.:* ☆.。.:*・°☆.。.:*・°☆.。.:*・°☆.。.:*");
Student s2 = new Student();
s2.age = 23;
s2.name = "吉利";
s2.show();
}
}
package com.itheima_01;
public class Student {
public String name;
/*public String university;
加了static之后,university就是个定值
被所有成员共享的成员,用static修饰*/
public static String university;
public int age;
public void show() {
System.out.println(name + "," + age + "," + university);
}
}
运行结果:
大兴,23,西安电子科技大学
☆.。.:*・°☆.。.:*・°☆.。.:*・°☆.。.:* ☆.。.:*・°☆.。.:*・°☆.。.:*・°☆.。.:*
吉利,23,西安电子科技大学
Process finished with exit code 0
浙公网安备 33010602011771号