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


相关推荐

  • ffmpeg参数详解_ffmpeg个版本

    ffmpeg参数详解_ffmpeg个版本-c:v指定编码器默认值:mpeg4mpeg4编码器,编码速度快,清晰度不够,处理后的文件比较大libx264编码器,编码比较慢,清晰度高,处理后的文件比较小-preset编码速度默认值:medium当编码器指定为libx264时可以指定该参数,编码速度越慢,清晰度越高,处理后的文件大小相同可选值:ultrafast、superfast、veryfast、

    2022年9月19日
    4
  • 数据库-库表设计 【分享一些库表设计经验】

    数据库-库表设计 【分享一些库表设计经验】本文的核心内容:记录积累一些库表设计方案与技巧数据库实体与实体间的对应关系1)数据库表的菜单【分类】设计:如省市关联、图书的一、二级分类。2)数据库表设计之树形结构的表3)表的简化方案(特定情况,例如,用户触发过的场景记录表)4)数据库表设计之购物车,利用Session暂时存储购物车信息。

    2022年6月20日
    35
  • excellvba引用计算机用户名,EXCEL VBA 取当前登录 用户名的多种实现方法[通俗易懂]

    excellvba引用计算机用户名,EXCEL VBA 取当前登录 用户名的多种实现方法[通俗易懂]EXCELVBA取当前登录用户名的多种实现方法1.使用WscriptDimwshAsObjectSetwsh=CreateObject(“WScript.Network”)Sheet1.Range(“a1”)=wsh.UserName2.使用环境变量dimiuseriuser=Environ(“username”)3.VBA获取excel文件当前用户名的代码(与…

    2022年10月14日
    2
  • SpringMvc工作流程图讲解

    SpringMvc工作流程图讲解SpringMvc工作流程图讲解这是博主根据很多书籍自己总结的一个过程,可能会有错误,敬请指出,共同学习

    2022年5月2日
    40
  • hdu4288 Coder(段树+分离)

    hdu4288 Coder(段树+分离)

    2022年1月2日
    51
  • Java多线程死锁问题

    Java多线程死锁问题死锁这么重要,请仔细阅读死锁问题死锁定义死锁举例如何排查死锁死锁发生的条件怎么解决死锁问题?线程通讯机制(wait/notify/notifyAll)LockSupport死锁问题死锁定义多线程编程中,因为抢占资源造成了线程无限等待的情况,此情况称为死锁。死锁举例注意:线程和锁的关系是:一个线程可以拥有多把锁,一个锁只能被一个线程拥有。当两个线程分别拥有一把各自的锁之后,又尝试去获取对方的锁,这样就会导致死锁情况的发生,具体先看下面代码:/***线程死锁问题*/public

    2022年7月13日
    13

发表回复

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

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