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


相关推荐

  • fastJson String转Map[通俗易懂]

    fastJson String转Map[通俗易懂]1、引入依赖<dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.58</version></dependency>2、JSONOb…

    2025年10月23日
    6
  • FastClick源码分析

    FastClick源码分析玩过移动端web开发的同学应该都了解过,移动端上的click事件都会有300毫秒的延迟,这300毫秒主要是浏览器为了判断你当前的点击时单击还是双击,但有时候为了更快的对用户的操作做出更快的响应,越过这个300毫秒的延迟是有点必要的,faskclick做的就是这件事,这篇文章会理清faskclick的整体思路,分析主要的代码,但不会贴出所有的代码,仅分析主干,由于历史原因,faskclick对旧版本…

    2022年6月19日
    31
  • Sybase PowerDesigner 12通用的破解方法

    Sybase PowerDesigner 12通用的破解方法

    2021年5月8日
    222
  • 个人能不能开发ctp期货交易_什么是程序化交易期货

    个人能不能开发ctp期货交易_什么是程序化交易期货接触CTP也才半年多,一边学习一边摸索,看到各大CTP的QQ群里,也都是在问一些很菜的问题,就简单总结和介绍下,今天主要是基础知识,即CTP程序的基础和开源的Demo版本:CTP交易接口是由::::::上海期货信息技术有限公司::::::开发的,提供C++的接口,网上也有很多C++的Demo版本,可以直接使用。1:上期所的接口为两个.dll、两个.lib和四个.h文件,初学者可以不要C

    2022年10月8日
    4
  • matlab中wavedec2,说说wavedec2函数【图】

    matlab中wavedec2,说说wavedec2函数【图】说说wavedec2函数【图】08-10栏目:技术TAG:wavedec2wavedec2http://maiqiuzhizhu.blog.sohu.com/110325150.htmlcopyrightjhua.orgwavedec2函数:copyrightjhua.org1.功能:实现图像(即二维信号)的多层分解.https://www.jhua.org多层,即多尺度.www.jhua…

    2022年6月29日
    20
  • php nginx 负载均衡

    php nginx 负载均衡1:nginx服务器192.168.182.128:8081/代码服务器:192.168.182.129:81192.168.182.131:812:在nginx服务器配置nginx.confupstreamtomcatserver1{server192.168.182.129:81weight=3;server192.168.182.131:81;}server{…

    2022年6月18日
    25

发表回复

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

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