在类之间传递布尔值,可以通过以下几种方式实现:
class MyClass {
private boolean myBoolean;
public MyClass(boolean myBoolean) {
this.myBoolean = myBoolean;
}
}
MyClass myClass = new MyClass(true);
class MyClass {
private boolean myBoolean;
public void setMyBoolean(boolean myBoolean) {
this.myBoolean = myBoolean;
}
}
MyClass myClass = new MyClass();
myClass.setMyBoolean(true);
class MyClass {
private boolean myBoolean;
public boolean getMyBoolean() {
return myBoolean;
}
}
MyClass myClass = new MyClass();
myClass.setMyBoolean(true);
boolean myBoolean = myClass.getMyBoolean();
interface MyInterface {
void setMyBoolean(boolean myBoolean);
boolean getMyBoolean();
}
class MyClass implements MyInterface {
private boolean myBoolean;
@Override
public void setMyBoolean(boolean myBoolean) {
this.myBoolean = myBoolean;
}
@Override
public boolean getMyBoolean() {
return myBoolean;
}
}
MyInterface myInterface = new MyClass();
myInterface.setMyBoolean(true);
boolean myBoolean = myInterface.getMyBoolean();
class ParentClass {
protected boolean myBoolean;
}
class ChildClass extends ParentClass {
public void setMyBoolean(boolean myBoolean) {
this.myBoolean = myBoolean;
}
public boolean getMyBoolean() {
return myBoolean;
}
}
ChildClass childClass = new ChildClass();
childClass.setMyBoolean(true);
boolean myBoolean = childClass.getMyBoolean();
在这些方法中,你可以根据具体的需求选择合适的方式来在类之间传递布尔值。
领取专属 10元无门槛券
手把手带您无忧上云