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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • SqlServer中Exists的使用

    SqlServer中Exists的使用1、简介不相关子查询:子查询的查询条件不依赖于父查询的称为不相关子查询 相关子查询:子查询的查询条件依赖于外层父查询的某个属性值的称为相关子查询。带Exists的子查询就是相关子查询 Exists表示存在量词:带有Exists的子查询不返回任何记录的数据,只返回逻辑值“True”或“False”2、表结构选课表:学号StudentNo、课程号CourseNo学生表:学号Stude…

    2022年7月14日
    61
  • Java 序列化

    Java 序列化

    2021年10月7日
    45
  • 很黄色的JAVA游戏_《yellow》很黄!很暴力?居然有这么黄的游戏!

    很黄色的JAVA游戏_《yellow》很黄!很暴力?居然有这么黄的游戏!原标题:《yellow》很黄!很暴力?居然有这么黄的游戏!《yellow》是一款很“黄”但是一点也不暴力的解密益智类游戏,游戏一共有50关,每一关都非常非常的黄,不适宜长时间玩耍~游戏虽好玩但是要适度哦~游戏的特色就是黄,一打开游戏游戏的界面就和游戏的名字一样,玩家要通过自己的脑洞来进行通关,通关的方法也和游戏的名字一样,只要让游戏界面全部变黄了就能通关了,游戏的解密相对其他解密游戏简直亲切,…

    2022年10月15日
    2
  • mktime()函数使用「建议收藏」

    mktime()函数使用「建议收藏」原型:time_tmktime(structtm*)其中的tm结构体定义如下:structtm{inttm_sec;/*秒–取值区间为[0,59]*/inttm_min

    2022年8月6日
    8
  • JavaScript两种数组去重方法[通俗易懂]

    JavaScript两种数组去重方法[通俗易懂]JavaScript两种数组去重方法第一种(利用splice方法去重)splice()方法用于插入、删除或替换数组的元素。//申明一个数组vararr=[1,3,5,4,3,1,5,7,8,4,7];functiontemp(arr){for(i=0;i<arr.length;i++){for(j…

    2022年6月24日
    25
  • 玩守望先锋显示渲染器丢失_win10守望先锋渲染内存不足

    玩守望先锋显示渲染器丢失_win10守望先锋渲染内存不足解决游玩守望先锋时出现"渲染设备已丢失"笔者配置:cpu:amd3600显卡:rtx2060出现问题:在打守望先锋的时候出现"渲染设备丢失。。。。&quot

    2022年8月5日
    5

发表回复

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

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