Java 使用setter和getter操作变量
我在另一个网站上看到了这个代码示例。我已经研究了一段时间,试图找出一些简单的东西,但我很难决定从哪里开始。 如何允许用户进入控制台以打开/关闭灯或风扇? 所有这些关于setter/getter的东西都令人困惑。编写打开/关闭某些东西的逻辑似乎很简单,但如果将它与构造函数和setter/getter混合在一起,我就会迷路。我无法理解如何以及在何处使用用户输入操作isOn布尔值 如果有任何指导或代码,我将不胜感激。 谢谢Java 使用setter和getter操作变量,java,Java,我在另一个网站上看到了这个代码示例。我已经研究了一段时间,试图找出一些简单的东西,但我很难决定从哪里开始。 如何允许用户进入控制台以打开/关闭灯或风扇? 所有这些关于setter/getter的东西都令人困惑。编写打开/关闭某些东西的逻辑似乎很简单,但如果将它与构造函数和setter/getter混合在一起,我就会迷路。我无法理解如何以及在何处使用用户输入操作isOn布尔值 如果有任何指导或代码,我将不胜感激。 谢谢 如何允许用户进入控制台以打开/关闭灯或风扇? 使用你的代码,你可以做这样的事情
如何允许用户进入控制台以打开/关闭灯或风扇?
使用你的代码,你可以做这样的事情
public static void main(String[] args) {
System.out.println("Enter On or Off for fan: ");
Scanner scanner = new Scanner(System.in);
String input = scanner.nextLine();
boolean on = false;
if(input.equalsIgnoreCase("yes")) {
on = true;
} else if( input.equalsIgnoreCase("no")) {
on = false;
} else {
System.out.println("Please enter yes or no!");
return;
}
Household fan=new Household("fan", on);
System.out.println(hh);
}
但这并不是真的使用你的二传手或接球手。你可以创建这样的家庭类
public class Household {
private String choice;//instance variables
private boolean isOn;
public void setOn(boolean isOn) {
this.isOn = isOn;
}
public boolean getOn() {
return isOn;
}
public void setChoice(String choice) {
this.choice = choice;
}
public String getChoice() {
return choice;
}
public String toString() {
return "Choice:" + getChoice() + "\tisOn:" + (isOn() ? "Yes" : "NO");
}
}
在上面的代码中,将其切换到:
Household fan = new Household();
fan.setChoice("fan");
fan.setOn(on);
System.out.println(fan);
如何允许用户进入控制台以打开/关闭灯或风扇?
使用你的代码,你可以做这样的事情
public static void main(String[] args) {
System.out.println("Enter On or Off for fan: ");
Scanner scanner = new Scanner(System.in);
String input = scanner.nextLine();
boolean on = false;
if(input.equalsIgnoreCase("yes")) {
on = true;
} else if( input.equalsIgnoreCase("no")) {
on = false;
} else {
System.out.println("Please enter yes or no!");
return;
}
Household fan=new Household("fan", on);
System.out.println(hh);
}
但这并不是真的使用你的二传手或接球手。你可以创建这样的家庭类
public class Household {
private String choice;//instance variables
private boolean isOn;
public void setOn(boolean isOn) {
this.isOn = isOn;
}
public boolean getOn() {
return isOn;
}
public void setChoice(String choice) {
this.choice = choice;
}
public String getChoice() {
return choice;
}
public String toString() {
return "Choice:" + getChoice() + "\tisOn:" + (isOn() ? "Yes" : "NO");
}
}
在上面的代码中,将其切换到:
Household fan = new Household();
fan.setChoice("fan");
fan.setOn(on);
System.out.println(fan);
如何允许用户进入控制台以打开/关闭灯或风扇?
使用你的代码,你可以做这样的事情
public static void main(String[] args) {
System.out.println("Enter On or Off for fan: ");
Scanner scanner = new Scanner(System.in);
String input = scanner.nextLine();
boolean on = false;
if(input.equalsIgnoreCase("yes")) {
on = true;
} else if( input.equalsIgnoreCase("no")) {
on = false;
} else {
System.out.println("Please enter yes or no!");
return;
}
Household fan=new Household("fan", on);
System.out.println(hh);
}
但这并不是真的使用你的二传手或接球手。你可以创建这样的家庭类
public class Household {
private String choice;//instance variables
private boolean isOn;
public void setOn(boolean isOn) {
this.isOn = isOn;
}
public boolean getOn() {
return isOn;
}
public void setChoice(String choice) {
this.choice = choice;
}
public String getChoice() {
return choice;
}
public String toString() {
return "Choice:" + getChoice() + "\tisOn:" + (isOn() ? "Yes" : "NO");
}
}
在上面的代码中,将其切换到:
Household fan = new Household();
fan.setChoice("fan");
fan.setOn(on);
System.out.println(fan);
如何允许用户进入控制台以打开/关闭灯或风扇?
使用你的代码,你可以做这样的事情
public static void main(String[] args) {
System.out.println("Enter On or Off for fan: ");
Scanner scanner = new Scanner(System.in);
String input = scanner.nextLine();
boolean on = false;
if(input.equalsIgnoreCase("yes")) {
on = true;
} else if( input.equalsIgnoreCase("no")) {
on = false;
} else {
System.out.println("Please enter yes or no!");
return;
}
Household fan=new Household("fan", on);
System.out.println(hh);
}
但这并不是真的使用你的二传手或接球手。你可以创建这样的家庭类
public class Household {
private String choice;//instance variables
private boolean isOn;
public void setOn(boolean isOn) {
this.isOn = isOn;
}
public boolean getOn() {
return isOn;
}
public void setChoice(String choice) {
this.choice = choice;
}
public String getChoice() {
return choice;
}
public String toString() {
return "Choice:" + getChoice() + "\tisOn:" + (isOn() ? "Yes" : "NO");
}
}
在上面的代码中,将其切换到:
Household fan = new Household();
fan.setChoice("fan");
fan.setOn(on);
System.out.println(fan);
所以试着去打破它 getter和setter背后的思想是能够检索和设置对象内部变量的数据,而不必公开变量本身。这意味着类顶部的字段应该声明为private。您只希望能够通过getter和setter访问它们 在main方法中,通过使用构造函数创建
househouse
对象的实例(您可以将构造函数视为构造实例的对象)。您的构造函数说,要调用它,它需要两个数据段,一个字符串
和一个布尔值
。当使用这两段数据调用构造函数时,它会将它们存储在它在字段choice
和isOn
中创建的实例中
现在您有了househouse
(hh
)的实例,您有了一个包含两段数据的对象。如果要检索为其中一个变量存储的数据,可以使用getter
hh.getChoice();
或
这将返回存储在选项
/isOn
字段中的家庭
特定实例的数据
如果要更改存储在hh中的数据,请使用setters。因此,如果要使用
hh.setOn(true);
或
所以试着去打破它 getter和setter背后的思想是能够检索和设置对象内部变量的数据,而不必公开变量本身。这意味着类顶部的字段应该声明为private。您只希望能够通过getter和setter访问它们 在main方法中,通过使用构造函数创建
househouse
对象的实例(您可以将构造函数视为构造实例的对象)。您的构造函数说,要调用它,它需要两个数据段,一个字符串
和一个布尔值
。当使用这两段数据调用构造函数时,它会将它们存储在它在字段choice
和isOn
中创建的实例中
现在您有了househouse
(hh
)的实例,您有了一个包含两段数据的对象。如果要检索为其中一个变量存储的数据,可以使用getter
hh.getChoice();
或
这将返回存储在选项
/isOn
字段中的家庭
特定实例的数据
如果要更改存储在hh中的数据,请使用setters。因此,如果要使用
hh.setOn(true);
或
所以试着去打破它 getter和setter背后的思想是能够检索和设置对象内部变量的数据,而不必公开变量本身。这意味着类顶部的字段应该声明为private。您只希望能够通过getter和setter访问它们 在main方法中,通过使用构造函数创建
househouse
对象的实例(您可以将构造函数视为构造实例的对象)。您的构造函数说,要调用它,它需要两个数据段,一个字符串
和一个布尔值
。当使用这两段数据调用构造函数时,它会将它们存储在它在字段choice
和isOn
中创建的实例中
现在您有了househouse
(hh
)的实例,您有了一个包含两段数据的对象。如果要检索为其中一个变量存储的数据,可以使用getter
hh.getChoice();
或
这将返回存储在选项
/isOn
字段中的家庭
特定实例的数据
如果要更改存储在hh中的数据,请使用setters。因此,如果要使用
hh.setOn(true);
或