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)
上一篇 2025年11月9日 下午1:22
下一篇 2025年11月9日 下午2:01


相关推荐

  • synchronized

    synchronized

    2021年5月5日
    209
  • linux内核编程_linux内核是什么

    linux内核编程_linux内核是什么什么是操作系统?指在系统中负责完成最基本功能和系统管理的部分,操作系统有哪些组成部分?内核——操作系统的内在核心 设备驱动程序 启动引导程序 命令行shell 其他种类的用户界面—-操作系统的外在表象 基本的文件管理工具和系统工具Linux内核的组成Linux内核源代码目录结构是什么,各目录有什么含义?arch:包含和硬件体系结构相关的代码,每种平台占一…

    2022年10月8日
    6
  • vue实现文件上传和下载_vue上传文件前端完整实例

    vue实现文件上传和下载_vue上传文件前端完整实例文件上传这里使用elementui组件库的文件上传组件1.手动上传(文件选取后需点击确认上传)action:上传地址auto-upload:是否在选取文件后立即进行上传,默认true手动上传要将其设置为falsebefore-upload:上传文件之前的钩子,参数为上传的文件,上传格式的规定要求可在此钩子函数中写(示例中规定上传格式xlsx或xls)on-success:文件上传成功时的钩子,function(response,file,fileList)ref:注册DOM对象(点

    2022年8月16日
    14
  • Oracle 创建用户详解(create user)

    Oracle 创建用户详解(create user)文章目录1概述2语法2.1创建3.2查询3扩展3.1表空间1概述#mermaid-svg-3X6xRk3SgBGokR8x.label{font-family:’trebuchetms’,verdana,arial;font-family:var(–mermaid-font-family);fill:#333;color:#333}#mermaid-svg-3X6xRk3SgBGokR8x.labeltext{fill:#333}#mermaid-svg-3X6xRk3SgB

    2022年5月18日
    75
  • 北美CS四大名校(美国前四大城市)

    1.北美CS方面三个梯队总体上讲Top20的计算机方向可以分成三个梯队:一、4个最为优秀的computerscienceProgram是麻省理工大学MIT,斯坦福大学Stanford,加州伯克莱分校UC.Berkeley和卡奈基梅隆CMU。这四家基本没什么争议,得到大家的广泛认可。二、6个其他前十的computerscience:UIUC,康乃尔大学Cornell,华盛顿大学U.o

    2022年4月12日
    113
  • 基于单片机的电集中抄表设计

    基于单片机的电集中抄表设计文末下载完整资料 1 集中抄表系统的发展及概况 1 1 电集中抄表系统 自动抄表 AutomaticMet AMR 是指采用通讯和计算机网络等技术自动读取和处理表计数据 发展电能自动抄表技术是提高用电管理水平的需要 也是网络和计算机技术迅速发展的必然 在用电管理方面 采用自动抄表技术 不仅能节约人力资源 更重要的是可提高抄表的准确性 减少因估计或誊写而造成帐单出错 使供用电管理部门能及时准确获得数据信息 由于电力用户因此不再需要与抄表者预约上门抄表时间 还能迅速查询帐单 故这

    2026年3月26日
    2

发表回复

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

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