Java中的Get和set(这里是新手程序员)
我正在尝试创建一个吉他程序。我有一个吉他放大器类,有一个声音对象,我试图访问我的皮卡类。我的目标是将拾取类中的sound属性设置为与GuitarAmp类中的sound属性相同,这样我就可以设置字符串的所有声音属性。我真的不知道该怎么做,我读过的关于Java中GET和SET的文章也没有帮助。下面列出了我的两门课,如有任何关于get和set的帮助,将不胜感激Java中的Get和set(这里是新手程序员),java,setter,getter,Java,Setter,Getter,我正在尝试创建一个吉他程序。我有一个吉他放大器类,有一个声音对象,我试图访问我的皮卡类。我的目标是将拾取类中的sound属性设置为与GuitarAmp类中的sound属性相同,这样我就可以设置字符串的所有声音属性。我真的不知道该怎么做,我读过的关于Java中GET和SET的文章也没有帮助。下面列出了我的两门课,如有任何关于get和set的帮助,将不胜感激 public class GuitarAmp { // This is what I want to get from this c
public class GuitarAmp
{
// This is what I want to get from this class
public GuitarAmpSound sound;
public GuitarAmp(GuitarAmpSound sound, int volume, int distortSetting) {
sound = new GuitarAmpSound();
sound.setVolume(64);
sound.setDistortSetting(GuitarAmpSound.JAZZ);
}
public void changeVolume(int newVolume)
{
sound.setVolume(newVolume);
}
}
这是皮卡课
public class GuitarPickup {
public GuitarAmpSound pickupSound;
public void Connect(GuitarString strings[], GuitarAmp amp)
{
pickupSound = new GuitarAmpSound();
//This is where I need to set it
for(int i = 1; i <= 6; i++)
{
strings[i].setSound(pickupSound);
}
}
}
公共类GuitarPickup{
公共吉他和拾音器;
公共void连接(GuitarString[],GuitarAmp)
{
pickupSound=新的GuitarAmpSound();
//这是我需要设置的地方
对于(int i=1;i您需要声明一个字段(属于类的特定实例的变量)来保存该对象的每一条信息。例如GuitarPickup
上的pickupSound
字段
在Java中,使用与getter和setter相同的字段名称是一种很强的惯例。例如,对于卷
,代码的相关部分如下所示:
public class GuitarAmpSound {
private int sound = 0;
public void setSound(int sound) {
this.sound = sound; // "this.sound" means the sound field, not the parameter
}
public int getSound() {
return sound; // or this.sound
}
}
如果您想为for
循环实现必要的代码,您的GuitarString
类需要一个自己命名的GuitarAssound
字段和相应的getter和setter
需要注意的是,for
循环中的条件有很多问题。Java中的数组是基于零的(因此6弦吉他上的弦会从0变为5),并且您不应该在循环中硬编码数组大小,而应该使用strings.length
。最后,如果您只想检索数组(或集合)中的每个元素,Java有一种更方便的语法:
您的代码毫无意义,请将GuitarAmp类替换为:
public class GuitarAmp {
//This is what I want to get from this class
private GuitarAmpSound sound;
public GuitarAmp() {
sound = new GuitarAmpSound();
sound.setVolume(64);
sound.setDistortSetting(GuitarAmpSound.JAZZ);
}
public void changeVolume(int newVolume){
sound.setVolume(newVolume);
}
public GuitarAmpSound getSound() {
return sound;
}
public void setSound(Sound sound) {
this.sound = sound;
}
}
对于获取和设置,规则很简单:
public YourClassName getYourObjectName() {
return yourObjectName;
}
public void setYourObjectName(YourClassName yourObjectName) {
this.yourObjectName = yourObjectName;
}
这真的很复杂明白你真正想要什么看看有什么变化但是
我想你需要重新构造这些物体。你想做什么?你能解释得更清楚些吗
public class GuitarAmp {
//This is what I want to get from this class
public GuitarAmpSound sound;
--> the sound you pass before call the constructor
sound.setVoolume(x);
sound.setDistortSetting(Y)
so you pass the object sound with the attributes full of information
public GuitarAmp(GuitarAmpSound sound){
this.sound = sound;
}
public void changeVolume(int newVolume){
this.sound.setVolume(newVolume);
}
}
这是皮卡课
public class GuitarPickup {
public GuitarAmpSound pickupSound;
public void Connect(GuitarString strings[], GuitarAmp amp)
{
pickupSound = new GuitarAmpSound();
//This is where I need to set it
for(int i = 1; i <= 6; i++)
{
strings[i].setSound(pickupSound);
}
}
}
公共级吉他演奏会{
public GuitarAmpSound pickupSound;
public void Connect(GuitarString strings[], GuitarAmp amp)
{
pickupSound = new GuitarAmpSound();
//This is where I need to set it
for(int i = 0; i<strings.length; i++)
{
amp.setSound(strings[i].getSound());
}
}
public GuitarAmpSound pickupSound;
公共void连接(GuitarString[],GuitarAmp)
{
pickupSound=新的GuitarAmpSound();
//这是我需要设置的地方
对于(int i=0;i从概念上讲,将相同的声音值保存到每个GuitarString没有什么意义。请阅读,谢谢!这是一个很大的帮助。我们被特别指示不要使用数组中的0位置,这就是我硬编码它的原因。至于GuitarString类中的GuitarAmpSound属性,它已经为GuitarSound属性和GuitarSound扩展了GuitarSound。@user3359778 Ouch。请注意,您的讲师试图提供帮助,但显然不理解编程,尤其是Java编程。如果您希望进行任何实际的Java编程,请获取Josh Bloch的最新版本的有效Java,并仔细阅读;哟你可能不会马上得到很多,但这会帮助你确定你不想陷入的坏习惯。我对他这学期的一些指示有意见。不幸的是,如果事情没有按照他的指示去做,我就得不到我想要的分数。我试着不染上任何坏习惯,但有几个学生已经抱怨过了我们会看看会发生什么。对不起,我可能应该提到还有其他几个类。GuitarAmpSound已经有了我需要的所有get和set方法。代码现在运行良好。谢谢你的帮助!