类和访问器方法 Java

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19435862/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 17:08:13  来源:igfitidea点击:

Class and accessor methods Java

javaclassmethodsaccessor

提问by

I don't understand accessor methodsand I'm stuck with creating setAge, getAge and getName.

我不理解访问器方法,我一直在创建 setAge、getAge 和 getName。

This is the question:

这是问题:

Add three accessor methods, setAge, getAgeand getName. These methods should set and get the values of the corresponding instance variables.

添加三个访问器方法setAgegetAgegetName。这些方法应该设置和获取相应实例变量的值。

public class Player {

    protected int age;
    protected String name;

    public Player(String namArg) {
        namArg = name;
        age = 15;
    }
}

采纳答案by Tim Knox

An accessor method is used to return the value of a private or protected field. It follows a naming scheme prefixing the word "get" to the start of the method name. For example let's add accessor methods for name:

访问器方法用于返回私有或受保护字段的值。它遵循命名方案,在方法名称的开头加上“get”一词。例如,让我们为 name 添加访问器方法:

class Player{
   protected name

//Accessor for name
   public String getName()
   {
     return this.name;
   }
}

you can access the value of protected name through the object such as:

您可以通过对象访问 protected name 的值,例如:

Player ball = new Player()
System.out.println(ball.getName())

A mutator method is used to set a value of a private field. It follows a naming scheme prefixing the word "set" to the start of the method name. For example, let's add mutator fields for name:

mutator 方法用于设置私有字段的值。它遵循命名方案,在方法名称的开头加上“set”一词。例如,让我们为 name 添加 mutator 字段:

//Mutator for name
   public void setName(String name)
   {
     this.name= name;
   }

now we can set the players name using: ball.setName('David');

现在我们可以使用以下方法设置球员姓名: ball.setName('David');

回答by ozzyzig

setAbcd methods are mutator methods which you use to set your protected data fields. getAbcd methods are accessor methods which you use to return values of the data fields, as they are usually private and not available outside the class.

setAbcd 方法是用于设置受保护数据字段的 mutator 方法。getAbcd 方法是用于返回数据字段值的访问器方法,因为它们通常是私有的并且在类之外不可用。

e.g

例如

public void getvariableName() {
    return variableName;
   }

回答by Shaban Mousa

public class Player {

protected int age;
protected String name;

// mutator methods
public void setAge(String a) { age = a; }

// mutator 方法
public void setAge(String a) { age = a; }

public void setName(String n) {
    name = n;
     }

// accessor method
public string getAge() { return age ; }

// 访问器方法
public string getAge() { return age ; }

public string getName() {
    return name;
     }


}

回答by Whoppa

Your instance variables are age and name. Your setter methods are void and set your passed arguments to the corresponding variable. Your getters are not void and return the appropriate variables.

您的实例变量是年龄和名称。您的 setter 方法是无效的,并将您传递的参数设置为相应的变量。你的 getter 不是 void 并返回适当的变量。

Try this and come back with questions.

试试这个,然后带着问题回来。

回答by Stella

Answer for: I don't understand accessor methods here is the thing:

回答:我不明白这里的访问器方法是这样的:

why do we need accessor methods? Encapsulation !!! Why do we need encapsulation?

为什么我们需要访问器方法?封装!!!为什么需要封装?

1) Imagine you (programmer#1) gonna write those setAge, getAge and getName methods. I am programmer#2. I most probably cant access age and name directly. So I have yo use your public accessor methods setAge, getAge and getName. This is to save your code and variables from mess. Cuz u cant trust all coders.

1) 想象一下你(程序员#1)要编写那些 setAge、getAge 和 getName 方法。我是程序员#2。我很可能无法直接访问年龄和姓名。所以我让你使用你的公共访问器方法 setAge、getAge 和 getName。这是为了避免混乱的代码和变量。因为你不能相信所有的程序员。

2) In setAge method u can provide VALIDATION

2) 在 setAge 方法中,您可以提供 VALIDATION

random evil programmer: ya I wanna make age equal to 234 so ur program results gonna crush hahaha

随机邪恶程序员:是的,我想让年龄等于 234,所以你的程序结果会崩溃哈哈哈

u: nah I gonna add validation condition into my setAge method so u can only make age equal from 0 to 90(whatever u want)

你:不,我要在我的 setAge 方法中添加验证条件,所以你只能让年龄等于 0 到 90(无论你想要什么)

This is the most popular reason why we use accessor methods.

这是我们使用访问器方法的最流行的原因。

Code explanation:

代码说明:

setAge explanation( this is just to get main idea)

setAge 解释(这只是为了获得主要思想)

public void setAge(int ageInput) { 
if ((ageInput > 10) && (ageInput <90))
 {age = a;}}

Random evil programmer sends ageInput into your public method. First of all, it checks if age value is correct. If yes, age instance(object) variable will become ageInput. If no, nothing will happen and your variables wont be messed up.

随机的邪恶程序员将 ageInput 发送到您的公共方法中。首先,它检查年龄值是否正确。如果是,则 age instance(object) 变量将变为 ageInput。如果没有,什么都不会发生,你的变量也不会被搞砸。

GetAge: it just returns current age value. Nothing fancy.

GetAge:它只返回当前的年龄值。没有什么花哨。

let me know if you have more questions ;) Best of luck to you.

如果您有更多问题,请告诉我;) 祝您好运。