简体   繁体   English

无法调用super.toString

[英]Can't call super.toString

I tried calling the super.toString of the superclass in the subclass Manager, but it won't work. 我尝试在子类管理器中调用超类的super.toString,但是它不起作用。 I want the output to be like this: 我希望输出是这样的:

current data:
#Person (ID=1):
firstname: NN                  
lastname: NN                  
email: unknown
date of birth: 01.01.1970
annual pay: 0.00
#Manager:
annual base pay: 0.00
bonus: 0.00

firstname: lastname: email: date of birth: enter date (y m d)

but all I get is without displaying attributes from the superclass: 但是我得到的只是不显示超类的属性:

firstname:
lastname:
email:
date of birth: enter date (y m d)

Any suggestions will be highly appreciated thanks 任何建议将不胜感激谢谢

 public class Person {

            private String fn;
            private String ln;
            private String email;
            private Date dob;
            private static int nextID=1;
            private final int ID=nextID++;

            Person(){
                this.fn = "NN";
                this.ln = "NN";
                this.email = "unknown";
                this.dob = new Date();
            }

            public String getFn(){
                return fn;
            }

            public void setFn(String fn){
                if(fn==null || fn.equals("")){
                    return;
                }

                this.fn=fn;
            }

            public String getLn(){
                return ln;
            }

            public void setLn(String ln){
                if(ln==null || ln.equals("")){
                    return;
                }

                this.ln=ln;
            }


            public String getEmail(){
                return email;
            }

            public void setEmail(String email){
                if(ln==null || ln.equals("")){
                    return;
                }
                if(!email.contains("@")){
                    return;
                }

                this.email=email;
            }

            public Date getDob(){
                return new Date(dob);
            }

            public void setDob(Date dob){

                if(dob==null){
                    return;
                }

                this.dob= new Date(dob);
            }

            public String toString(){
                return String.format("current data:\n#Person (ID=%d)\nfirstname: %s\nlastname: %s\nemail: %s\ndate of birth: %s\nannual pay: %d", getID(), getFn(), getLn(), getEmail(), getDob(), getAnnualPay());
            }

            public int getAnnualPay(){
                return 0;
            }

            public void editPerson() {
                //firstname: lastname: email: date of birth: enter date (y m d)
                TextIO.putln("firstname:");
                this.fn = TextIO.getln();

                TextIO.putln("lastname:");
                this.ln = TextIO.getln();

                TextIO.putln("email:");
                this.email = TextIO.getln();

                TextIO.putln("date of birth: enter date (y m d)");
                this.dob = new Date();
            }

            public int getID(){
                return ID;
            }



        }



        public class Manager extends Person {

            private int annualBasePay;
            private int bonus;

            public Manager(){
                this.annualBasePay = 0;
                this.bonus = 0;
            }

            public int getAnnualPay(){
                return annualBasePay+bonus;
            }

            public void editPerson(){
                this.toString();
                super.editPerson();
            }

            public void setAnnualBasePay(int annualBasePay){
                if(annualBasePay<=0){
                    return;
                }
                this.annualBasePay=annualBasePay;
            }

            public int getAnnualBasePay(){
                return annualBasePay;
            }

            public void setBonus(int bonus){
                if(bonus<0){
                    return;
                }
                this.bonus=bonus;
            }

            public int getBonus(){
                return bonus;
            }

            public String toString(){
                //super.toString();
                return String.format("%s/n#Manager:\nannual base pay: %d\nbonus: %d",super.toString(),getAnnualBasePay(), getBonus());
            }
        }

Adjust: 调整:

public String toString(){
                super.toString();
                return String.format("%s/n#Manager:\nannual base pay: %d\nbonus: %d",super.toString(),getAnnualBasePay(), getBonus());
        }

to

 public String toString(){

            return super.toString()+"\n"+String.format("%s/n#Manager:\nannual base pay: %d\nbonus: %d",super.toString(),getAnnualBasePay(), getBonus());
        }

Forget about super.toString(). 忘了super.toString()。 Just invoke the getters for the properties you need from Person. 只需为Person中所需的属性调用getter。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java - 我的 super.toString 返回 null - Java - My super.toString is returning null Java-调用super.toString()格式化结果集 - Java - Calling super.toString() to format a resultset 将toString()方法中的super关键字隐式转换为super.toString()不会发生 - Implicit conversion of super keyword in toString() method to super.toString() not happening Java子类使用super.tostring(),但不使用superclass&#39;方法。 为什么? - Java Subclass uses super.tostring() but not superclass' method in it. Why? 使用super.toString将String和int添加到父类中 - Adding in String and int to parent class with super.toString 如何在Java中使用super.toString()将toString从子级传递到父级 - How to pass toString from child to parent using super.toString() in Java 为什么&#39;T.super.toString()&#39;和&#39;super :: toString&#39;使用合成访问器方法? - Why 'T.super.toString()' and 'super::toString' use a synthetic accessor method? 为什么我不能在double值上调用toString()? - Why can't I call toString() on a double value? 如何从派生类外部调用超级方法(即:toString()) - How to call a super method (ie: toString()) from outside a derived class 为什么我不能直接转换 super 调用的结果? - Why can't I cast the result of the super call directly?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM