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


相关推荐

  • 详细介绍scrollIntoView()方法属性

    详细介绍scrollIntoView()方法属性因为工作中用到了锚点设置,常用的总是出问题,后来扒拉出了这个属性,详细研究了下方便日后使用介绍scrollIntoView()的详细属性简介该scrollIntoView()方法将调用它的元素滚动到浏览器窗口的可见区域。PS:根据其他元素的布局,元素可能无法完全滚动到顶部或底部。TIPS:页面(容器)可滚动时才有用!语法element.scrollIntoView();//等同于element.scrollIntoView(true)element.scrollIntoV

    2022年6月15日
    69
  • java基础练习题(含答案)[通俗易懂]

    java基础练习题(含答案)[通俗易懂]7、数组操作题一现在有如下一个数组:intoldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5},要求将以上数组中值为0的项去掉,将不为0的值存入一个新的数组,生成的新数组为:intnewArr[]={1,3,4,5,6,6,5,4,7,6,7,5}intoldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5};intcount=0;for…

    2022年7月7日
    24
  • HP打印机维修资料大全(续)

    HP打印机维修资料大全(续)

    2021年7月29日
    72
  • python安装numpy后pycharm导入不了_python的numpy库

    python安装numpy后pycharm导入不了_python的numpy库pycharm安装numpy失败的解决办法在pycharm中安装numpy时报错ValueError:check_hostnamerequiresserver_hostname始终无法安装发现最后一行跟hostname相关,就百度了一下发现这篇文章:ValueError:check_hostnamerequiresserver_hostname的解决办法记录发现是因为打开了VPN导致系统代理不正常关掉代理即可安装成功:…

    2022年8月25日
    4
  • Calendar类_java calendar

    Calendar类_java calendarCalendar类简介Calendar是javautil包下的一个工具类,提供了很方便的不同日期格式的处理。啥也不说了,直接撸代码:publicstaticvoidmain(String[]args){System.out.println(“————Calendar无参构造————“);//Cal…

    2022年9月24日
    0
  • Windows键失灵的三种处理方式「建议收藏」

    Windows键失灵的三种处理方式「建议收藏」对于经常使用电脑学习或者工作的我们来说,特别是搞编程的小伙伴们。Windows键的失灵将会导致我们的工作效率大大降低,比如:Win+E快速打开我的电脑;Win+R快速打开命令行等,许多组合键都不能使用,好烦躁。不要急,先看看这篇文章,可能有惊喜哦!

    2022年5月4日
    100

发表回复

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

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