需求:写一个javaBean类来描述这个班级的学生和老师
属性:姓名,年龄,性别
行为:学习
package mm.java.Static;
public class Static01 {
//属性:姓名 年龄 性别
//新增:老师的姓名
private String name;
private int age;
private String gender;
public static String teacherName;
public Static01() {
}
public Static01(String name, int age, String gender) {
this.name = name;
this.age = age;
this.gender = gender;
}
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;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
//行为
public void study(){
System.out.println(name + "正在学习");
}
public void show(){
System.out.println(name + "," + age + "," + gender + teacherName);
}
}
package mm.java.Static;
public class Test01 {
public static void main(String[] args) {
//1.创建第一个学生对象
Static01 s1 = new Static01();
s1.setName("张三");
s1.setAge(23);
s1.setGender("男");
s1.teacherName = "林挺老师";
s1.study();
s1.show();
//2.创建第二个学生对象
Static01 s2 = new Static01();
s2.setName("李四");
s2.setAge(24);
s2.setGender("女");
s2.study();
s2.show();
}
}
static表示静态,是Java中的一个修饰符,可以修饰成员方法,成员变量
1.被static修饰的成员变量,叫作静态变量
特点:
01被该类所有的对象共享
02不属于对象,属于类
03随着类的加载而加载,优于对象存在
调用方式:
01类名的调用
02对象名调用