Java - 使用Accessor和Mutator方法 [英] Java - Using Accessor and Mutator methods

查看:786
本文介绍了Java - 使用Accessor和Mutator方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做家庭作业。我很困惑应该怎么做。

I am working on a homework assignment. I am confused on how it should be done.

问题是:


创建一个名为IDCard的类,其中包含一个人的姓名,ID号,
以及包含该人的photogrpah的文件的名称。为每个字段编写
访问器和mutator方法。将两个重载构造函数后面的
添加到类中:

Create a class called IDCard that contains a person's name, ID number, and the name of a file containing the person's photogrpah. Write accessor and mutator methods for each of these fields. Add the following two overloaded constructors to the class:

public IDCard()public IDCard(String n,int ID,String filename)

public IDCard() public IDCard(String n, int ID, String filename)

使用这两个
构造函数创建不同的ojbect并使用
访问器和mutator方法在控制台上打印它们的值来测试程序。

Test your program by creating different ojbects using these two constructors and printing out their values on the console using the accessor and mutator methods.

到目前为止我已经重写了这个:

I have re-written this so far:

public class IDCard {
String Name, FileName;
int ID;

public static void main(String[] args) {

}

public IDCard()
{
    this.Name = getName();
    this.FileName = getFileName();
    this.ID = getID();
}

public IDCard(String n, int ID, String filename)
{

}

public String getName()
{
    return "Jack Smith";
}

public String getFileName()
{
    return "Jack.jpg";
}

public int getID()
{

        return 555;
    }
}


推荐答案

让我们回顾一下基础知识:
Accessor和Mutator只是一个吸引人和一个二传手的奇特名字。
getter,Accessor,返回一个类的变量或其值。一个setter,Mutator,设置一个类变量指针或它的值。

Let's go over the basics: "Accessor" and "Mutator" are just fancy names fot a getter and a setter. A getter, "Accessor", returns a class's variable or its value. A setter, "Mutator", sets a class variable pointer or its value.

首先你需要设置一个带有一些变量来获取/设置的类:

So first you need to set up a class with some variables to get/set:

public class IDCard
{
    private String mName;
    private String mFileName;
    private int mID;

}

但是哦不!如果实例化此类,则这些变量的默认值将毫无意义。
B.T.W. instantiate是一个奇特的词:

But oh no! If you instantiate this class the default values for these variables will be meaningless. B.T.W. "instantiate" is a fancy word for doing:

IDCard test = new IDCard();

所以 - 让我们设置一个默认构造函数,这是你实例化时调用的方法class。

So - let's set up a default constructor, this is the method being called when you "instantiate" a class.

public IDCard()
{
    mName = "";
    mFileName = "";
    mID = -1;
}

但是如果我们知道我们想要给变量的值呢?所以让我们创建另一个构造函数,一个带参数的构造函数:

But what if we do know the values we wanna give our variables? So let's make another constructor, one that takes parameters:

public IDCard(String name, int ID, String filename)
{
    mName = name;
    mID = ID;
    mFileName = filename;
}

哇 - 这很好。但是很愚蠢。因为我们无法访问(=读取)变量的值。所以让我们添加一个getter,当我们在它时,也添加一个setter:

Wow - this is nice. But stupid. Because we have no way of accessing (=reading) the values of our variables. So let's add a getter, and while we're at it, add a setter as well:

public String getName()
{
    return mName;
}

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

很好。现在我们可以访问 mName 。添加其余的访问者和mutator,你现在是一个经过认证的Java新手。
祝你好运。

Nice. Now we can access mName. Add the rest of the accessors and mutators and you're now a certified Java newbie. Good luck.

这篇关于Java - 使用Accessor和Mutator方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