在我想要的项目中,Getter和Setter方法用于所有成员变量。我怎样才能做到这一点?这是什么意思? 以下是我想使用getter和setter方法的代码
isNaN
答案 0 :(得分:1)
public class Connection {
private int previousReading;
private int currentReading;
private float[] slabs;
public Connection(int currentReading, int previousReading,float slabs[])
{
this.currentReading = currentReading;
this.previousReading = previousReading;
this.slabs = slabs;
}
public int getPreviousReading() {
return previousReading;
}
public void setPreviousReading(int previousReading) {
this.previousReading = previousReading;
}
public int getCurrentReading() {
return currentReading;
}
public void setCurrentReading(int currentReading) {
this.currentReading = currentReading;
}
public float[] getSlabs() {
return slabs;
}
public void setSlabs(float[] slabs) {
this.slabs = slabs;
}
}
答案 1 :(得分:1)
为了实现所需的设计,您需要遵循封装概念(也称为' 数据隐藏'),这是一个基本的OOP概念。 Encapsulation的含义基本上是将变量包装在访问器方法(称为getter和setter)中,以便从其他类中隐藏它们。 变量只能通过那些getter和setter访问,而不能直接访问。
public class Person {
private String name;
private int age;
public String getName() {
return this.name;
}
public String setName(final String name) {
this.name = name;
}
public String getAge() {
return this.age;
}
public String setAge(final String age) {
this.age = age;
}
}