지난 며칠간 Java 클래스를 실험 해 보았습니다. youtube의 TheNewBoston과 java 문서에서 학습했습니다.기본 및 하위 클래스 생성자 문제
나는 다음과 같은 시나리오를 만들었고, 내가 가진 몇 가지 질문에 대한 여러분의 (여자도) 전문적인 비판과 깊이있는 지식을 찾습니다.
person 클래스와 person_financial 클래스는 각각 기본 클래스와 하위 클래스입니다.
사람 클래스 :
이public class person {
private String name;
private String sex;
private int age;
private double height;
private double weight;
private double intelligence;
// person constructor arguments order: name, height, weight, age, sex, intelligence
public person(){
this("noname",0,0,0,"undefined",5);
}
public person(String n){
this(n,0,0,0,"undefined",5);
}
public person(String n, double h){
this(n,h,0,0,"undefined",5);
}
public person(String n, double h, double w){
this(n,h,w,0,"undefined",5);
}
public person(String n, double h, double w, int a){
this(n,h,w,a,"undefined",5);
}
public person(String n, double h, double w, int a, String s){
this(n, h, w, a, filterSex(s), 5);
}
public person(String n, double h, double w, int a, String s, double i){
name = n;
height = h;
weight = w;
age = a;
sex = filterSex(s);
intelligence = i;
}
public void setName(String n){
name = n;
}
public void setHeight(double h){
height = h;
}
public void setWeight(double w){
weight = w;
}
public void setAge(int a){
age = a;
}
public void setSex(String s){
sex = filterSex(s);
}
public void setIntel(double i){
intelligence = i;
}
public String getName(){
return name;
}
public double getHeight(){
return height;
}
public double getWeight(){
return weight;
}
public int getAge(){
return age;
}
public String getSex(){
return sex;
}
public double getIntel(){
return intelligence;
}
public String getInfo(){
return String.format("Name: %s,\nSex: %s,\nAge: %d,\nIntelligence: %.2f,"
+ "\nHeight: %.2f,\nWeight: %.2f\n", name, sex, age,
intelligence, height, weight);
}
private static String filterSex(String s){
return ((s.equalsIgnoreCase("male") ||
s.equalsIgnoreCase("female")) ? s : "undefined");
}
}
person_financial 클래스 :
이public class person_financial extends person {
private double monies = 0;
public void definePerson(String n, int a, String s, double i, double h, double w){
setName(n);
setAge(a);
setSex(s);
setIntel(i);
setHeight(h);
setWeight(w);
}
public person_financial() {
this(0);
}
public person_financial(double m) {
monies = m;
}
public void depositMonies(double m) {
monies += m;
}
public void withdrawlMonies(double m) {
if (m <= monies) {
monies -= m;
}
}
public double getBalance() {
return monies;
}
}
다음 메인 클래스에서 내가 한 다음 person_financial 클래스
public class Main {
public static void main(String[] args) {
person p1 = new person("I have no Name", 180, 72, 38, "Alien", 7.2);
System.out.println(p1.getName());
person_financial pf1 = new person_financial(100.00);
pf1.depositMonies(50.02);
System.out.printf("%s has %.2f monies.\n", pf1.getName(), pf1.getBalance());
pf1.definePerson("some_name", 42, "male", 10, 180, 72);
System.out.println(pf1.getInfo());
}
}
, 내가 만든 가지고 그렇지 않으면 def 였을 모든 특성을 정의하는 데 사용하는 "definePerson()"이라는 메서드 'person'클래스의 'person()'생성자에서 가져온 것입니다. 하위 클래스의 기본 클래스에있는 변수에 값을 할당하는 데 더 전문적인 방법이있을 것이라고 확신합니다. 그 중 일부는 알지 못합니다. ...
또한 생성자를 " person '클래스를 사용하여'pf1 '에 대한 특성을 정의 할 수 있습니까? 예를 들어, 수동으로 각 속성 (예 : pf1.setName ("something"))을 설정하지 않아도됩니다. , 또는 pf1.setAge (1000000); 등 ... 또는 'definePerson()'에서와 같이 메서드를 사용하면됩니다.
아무런 도움이되지 않으므로, 감사합니다.).
감사합니다. 서브 클래스의 생성자에 "super ()"를 추가했습니다. –
Inventor22