java类的一个简单例子

以教师类为例

 

1. 先定义一个教师类:

package Lei_Learning;

public class Teacher_Lei {
    private final int baseSalary= 1000;
    // 属性-成员变量
    String name;
    int teaching_age;
    //int teaching_class;
    String teaching_level;
    
    
    // 构造方法(空构造)
    Teacher_Lei(){
        
    }
    
    // 成员方法
    // 打印上述成员变量,调用getSalary方法
    
    public void show() {
        int salary = getSalary(baseSalary,teaching_age,teaching_level);
        System.out.println("The salary of " + name + " is :" + salary);
    }
    
    // 提供一个获取工资的方法
    // 在这个方法内调用成员变量计算工资,给工资变量赋值
    private int getSalary(int baseSalary, int teaching_age, String teaching_level) {
        switch(teaching_level) {
        case "讲师": return baseSalary*2+teaching_age*100;
        case "副教授": return baseSalary*3+teaching_age*100;
        case "教授": return baseSalary*4+teaching_age*100;
        default: return 0;
        }
    }
    
    

}

 

2. 创建一个教师Demo调用教师类中的方法:

package Lei_Learning;

public class Teach_Demo {
    public static void main(String[] agrs) {
        Teacher_Lei teach = new Teacher_Lei();
        teach.name = "zzg";
        teach.teaching_level = "教授";
        teach.teaching_age = 5;
        teach.show();
    }
}

 

运行结果:

 

posted @ 2020-09-24 16:17  ZengZG  Views(423)  Comments(0Edit  收藏  举报