zoukankan      html  css  js  c++  java
  • 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();
        }
    }

    运行结果:

  • 相关阅读:
    PowerShell尝试ssh登录
    PowerShell收发TCP消息包
    powershell对指定IP进行端口扫描
    PowerShell尝试登录ftp
    PowerShell批量扫描IP和端口
    《PowerShell 3.0 Advanced Admin handbook》已于今日上市
    PowerShell尝试登录SQL Server
    Docker 数据卷
    Dockerfile自定义镜像
    Docker 容器操作
  • 原文地址:https://www.cnblogs.com/ZZG-GANGAN/p/13724661.html
Copyright © 2011-2022 走看看