I am a beginner of Java. I have a question.
Foo.java
public class Foo {
private String name;
private int num;
Foo(String name, int num) {
this.name = name;
this.num = num;
}
public String getName() {
return this.name;
}
public int getNum() {
return this.num;
}
public void setName(String name) {
this.name = name;
}
public void setNum(int num) {
this.num = num;
}
}
Main.java
public class Main {
public static void main(String[] args) {
String name = "Tom";
int num = 60;
Foo bar = new Foo(name, num);
}
}
Instead of this typical style of class on Java, I'd like to set and get instance properties with getter()
and setter()
method like...
public void setter(String p) {
this.p = p;
}
public String getter(String q) {
return this.q
}
I know these don't work, but I want to write Java method like following these codes in Python
.
setattr(self, key, val)
getattr(self, key)
Would you please give me some pieces of advices?