1 class Check{ // 完成具体的验证操作
2 public boolean validate(String name,String password){
3 if(name.equals("lixinghua")&&password.equals("mldn")){ // 验证
4 return true ;
5 }else{
6 return false ;
7 }
8 }
9 };
10 class Operate{ // 本类只是调用具体的验证的操作
11 private String info[] ; // 定义一个数组属性,用于接收全部输入参数
12 public Operate(String info[]){
13 this.info = info ; // 通过构造方法取得全部的输入参数
14 }
15 public String login(){
16 Check check = new Check() ; // 实例化Check对象,用于检查信息
17 this.isExit() ; // 判断输入的参数是否正确
18 String str = null ; // 用于返回信息
19 String name = this.info[0] ;// 取出姓名
20 String password = this.info[1] ; // 取出密码
21 if(check.validate(name,password)){ // 登陆验证
22 str = "欢迎" + name + "光临!" ;
23 }else{
24 str = "错误的用户名和密码!" ;
25 }
26 return str ;
27 }
28 public void isExit(){ // 判断参数个数,来决定是否退出程序
29 if(this.info.length!=2){
30 System.out.println("输入的参数不正确,系统退出!") ; // 给出一个正确的格式
31 System.out.println("格式:java LoginDemo02 用户名 密码") ;
32 System.exit(1) ; // 系统退出
33 }
34 }
35 };
36 public class LoginDemo02{
37 public static void main(String args[]){
38 Operate oper = new Operate(args) ; // 实例化操作类的对象
39 System.out.println(oper.login()) ; // 取得验证之后的信息
40 }
41 };
![]()