java mutator,Java – 使用Accessor和Mutator方法「建议收藏」

java mutator,Java – 使用Accessor和Mutator方法「建议收藏」Iamworkingonahomeworkassignment.Iamconfusedonhowitshouldbedone.Thequestionis:CreateaclasscalledIDCardthatcontainsaperson’sname,IDnumber,andthenameofafilecontainingt…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

java mutator,Java - 使用Accessor和Mutator方法「建议收藏」

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

The question is:

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)

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;

}

}

解决方案

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;

}

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

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;

}

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;

}

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

Good luck.

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/194258.html原文链接:https://javaforall.net

赞 (0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 谷歌打开微信定位服务器地址,使用Chrome修改user agent模拟微信内置浏览器

    谷歌打开微信定位服务器地址,使用Chrome修改user agent模拟微信内置浏览器很多时候,我们需要模拟微信内置浏览器,今天教大家用chrome简单模拟。如图设置:F12或者右键审查元素进入开发者模式,点击Emulation,然后点击Network,把Spoofuseragent改成Other,并把下面的带复制进去,有三句,大家自己尝试。回车然后刷新页面即可。Mozilla/5.0(iPhone;CPUiPhoneOS5_1likeMacOSX)Appl…

    2022年6月1日
  • 前端程序员简历制作建议「建议收藏」

    前端程序员简历制作建议「建议收藏」大家好,我是漫步,今天来分享一下前端程序员简历该怎么写昨天的抽奖活动还在进行中,去看看吧排版工整好的简历,万里挑一,HR每天要看上百份简历,一份排版工整、段落错落有致、文字简洁分明的简历…

    2022年10月6日
  • quartus ii 9.0安装激活成功教程教程_quartusii激活成功教程教程

    quartus ii 9.0安装激活成功教程教程_quartusii激活成功教程教程其实很多时候我们用web版就够了,不用激活成功教程,不要license,很方便, web版链接:https://pan.baidu.com/s/1OSvnko0b_TEEZvQ7EeQB6A 密码:g920 点击QuartusSetupWeb-13.1.0.162.exe进行安装,安装完成以后对modelsim进行配置,将vsim.exe的路径添加到tools->options->EDATo…

    2022年10月15日
  • Node.js的安装及环境配置【超详细】

    Node.js的安装及环境配置【超详细】Step1:去官网下载安装包下载方式1:官网下载根据自己电脑系统及位数选择。注意:图中.msi和.zip格式区别:1、.msi是Windowsinstaller的数据包,实际上是一个数据库,提供安装,修改,卸载你所安装的程序。此外:它还包含有关安装过程本身的信息。例如:安装序列、目标文件夹路径、安装选项和控制安装过程的属性。2、.zip是一个压缩包,解压之后即可,不需要安装。…

    2022年6月11日
  • apk加壳加密工具(apk protect) v1.0下载「建议收藏」

    apk加壳加密工具(apk protect) v1.0下载「建议收藏」apk加壳加密工具(apk_protect)是用于加密apk文件中dex文件的加密工具,加密的东西主要有字符串加密、流程加密、类名加密和api加密(未完成,后续支持)等,有于较好的保护apk文件,使之不易激活成功教程分析。__我对apk_protect在线加密的有效性进行了测试和分析,发现确实给android_apk提供了无法激活成功教程的加密壳。虽然在线加密已经是非常省时省力的了,但是仍然有不少程序员懒于折腾(尽管这已经不叫折腾了,就是上传一下再下载,比起写代码来说,这简直就是享受)。于是,意外的发现他们已经推出了懒人版

    2022年6月27日
  • C++通过TinyXML类库读写XML文件

    C++通过TinyXML类库读写XML文件TinyXML是一个开源的解析XML的解析库,能够用于C++,能够在Windows或Linux中编译。这个解析库的模型通过解析XML文件,然后在内存中生成DOM模型,从而让我们很方便的遍历这棵XML树。DOM模型即文档对象模型,是将整个文档分成多个元素(如书、章、节、段等),并利用树型结构表示这些元素之间的顺序关系以及嵌套包含关系。

    2022年4月30日

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号