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.

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/194258.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)
blank

相关推荐

  • cmd查看maven版本_maven获取最新版本

    cmd查看maven版本_maven获取最新版本cmd查看maven版本小技巧①键盘按下windows+Q②输入cmd③右击cmd以管理员身份运行④输入mvn-v或者mvn-version

  • Vue.js 入门教程[通俗易懂]

    Vue.js 入门教程[通俗易懂]Vue.js教程Vue.js(读音/vjuː/,类似于view)是一套构建用户界面的渐进式框架。Vue只关注视图层,采用自底向上增量开发的设计。Vue的目标是通过尽可能简单的API实现响应的数据绑定和组合的视图组件。MVVM模式下图不仅概括了MVVM模式(Model-View-ViewModel),还描述了在Vue.js中ViewModel是如何和View以及Model进行交互…

  • Oracle 绑定变量_oracle变量不在选择列表中

    Oracle 绑定变量_oracle变量不在选择列表中begintest.test(a=>1,b=>:603);end;执行的时候报错:Ora-01008错误:Oracle?并非所有变量都已绑定原因是??b=>:603之前多了一个冒号导致他判断错误,解决方法是检查和删除不需要的冒号。…

  • 大数据在应急管理中的应用[通俗易懂]

    大数据在应急管理中的应用[通俗易懂]随着互联网、社交媒体和人工智能的技术发展和应用普及,大数据在应急管理中发挥的作用将越来越重要,是应急管理未来发展的重要方向之一。应急管理部的成立为中国应急管理的发展提供了政策上的支持,也为发展大数据在中国应急管理中的应用提供了契机。现阶段,理论研究尚无法完全预知大数据在应急管理中的具体应用。但基于对应急管理基本原理的掌握,结合对大数据本质属性的理解和对中国应急管理制度情境的了解,我们可以初步厘清大…

  • linux chmod 755

    linux chmod 755chmod是Linux下设置文件权限的命令,后面的数字表示不同用户或用户组的权限。一般是三个数字:第一个数字表示文件所有者的权限第二个数字表示与文件所有者同属一个用户组的其他用户的权限第三个数字表示其它用户组的权限。权限分为三种:读(r=4),写(w=2),执行(x=1)。综合起来还有可读可执行(rx=5=4+1)、可读可写(rw=6=4+2)、可读可写可执行(rwx=7=4+2+1)。所以,ch…

  • 关于爱未删减版高清_init not tainted

    关于爱未删减版高清_init not taintedhttp://blog.csdn.net/wangkaiblog/article/details/8741042http://blog.linux.org.tw/~jserv/archives/001954.html

发表回复

您的电子邮箱地址不会被公开。

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