public class Person {
String name;
int year;
int month;
int day;
String email;
String phonenr;
public Person(String name, int year, int month, int day, String email, String phonenr) {
this.name = name;
this.year = year;
this.month = month;
this.day = day;
this.email = email;
this.phonenr = phonenr;
}
我有这个对象。我只想在另一个类中访问电子邮件属性,因此我可以使用test目录中的assert检查电子邮件是否有效。如何只访问来自另一个班级的Person的电子邮件属性,以便以后使用它来验证电子邮件?
这是我要从中访问电子邮件属性的类。
public class PersonValidator {
public static boolean email(String email){
Person onePerson = new Person();
return false;
}
}
这是测试类,用于测试电子邮件是否有效:
class PersonRegisterTest {
@Test
void checkValidEmail() {
assertTrue(PersonValidator.email("[email protected]"));
assertTrue(PersonValidator.email("[email protected]"));
assertTrue(PersonValidator.email("[email protected]"));
}
最好将Person类中的数据成员/变量保留为私有,并且可以添加getters和setters方法,可以从类外部访问它们,并可以使用该方法访问类的数据成员/变量。