java中的单态(单例)模式

package org.lcq.util;

 

import java.util.Properties;

 

public class IntegralGrade {

 

 private Properties pro = null;


 private static final IntegralGrade  ig = new IntegralGrade();    / /首先这里要声明静态的常量


 // key-->value
 private   IntegralGrade() {//单态设计             //把构造方法声明为private私有的
  this.pro = new Properties(); 
  this.pro.setProperty("1", "开发爱好者");
  this.pro.setProperty("2", "初级程序员");
  this.pro.setProperty("3", "软件工程师");
  this.pro.setProperty("4", "高级软件工程师");
  this.pro.setProperty("5", "项目主管");
  this.pro.setProperty("6", "项目经理");
  this.pro.setProperty("7", "项目总监");
  this.pro.setProperty("8", "首席技术执行官");
 }

 

 public static IntegralGrade getInstance(){      //声明一个静态的方法进行调用
  return ig;
 }
 
 // 确认等级
 public int getGrade(int ita) {
  int grade = 0;
  if (ita >= 0 && ita <= 300) {
   grade = 1;
  }
  if (ita >= 301 && ita <= 1000) {
   grade = 2;
  }
  if (ita >= 1001 && ita <= 5000) {
   grade = 3;
  }
  if (ita >= 5001 && ita <= 10000) {
   grade = 4;
  }
  if (ita >= 10001 && ita <= 20000) {
   grade = 5;
  }
  if (ita >= 200001 && ita <= 50000) {
   grade = 6;
  }
  if (ita >= 50001 && ita <= 100000) {
   grade = 7;
  }
  if (ita >= 100001 && ita <= 500000) {
   grade = 8;
  }
  return grade;
 }

 

 // 取得等级信息
 public String getGradeInfo(int grade) {
  String str = "";
  str = this.pro.getProperty(grade + "");
  return str == null ? "" : str;
 }

 

 public static void main(String[] args) {
  IntegralGrade ita = new IntegralGrade();
  System.out.println(ita.getGradeInfo(ita.getGrade(50002)));
 }

}

 

 

---------------------------------------------------------

调用的方法如下:

   IntegralGrade.getInstance().getGradeInfo(3);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值