我在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>生成Setter/Geter
您需要在另一个类中创建一个类的对象。然后可以对它们调用.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的目的是允许您限制/扩展属性的范围或功能,彼此独立。
您可能希望您的"name"属性在PersonInfo类之外是只读的。在这种情况下,您有一个getter,但没有setter。您可以通过构造函数传入只读属性的值,并通过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属性是私有的,我们无法从外部访问和设置它。因为没有setter,我们也无法更改这个值,使其成为只读值。
作为另一个例子,我们可能希望在修改内部值之前进行一些验证逻辑。这可以在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;
}
}
现在,只有当要设置的值的长度大于10时,我们才能设置"name"的值。这只是一个非常基本的例子,您可能希望在那里进行错误处理,以防有人在您的方法中干扰无效值,并在方法不起作用时抱怨。
您可以对所有想要的值执行相同的过程,并将它们添加到构造函数中,以便在初始设置它们。至于实际使用这种模式,您可以执行以下操作来查看它的作用:
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;
}
个人信息:
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;
}
然后修复打印语句:
System.out.println("Your name is: " + myInfo.getName() + " and you are a " + myInfo.getSex());