从code学习java tiger 之 枚举

Posted on 2006-06-22 18:22 BlueO2 阅读(336) 评论(0)  编辑  收藏 所属分类: JAVA foundation
public   enum  User  {
    Admin,User,Guest,Unknown   
}

public   class  Login  {
    
    
private  User user;
    EnumMap
< User,String >  userName  =   new  EnumMap < User, String > (User. class );
    
/**  Creates a new instance of Login  */
    
public  Login()  {
        userName.put(User.Admin, 
" Administrator " );
        userName.put(User.User, 
" David " );
        userName.put(User.Guest, 
" Steve " );
    }

    
public   boolean  isAdmin(User user) {
        
if (user.equals(User.Admin)) {
            
return   true ;
        }

        
return   false ;
    }

    
public   void  printUserRole() {
        User[] users 
=  user.values();
        
for (User u : user.values()) {
            System.out.println(u.toString());
        }

    }

    
public   void  isRole(User user) {
        
switch (user) {
            
case  Admin:
                System.out.println(
" admin " );
                
break ;
            
case  User:
                System.out.println(
" User " );
                
break ;
            
case  Guest:
                System.out.println(
" Guest " );
                
break ;
            
default :
                System.out.println(
" unknow " );
        }

    }

    
public   static   void  main(String[] args) {
        Login login 
=   new  Login();
        System.out.println(login.isAdmin(User.Admin));
        login.printUserRole();
        login.isRole(User.User);
    }

    
}

只有注册用户登录后才能发表评论。


网站导航:
 

posts - 29, comments - 3, trackbacks - 0, articles - 0

Copyright © BlueO2