塞特斯没有做他们应该在java中做的事情

时间:2013-03-18 03:22:25

标签: java setter

我有一个只有99个频道和20个最高音量的电视类但我的音量和频道设置器也没有正常工作,当我添加一个而不是添加1 到10这是我的默认音量,它只添加一个给定的音量数字 我的电视课程

public class TV
{
    private String flag;
    private int ch;
    private int vol = 10;

    public TV(String onOffSwitch, int channel, int volume)
    {
        flag = onOffSwitch;
        ch = channel;
        vol = volume;
    }

    public void setFlag(String onOffSwitch)
    {
        onOffSwitch = "Off";
    }// End of setFlag

    public String getFlag()
    {
        return flag;
    }// End of getFlag

    public void setCh (int newChannel)
    {
        if (newChannel >= 99)
        {
            ch = 99;
        }else
        {
            ch = newChannel;
        }

        if(newChannel < 0)
        {
            ch = 0;
        }
    }//end of setCh

    public int getCh ()
    {
        return ch;
    }// End of getCh

    public void setVol(int newVolume)
    {
        if(newVolume >= 20)
        {
            vol = 20;
        }

        if(newVolume < 0)
        {
            vol=0;
        }
    }// End of SetVolume

    public void incrementVolume()
    {
        vol++;
    }

    public int getVol()
    {
        return vol;
    }// ENd of getVolume

    public String toString()
    {
        return String.format("%s :%s\n%s:%d\n%s :%d","TV is switched", flag,"TV channel",ch,"TV volume",vol);
    }

}// End of TV class

我的测试云端硬盘类

import java.util.Scanner;

public class TvTest
{
    public static void main (String[] args)
    {
        String tvStatus;
        int chan;
        int volu;

        Scanner input = new Scanner(System.in);
        TV tv2 = new TV("off",105,10);

        System.out.print(tv2);
        System.out.println();

        System.out.print("Turn TV On or Off ?");
        tvStatus = input.nextLine();

        System.out.print("Channel : ");
        chan = input.nextInt();

        System.out.print("Volume : ");
        volu = input.nextInt();

        TV tv1 = new TV(tvStatus,chan,volu);
        tv1.incrementVolume();

        System.out.println(tv1);
    }
}

和测试输出

TV is switched :off
TV channel:105
TV volume :10
Turn TV On or Off ?on
Channel : 105
Volume : 1
TV is switched :on
TV channel:105
TV volume :2

为什么我的安装人员不工作?

5 个答案:

答案 0 :(得分:2)

你的构造函数应该使用你的setter:

public TV(String onOffSwitch, int channel, int volume)
{
    this.setFlag(onOffSwitch);
    this.setCh(channel);
    this.setVol(volume);
}

setFlag应该将flag设置为传入的值。

public void setFlag(String onOffSwitch)
{
    this.flag = onOffSwitch;
}// End of setFlag

答案 1 :(得分:1)

public void setFlag(String onOffSwitch)
{
    onOffSwitch = "Off";
}// End of setFlag

onOffSwitch变量不是字段。这是参数列表中的名称,应改为flag

答案 2 :(得分:1)

 public void setVol(int newVolume)
    {
        if(newVolume >= 20)
        {
            vol = 20;
        }

        if(newVolume < 0)
        {
            vol=0;
        }
    }// End of SetVolume

除非新值超出范围,否则该setter不会执行任何操作。

答案 3 :(得分:0)

问题不在于你的setter / getters(尽管他们看起来有点奇怪),问题是,你在构造函数中忽略它们......

public TV(String onOffSwitch, int channel, int volume)
{
    flag = onOffSwitch;
    ch = channel;
    vol = volume;
}

尝试使用类似

的内容
public TV(String onOffSwitch, int channel, int volume)
{
    setFlag(flag);
    setCh(channel);
    setVol(volume);
}

,而不是...

变量onOffSwitch也是未定义的,所以你的例子不会编译;)。它应该读起来像......

public void setFlag(String onOffSwitch)
{
    flag = onOffSwitch;
}// End of setFlag

请注意,我可能会使用boolean值并使用turnOnturnOff方法,但那只是我...

答案 4 :(得分:0)

我一整天都在喝酒,所以请记住这一点,但我猜你也是因为你无法完成这项基本任务。 p

首先,永远不要,我的意思是使用String来表示两个状态值。布尔很好,所以使用它们。

