发布网友 发布时间:2022-04-25 15:38
共1个回答
热心网友 时间:2023-10-13 16:35
一般用传参来解决的,示例如下:
package test20210106;
/**灯泡类*/
public class Bulb {
//发光量
private double glowAmount;
//get、set方法
public double getGlowAmount() {
return glowAmount;
}
public void setGlowAmount(double glowAmount) {
this.glowAmount = glowAmount;
}
}
package test20210106;
/**开关类*/
public class Switch {
/**设置发光量30%*/
public void glowAmount30(Bulb bulb){
bulb.setGlowAmount(0.3);
System.out.println("当前灯泡的发光量是: "+(int)(bulb.getGlowAmount()*100)+"%");
}
/**设置发光量60%*/
public void glowAmount60(Bulb bulb){
bulb.setGlowAmount(0.6);
System.out.println("当前灯泡的发光量是: "+(int)(bulb.getGlowAmount()*100)+"%");
}
/**设置发光量100%*/
public void glowAmount100(Bulb bulb){
bulb.setGlowAmount(1);
System.out.println("当前灯泡的发光量是: "+(int)(bulb.getGlowAmount()*100)+"%");
}
}
package test20210106;
//
public class Test {
public static void main(String[] args) {
Bulb bulb = new Bulb();
Switch s = new Switch();
s.glowAmount30(bulb);
s.glowAmount60(bulb);
s.glowAmount100(bulb);
}
}