我有一个遥控器,我想附加到另一个班级的开关,但我不知道该怎么做。
public class Remote {
boolean stateRemote;
public Remote()
{
stateRemote = false;
}
public void attach(Switch aSwitch)
{
}
public void pressButton()
{
if (stateRemote == false)
{
stateRemote = true;
}
else if (stateRemote == true)
{
stateRemote = false;
}
}
}
在“公共无效附加”中,我不知道该放什么。
这是我之前提到的另一个课程。
System.out.println("Testing Switch Methods\n_________________________________");
System.out.println("Making a new Switch with the no constructor having no parameters");
Switch aSwitch= new Switch();
System.out.println("After construction, Switch is on?: " + aSwitch.state());
aSwitch.flip();
System.out.println("After flipping, Switch is on?: " + aSwitch.state());
System.out.println("Making a new Switch with constructor having its parameter set to true");
aSwitch= new Switch(true);
System.out.println("After construction, Switch is on?: " + aSwitch.state());
aSwitch.flip();
System.out.println("After flipping, Switch is on?: " + aSwitch.state());
System.out.println("\nTesting Plug Methods\n_________________________________");
Plug aPlug = aSwitch.wherePlug();
System.out.println("Switch is on? " + aSwitch.state() +
" Plug is on? " + aPlug.state());
aSwitch.flip();
System.out.println("Switch is on? " + aSwitch.state() +
" Plug is on? " + aPlug.state());
System.out.println("\nTesting Lamp Methods\n_________________________________");
Lamp aLamp = new Lamp();
System.out.println("After construction, Lamp is on?: " + aLamp.state());
aLamp.attach(aSwitch.wherePlug());
System.out.println("After plugging, Lamp is on?: " + aLamp.state());
System.out.println("\nTesting Remote Methods\n_________________________________");
Remote aRemote = new Remote();
aRemote.attach(aSwitch);
System.out.println("Before pressing remote button");
System.out.println("Switch on? " + aSwitch.state() +
" Plug on? " + aPlug.state());
System.out.println("After pressing remote button");
aRemote.pressButton();
System.out.println("Switch on? " + aSwitch.state() +
" Plug on? " + aPlug.state());
切换班级
public class Switch {
boolean stateSwitch;
Plug plug;
public Switch()
{
stateSwitch = false;
plug = new Plug(stateSwitch);
}
public Switch(boolean initialState)
{
stateSwitch = initialState;
plug = new Plug(stateSwitch);
}
public boolean state()
{
return stateSwitch;
}
public void flip()
{
stateSwitch = !stateSwitch;
plug.flip();
}
public Plug wherePlug()
{
return plug;
}
}
插件类
public class Plug {
boolean statePlug;
public Plug()
{
statePlug = false;
}
public boolean state() {
return statePlug;
}
public Plug(boolean state)
{
statePlug = state();
}
public void flip()
{
statePlug = !statePlug;
}
}
答案 0 :(得分:1)
我想你想要这样的东西:
将其添加到班级的顶部:
//Add a field in your Remote class representing a Switch that your Remote can attach to
Switch s;
然后在attach()中添加行
//Give our Remote a reference to the Switch provided as the argument when you call attach()
s=aSwitch;
//Make the state of the Remote equal to the current state of the Switch
stateRemote = s.state();
然后在pressButton()中添加:
if(s != null) {
// When we click the button we want the Switch to be flipped. However first we have to check that s != null to make sure that we are attached to a Switch.
s.flip();
}
当我们在Remote上调用pressButton()时,它会在连接的Switch上调用flip()。因此,下次我们检查s.state()时,它将返回与我们按下按钮之前相反的情况。
添加插件:
在Plug类
中为插件添加新的构造函数
public Plug(boolean state) {
statePlug = state;
}
添加flip()方法
public void flip() {
statePlug = !statePlug;
}
在Switch类
中在Switch类的顶部添加
//declare a field in Switch for the Plug
Plug p;
在公共Switch()和公共Switch(布尔值)中,将此行添加到底部:
//create a new instance of Plug that we will use with this Switch.
//make the initial state of the plug equal to the state of the Switch.
p = new Plug(stateSwitch);
在wherePlug()中放置
//return the Plug we created
return p;
在Switch类的flip()中添加
//flip the value of our Plug so that it equals the value of the Switch
p.flip();
我不知道您是否应该如何为您的作业找到插件,但它将与您提供的测试代码一起使用。