Java setter和getter

Java setter和getter,java,setter,getter,Java,Setter,Getter,我在java中使用setter和getter已经很长时间了 例如,如果我想用适当的set和get方法编写一个包含姓名、性别、年龄等信息的类。然后在另一个类中,我想用以下示例测试我的集合和getter: personInfo = myInfo() = new Personinfo("Anna", "female", "17"); 我该怎么做 我知道我可以打印出如下内容: public void printout() { System.out.printf("Your name is:

我在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());
}

您需要在另一个类中创建一个类的对象。然后可以对它们调用
.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());

让eclipse为您处理它

点击你的变量
Source>generatesetter/Getter

通过如下实例化构造函数来创建对象

Personinfo pi = new Personinfo("Anna", "female", "17");
pi.setName("Alan");
然后,您可以按如下方式对该对象调用方法

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

在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;
 }
然后修复打印语句:

System.out.println("Your name is: " + myInfo.getName() + " and you are a " +        myInfo.getSex());
以下是您的操作方法:

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 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());
}
}
这将打印出:

我的名字是:我的名字


我的名字是:OtherName

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());
}


Builder设计模式有用吗?可能重复的可以显示PersonInfo的代码吗?这是一个重复的问题。然而,对于测试getter/设置,您可能不想打印出来。请仔细阅读单元测试。虽然我同意,但这个人似乎不了解该语言的基本知识。获得一个工具来创建他不理解的代码(尤其是在这个级别上)是个坏主意。是的,在开始生成代码之前,我需要从头开始学习,但感谢您的提示。我会记住它。要正确阅读,请参阅“Head first java”第4章,主题Getter和Setteral。因此,我在您的问题中修复了您的print语句语法,以使用println(对初学者来说更容易),并修复了一个连接错误。您必须将姓名和年龄声明为私有,否则我看不到限制Getter有多大用处,setters您需要声明getName和getAge访问修饰符。我认为保护是默认的。公众可能是我们想要的。@SpaceRocker没有真正的需要,但我想我这么做是为了良好的实践。对于这么简单的程序来说,getter和setter通常是在浪费时间,关键是要教他们如何使用getter和setter,在name setter中这样做可以吗?如果(name==anna){this.name=“name take”}或者{this.age=age;}就是这个,我知道!但是现在,在我的另一节课上,我如何向他们传递信息?我知道它可能是这样的:personInfo=myInfo()=newpersoninfo(“安娜”,“女性”,“17”);例如,在上面的类中添加构造函数,这是我不懂的。那么,当我必须用构造函数初始化setter和getter时,为什么还要用它们呢?你不需要,因为你举了一个例子,你希望这样做,所以这是可能的,但你在实践中没有这样做。有时,你有一些必须设置的强制变量,而有些是可选的,因此,您可以组合这样一种方式,将强制构造函数放在构造函数中,而将可选构造函数(有时设置为默认值)放在setters中。默认构造函数是编译器自动生成的构造函数,除非您定义另一个构造函数。所以在这种情况下,将不会生成默认构造函数,因为我显式定义了一个具有3个参数的构造函数。使用this关键字返回有什么区别吗?或者我可以仅仅返回一个示例?而不是回到这个年龄;刚刚回归的年龄;在这种情况下,它只能通过使用returnage来工作,但是如果在方法中使用与实例变量相同的名称定义变量,那么我们需要使用关键字this来区分这两个变量。