简体   繁体   中英

How to call a class from another class

java, B.java, C.java in all the three class I do the same validation for a particular string. Is it possible to write a single class and call it as a method in the class wherever its required, below is the validation for the mobile number which i use in the above three classes respectively.

A.java

mobile = (EditText) findViewById(R.id.app_mobile);

String ap_Mobile_No = mobile.getText().toString();

if (ap_Mobile_No.equals(null) || (ap_Mobile_No.length() != 10) || ap_Mobile_No.startsWith("0")){
    mobile.setError("E");

    result = false;
}

B.java

EditText mobile = (EditText) findViewById(R.id.gua_mobile);

String gu_Mobile_No = mobile.getText().toString();
if (gu_Mobile_No.equals("") || gu_Mobile_No.length() != 10 || gu_Mobile_No.startsWith("0")) {

    mobile.setError(ssbuilder);
    result = false;
}

You can create Class for this validation and call this class using Method;

public class Validation
{

   public static boolean checkValidation(arguments)
   {
       check your validation here.

        return true or false;
   }

}

Yes you can. Create a class lets say Validator.java create static method validate which returns boolean on the basis of success and failure. and call function any where as

 Validatorn.validate(<arguments>)

so your class become

public final class Validator {

   public static boolean validate(final EditText mobile) {
       boolean isSuccess = true;
       String gu_Mobile_No= mobile.getText().toString();
       if (gu_Mobile_No.equals("") || gu_Mobile_No.length() != 10 || gu_Mobile_No.startsWith("0")) {
          mobile.setError(ssbuilder);
          isSuccess = false;
       }
       return isSuccess;
   }
}

Now class A.java become

mobile = (EditText) findViewById(R.id.app_mobile);
if(Validator.validate(mobile)) {
    return false;
}

and class B.java become

EditText mobile = (EditText) findViewById(R.id.gua_mobile);
if(Validator.validate(mobile)) {
    return false;
}

Yes you can write a single class for the validation purpose. Add the validator methods as static methods and call it from other classes.

- You can try to create a class with static methods and static variables and then call it in which ever class you want.

- Or alternatively you can use Singleton Pattern .

Yes, a simple solution is to create a static method in a MobileValidator class. Static methods are just one way of doing it...

public class MobileValidator {


public static boolean isValid(EditText mobile) {

    String gu_Mobile_No = mobile.getText().toString();
    if (gu_Mobile_No.equals("") || gu_Mobile_No.length() != 10 || gu_Mobile_No.startsWith("0")) {
       return false;
    }
    return true;
}

}

Then use this in your A/B classes

eg

boolean valid = MobileValidator.isValid(mobile);
if(!valid) {
  // do something
}

Create a Utility class and implement the method for mobile number validation and invoke it wherever you want. Please ensure that you mark the method as static. this will avoid creation of new instances every time you validate the mobile number

how about , create a class like below

public class Validate{
  public static boolean ValidateString(String validateString){
    return (validateString.equals("") || validateString.length() != 10 || validateString.startsWith("0"));
  }
} 

then , in your code ,you can

A.java

 mobile = (EditText) findViewById(R.id.app_mobile);

    String ap_Mobile_No = mobile.getText().toString();

    if (Validate.ValidateString(ap_Mobile_No)==false)
     {
       mobile.setError("E");
       result = false;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM