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)
上一篇 2025年11月9日 下午1:22
下一篇 2025年11月9日 下午2:01


相关推荐

  • java基础 —- 关键字 strictfp

    java基础 —- 关键字 strictfp自 Java2 以来 Java 语言增加了一个关键字 strictfp 虽然这个关键字在大多数场合比较少用 但是还是有必要了解一下 strictfp 的意思是 FP strict 也就是说精确浮点的意思 在 Java 虚拟机进行浮点运算时 如果没有指定 strictfp 关键字时 Java 的编译器以及运行环境在对浮点运算的表达式是采取一种近似于我行我素的行为来完成这些操作 以致于得到的结果往往无法令你满意 而一旦使

    2026年3月17日
    1
  • Lombok使用与踩坑

    Lombok使用与踩坑一 使用 Lombok1 在 pom 文件中引入依赖 dependency groupId org projectlombo groupId artifactId lombok artifactId optional true optional dependency

    2025年7月25日
    4
  • spring cloud 入门系列五:使用Feign 实现声明式服务调用

    一、SpringCloudFeign概念引入通过前面的随笔,我们了解如何通过SpringCloudribbon进行负责均衡,如何通过SpringCloudHystrix进行服务断路保护,两

    2022年2月16日
    43
  • 命令查看Win10等详细激活信息的方法

    命令查看Win10等详细激活信息的方法命令查看Win10等详细激活信息方法:1、slmgr全称:SoftwareLicenseManager2、VBS是基于VisualBasic的脚本语言。VBS的全称是:MicrosoftVisualBasicScriptEdition。(微软公司可视化BASIC脚本版)。Win+R输入:1、slmgr.vbs-dlv显示:最为详尽…

    2022年5月29日
    41
  • Linux进程间通信——使用共享内存

    Linux进程间通信——使用共享内存下面将讲解进程间通信的另一种方式,使用共享内存。一、什么是共享内存顾名思义,共享内存就是允许两个不相关的进程访问同一个逻辑内存。共享内存是在两个正在运行的进程之间共享和传递数据的一种非常有效的方式。不同进程之间共享的内存通常安排为同一段物理内存。进程可以将同一段共享内存连接到它们自己的地址空间中,所有进程都可以访问共享内存中的地址,就好像它们是由用C语言函数malloc分配的内存一

    2025年8月21日
    4
  • WPF面板布局介绍Grid、StackPanel、DockPanel、WrapPanel

    WPF面板布局介绍Grid、StackPanel、DockPanel、WrapPanelWPF面板布局介绍Grid、StackPanel、DockPanel、WrapPanel回顾上一篇,我们介绍了基本控件及控件的重要属性和用法,我们本篇详细介绍WPF中的几种布局容器及每种布局容器的使用场景,当然这些都是本人在实际项目中的使用经验,可能还存在错误之处,还请大家指出。本文大纲1、Grid2、StackPanel3、DockPanel…

    2022年7月22日
    10

发表回复

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

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