class Televsion {
    power = false; # off
    power = true;  # on
}

其次,你的变量名很糟糕。不是意思,他们之间有相当多的错过匹配,不必要的程序复杂。也不要使用缩写或短手变量名。我可以理解,如果这可能是20年或10年前,但现代编辑倾向于有一个自动完成功能,有助于填写一到两个关键代码。

看起来您还没有了解关键字“this”。当您在类中使用“this”时,它会从类中调用特定的变量或方法。主要的一点是,它可以避免参数和局部变量发生任何名称冲突。

class Televsion {
    private boolean power;
    private int channel;
    private int volume;

    public Televsion(  boolean power, int channel, int volume ) {
        this.power = power;
        this.channel= channel;
        this.volume = volume;
    }
}

通过这种方式,读者可以更加理智; P

在main方法中测试代码适用于newbs和loser。 JUnit现在几乎内置于Java中,所以为什么不使用它,或者你可以使用TestNG ^。^

构建测试套件可以更容易地设计和重做代码。

你也可能想看看Hamcrest。

public TelevsionTest {
    private Televsion televsion;

    @Before
    public void boilerplateSetUpCode() {
        // The before annonation has it's roots in apsectJ, I beleve.
        // In this context it makes this method run before every test.

        televsion = new Televsion();
    }

    @Test
    public void testSwitchOn() {
        televsion.switchOn();
        assertTrue( televsion.getPowerState() == true ); 
        # JUnits assertions are weak, that's why I like hamcrest.
    }
}

尝试编写可重用性和可维护性的代码。从长远来看,这可以使生活更轻松,但在拍摄期间却是一个挑战。如果你有线或卫星怎么办,这会影响频道号吗?

以下是关于如何重新解决问题的五分钟黑客攻击。

public class Televsion {     私有Logger4j记录器;

private ChannelSource channelSource; // Naming is a little weak
private int volume;
private boolean power;

public Tv() {
    channelSource= channelSource.LOCAL;
    volume = 0;
    power = false;
}

public turnOn(){ power = true; }
public void turnOff() { power = false; }
public void muteVolume() { volume = 0; }
public void setVolume( volume ) {
    if ( volume > -1 && volume < 100 ) {
        this.volume = volume;
    } else {
        logger.dubug( String.format("Arg: %d", volume) );
        // This is anti-pattern of how not to word a debugging statement.
        // Plus, I'm lazy...
    }
}
public void changeChannelSource( ChannelSource channelSource ) {
    this.channelSource = channelSource;
}
public void changeChannelNumber( int channelNumber ) {
    channelSource.changeChannelNumber( channelNumber );
}

// I like singletons 
public enum ChannelSource {
    LOCAL(0), ALT(0), CABLE(0);

    private int channelNumber;

    private ChannelSource( int channelNumber ) {
        this.channelNumber = channelNumber ;
    }        


    public int getChannelNumber() { return channelNumber; }
    public void setChannelNumber( channelNumber ) {
        // Lazy hack
        if ( this.getName() == "LOCAL" ) {
            if ( channelNumber > -1 && channelNumber < 100 ) {
                this.channelNumber = channelNumber;
            } else {
                logger.debug( 
                    String.format("Unable to change %s, channel to %d", this.getName(), channelNumber) 
                );
            }
        }
    }
}

}

希望如果您开始提高编码标准并开始学习优秀的编程技术,那么您可能会喜欢编写足以构建实际Stack Exchange帐户的代码:)

这是一个简单的列表,您应该学习如何让您的Java老师满意。

- Unit Testing
   + Might as well let a computer grade the code then a human, free A's

- Data Structures
   + Just using collections good enough.  
   + Side rant, If use this style of matrices, Object[][] I will haunt you down.
      - It's inefficient, plus hard for people maintaining this shitty code
      - Jagged arrays can work just as well List< List<Object> > or you can even just use a single array, [] and sprinkle in some magic.

- Annotations
   + Helps takes the Java out of Java

- Read Effective Java
   + It's not a difficult book, will make life easier.

- Know a little about concurrency
   + If you use the synchronized keyword, then you know something is wrong.
   + Take a look into concurrent utils API
   + Read Java Concurrency in Practice

- Spring
   + If you want to get paid for knowing Java

我确信我还有很多其他的东西要离开,但对于任何开始学习Java的人来说都应该足够了。