Java setter和getters

很长一段时间以来,我一直在努力解决java中的setter和getter问题。

例如,如果我想用适当的set和get方法编写一个包含名称,性别,年龄等信息的类。 然后在另一个类中,我想以此为例测试我的集合和getter:

personInfo = myInfo() = new Personinfo("Anna", "female", "17"); 

我怎么做?

我知道我可以打印出像:

 public void printout() { System.out.printf("Your name is: " + getName() + " and you are a " + getSex()); } 

这是一个简单的示例,向您展示如何执行此操作:

 public class Person { private String name; private String gender; private int age; Person(String name, String gender, int age){ this.name = name; this.gender = gender; this.age = age; } public void setName(String name){ this.name = name; } public void setGender(String gender){ this.gender = gender; } public void setAge(int age){ this.age = age; } public String getName(){ return this.name; } public String getGender(){ return this.gender; } public int getAge(){ return this.age; } public static void main(String[] args) { Person me = new Person("MyName","male",20); System.out.println("My name is:" + me.getName()); me.setName("OtherName"); System.out.println("My name is:" + me.getName()); } } 

这将打印出来:

我的名字是:MyName

我的名字是:OtherName

让eclipse处理它为你

单击变量Source> Generate Setter / Getter

您需要在另一个类中创建一个类的对象。 然后,您可以在它们上调用.get().set()方法。 我将在2分钟内发布一个例子

第一类(我将其称为Person)将具有返回其字段的方法

 private String name = ""; private String age = 0; public Person(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public int getAge() { return age; } 

第二个类将在创建第一个类的对象后调用这些方法

 bob = new Person("Bob", 21); System.out.println("Your name is: " + bob.getName() + " and you are " + bob.getAge()); 

getter和setter的目的是让你限制/扩展你的财产的范围或function,彼此独立。


您可能希望您的’name’属性在PersonInfo类之外只读。 在这种情况下,你有一个getter,但没有setter。 您可以通过构造函数传入readonly属性的值,并通过getter检索该值:

 public class PersonInfo { //Your constructor - this can take the initial values for your properties public PersonInfo(String Name) { this.name = Name; } //Your base property that the getters and setters use to private String name; //The getter - it's just a regular method that returns the private property public String getName() { return name; } } 

我们可以使用getName()来获取此类实例之外的’name’的值,但由于name属性是private,因此我们无法从外部访问和设置它。 而且因为没有setter,所以我们也无法改变这个值,使其成为readonly。


另一个例子,我们可能想在修改内部值之前做一些validation逻辑。 这可以在setter中完成,另一种方式是getter和setter可以派上用场:

 public class PersonInfo { public PersonInfo(String Name) { this.setName(Name); } //Your setter public void setName(String newValue) { if (newValue.length() > 10) { this.name = newValue; } } 

现在我们只能设置’name’的值,如果我们想要设置的值的长度大于10.这只是一个非常基本的例子,你可能想要在那里进行error handling,以防有人干扰无效值在你的方法和抱怨它不起作用。


您可以对所需的所有值执行相同的过程,并将它们添加到构造函数中,以便最初设置它们。 至于实际使用此模式,您可以执行以下操作以查看其中的操作:

 public static void main(String[] args) { PersonInfo myInfo = new PersonInfo("Slippery Sid",97,"male-ish"); var name = myInfo.getName(); System.out.printf("Your name is: " myInfo.getName() + " and you are a " myInfo.getSex()); myInfo.setName("Andy Schmuck"); System.out.printf("Your name is: " myInfo.getName() + " and you are a " myInfo.getSex()); } 

您可以通过实例化构造函数来创建对象,如下所示

 Personinfo pi = new Personinfo("Anna", "female", "17"); 

然后,您可以按如下方式调用该对象上的方法

 pi.setName("Alan"); 

要么

 pi.getName(); 

这是你如何做到的:

 public class PersonInfo { private String name; private String sex; private int age; /** GETTERS **/ public String getName(){ return name; } public String getSex(){ return sex; } public int getAge(){ return age; } /** SETTERS **/ public void setName(String name){ this.name = name; } public void setSex(String sex){ this.sex = sex; } public void setAge(int age){ this.age = age; } } class Test{ public static void main(String[] args){ PersonInfo pinfo = new PersonInfo(); pinfo.setName("Johny"); pinfo.setSex("male"); pinfo.setAge(23); //now print it System.out.println("Name: " + pinfo.getName()); System.out.println("Sex: " + pinfo.getSex()); System.out.println("Age: " + pinfo.getAge()); } } 

或者你也可以添加它:

 @Override public String toString(){ return "Name: " + this.name + "\n" + "Sex: " + this.sex + "\n" + "Age: " + this.age; } 

然后只是一个.toString

编辑:

在类中添加此构造函数以初始化对象:

 public PersonInfo(String name, String sex, int age){ this.name = name; this.sex = sex; this.age = age; } 

在personInfo:

 public Person(String n, int a, String s){ this.name=n; this.age=a; this.sex=s; } public String getName(){ return this.name; } public int getAge(){ return this.age; } public String getSex(){ return this.sex; } public void setName(String n){ this.name = n; } public void setAge(int a){ this.age = a; } public void setSex(String s){ this.sex = s; } 

然后修复print语句:

 System.out.println("Your name is: " + myInfo.getName() + " and you are a " + myInfo.getSex());