2015-11-01 30 views
0

面向对象程序的一些优点是可以进行高效快速的编程,但我没有学会如何使用它。到目前为止,我所知道的是如何以脚本样式构建程序的基础知识,我从来不使用方法/对象,所以我的问题是如何将这些代码转换为方法,以便我可以学习如何做到这一点,我GOOGLE了一下,看了视频,但我仍然不明白,我需要真实世界的例子。Java如何将功能代码转换为方法

这里是我的脚本代码风格:“”

String[] studentRoster = 
     { 

      "1,John,Smith,[email protected],20,88,79,59", 
      "2,Suzan,Erickson,[email protected],19,91,72,85", 
      "3,Jack,Napoli,The_lawyer99yahoo.com,19,85,84,87", 
      "4,Erin,Black,[email protected],22,91,98,82", 
      "5,Adan,Ramirez,[email protected],24,100,100,100" 

     }; 

for (int k = 0; k < studentRoster.length; k++) 
    { 
     String s2 = studentRoster[k]; 
     String [] parts2 = s2.split(","); 
     String Email2 = parts2[3]; 

     String invemail2 = Email2; 


     boolean emailfound = false; 
     boolean emailfound2 = false; 
     boolean enotfound; 
     boolean enotfound2; 

     for (int i = 0; i < invemail2.length(); i++) 
     { 
      char emailfind = invemail2.charAt(i); 


      if (emailfind == '@') 
      { 

       emailfound = true; 
      } 


      else 
      { 
       enotfound = false; 
      } 


      if (emailfind == '.') 
      { 
       emailfound2 = true; 
      } 

      else 
      { 
       enotfound2 = false; 
      } 

     } 

     if (emailfound && emailfound2) 
     { 
      System.out.println(invemail2 + " " + "is a valid email"); 
     } 
     else 
     { 
      System.out.println(invemail2 + "is invalid"); 
     } 
    } 

这一切的程序做是无效的电子邮件检查通过查找缺失索引和或'@',但是,如何使用此代码的方法?我如何使用面向对象的编程来使这个更清洁?这是使用一个类,我如何使用2个类来实现相同的事情?谢谢。

回答

1

下面就以这个代码转换成OO并开始OO思维方式:

1)什么是“实体”我的工作 - 电子邮件地址,所以也许我需要一个Email

public class Email 
{ 
{ 

2)我可以建立从字符串的电子邮件,但首先需要验证 - 我的类需要有一个构造函数字符串,并将其解析为一个Email情况下,将有一个isValid()方法

公共CL ass Email { String address = null;

public Email(String input) 
{ 
    // parse input into address 
} 

public boolean isValid() 
{ 
    return address != null; 
} 

public static void main(String[] args) 
{ 
    String[] studentRoster = { 
      "1,John,Smith,[email protected],20,88,79,59", 
      "2,Suzan,Erickson,[email protected],19,91,72,85", 
      "3,Jack,Napoli,The_lawyer99yahoo.com,19,85,84,87", 
      "4,Erin,Black,[email protected],22,91,98,82", 
      "5,Adan,Ramirez,[email protected],24,100,100,100" 
    }; 

    for (int k = 0; k < studentRoster.length; k++) { 
     Email email = new Email(studentRoster[k]); 
     if (email.isValid()) { 
      System.out.println(email.address + " is a valid email"); 
     } else { 
      System.out.println(email.address + " is invalid email"); 
     } 
    } 
} 

}

3)或者,我可以有一个静态containsEmail()方法,需要一个字符串,返回真/假

0

您可以创建一个名为“示例”级和内部定义的方法它以String Array作为参数。你可以这样编码 -

public class Example { 

public void display(String[] data){ 

    for(int i=0;i<data.length;i++){ 
     boolean flag=false; 
     String[] parts=data[i].split(","); 
     String email=parts[3]; 
     int index1=email.indexOf("@"); 
     int index2=email.lastIndexOf("."); 
     if(index1!=-1 && index2>index1){ 
      flag=true; 
     } 

     if(flag==true){ 
      System.out.println(email+ " is a valid email."); 
     } 
     else{ 
      System.out.println(email+ " is an invalid email."); 
     } 
    } 
} 
} 

现在创建另一个名为“Start”的类,它包含main方法。在它里面你可以声明你的String数组,创建一个例子类的对象并通过传递String数组来调用它的显示方法。做这样的事情 -

public class Start { 

public static void main(String[] args) { 

    String[] studentRoster = 
     { 

      "1,John,Smith,[email protected],20,88,79,59", 
      "2,Suzan,Erickson,[email protected],19,91,72,85", 
      "3,Jack,Napoli,The_lawyer99yahoo.com,19,85,84,87", 
      "4,Erin,Black,[email protected],22,91,98,82", 
      "5,Adan,Ramirez,[email protected],24,100,100,100" 

     }; 

    Example e=new Example(); 
    e.display(studentRoster); 

} 

}