如何使toString()方法返回Super Class私有字段及其实例字段? [英] How make toString() method return Super Class private fields also along with its instance fields?

查看:132
本文介绍了如何使toString()方法返回Super Class私有字段及其实例字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法让 toString()包含超级的私有字段?我尝试添加 super.toString(),但是没有用。

Is there a way to make toString() include private fields of the super class? I tried adding a super.toString(), no use however.

请参阅下面的代码

Employee.java

package test;

public class Employee {

private  String name;
private int id;
private double salary;

public Employee(String name, int id, double salary) {
    super();
    this.name = name;
    this.id = id;
    this.salary = salary;
}

public double getSalary() {
    return salary;
}

@Override
public String toString() {
    return "Employee [name=" + name + ", id=" + id + ", salary=" + salary
            + "]";
}

public static void main(String[] args) {

    Employee e=new Employee("Joe", 14, 5000);
    System.out.println(e);
    Manager m=new Manager("Bill", 23, 5000, 10);
    System.out.println(m);
    System.out.println("Employee Salary is "+e.getSalary()+"\nManager salary is "+m.getSalary());
}
}

Manager.java

package test;

public class Manager extends Employee{

private double bonus;
public Manager(String name, int id, double salary,int bonus) {
    super(name, id, salary);
    this.bonus=bonus;
}

public double getSalary()
{
    double baseSalary=super.getSalary();

    return (baseSalary+baseSalary*(bonus/100));

}

@Override
public String toString() {
    return(this.getClass().getName()+" ["+super.toString().substring((this.getClass().getSuperclass().getName().length()-3
            ), (super.toString().length())-1)+", bonus="+bonus+"]");
    //didn't work
    //super.toString();
    //return "Manager [bonus=" + bonus + "]";
}



}

输出

Employee [name=Joe, id=14, salary=5000.0]
test.Manager [name=Bill, id=23, salary=5000.0, bonus=10.0]
Employee Salary is 5000.0
Manager salary is 5500.0

这是我能做的最好的,连接 super.toString() +'一组字符串',当然这很乱,还有其他一些方法,即使语言规范不允许eclipse有一些设施可以做到这一点,注意:我用 eclipse 生成 toString 方法,我可以用任何方式告诉eclipse包含超类字段,

That was the best i could do , to concatenate super.toString()+' a set of Strings', surely this is messy , is there some other way , even if the language spec does not allow it does eclipse have some facility to do that , NOTE: I used eclipse to generate the toString method , any way by which i can tell eclipse to include the super class fields too,

换句话说我可以替换这个凌乱的代码

In other words can i replace this messy code

return(this.getClass().getName()+" ["+super.toString().substring((this.getClass().getSuperclass().getName().length()-3
            ), (super.toString().length())-1)+", bonus="+bonus+"]");

eclipse 自动化流程并生成合适的方法它?

by getting eclipse to automate the process and generate a suitable way to do it?

推荐答案

如果在超类中创建getter和setter,那么可以通过这些方法访问变量。其他可能性是将可见性从私有更改为受保护

If you create getters and setters in your superclass then you can acces the variables through those methods. Other possibility is to change the visibility from private to protected

第一个解决方案看起来像这样

first solution looks like this

员工

public class Employee {

    private String name;
    private int id;
    private double salary;

    public Employee(String name, int id, double salary) {
        super();
        this.name = name;
        this.id = id;
        this.salary = salary;
    }

    public double getSalary() {
        return salary;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }

    @Override
    public String toString() {
        return "Employee [name=" + name + ", id=" + id + ", salary=" + salary + "]";
    }
}

经理

public class Manager extends Employee {

    private double bonus;

    public Manager(String name, int id, double salary, int bonus) {
        super(name, id, salary);
        this.bonus = bonus;
    }

    public double getSalary() {
        double baseSalary = super.getSalary();

        return (baseSalary + baseSalary * (bonus / 100));

    }

    @Override
    public String toString() {
        return "Manager [name=" + getName() + ", id=" + getId() + ", salary=" + getSalary() + ", bonus=" + bonus + "]";
    }

}

第二个(使用受保护的)

Second one (using protected)

员工

public class Employee {

    protected String name;
    protected int id;
    protected double salary;

    public Employee(String name, int id, double salary) {
        super();
        this.name = name;
        this.id = id;
        this.salary = salary;
    }

    public double getSalary() {
        return salary;
    }



    @Override
    public String toString() {
        return "Employee [name=" + name + ", id=" + id + ", salary=" + salary + "]";
    }
}

经理

public class Manager extends Employee {

    protected double bonus;

    public Manager(String name, int id, double salary, int bonus) {
        super(name, id, salary);
        this.bonus = bonus;
    }

    public double getSalary() {
        double baseSalary = super.getSalary();

        return (baseSalary + baseSalary * (bonus / 100));

    }

    @Override
    public String toString() {
        return "Manager [name=" + name + ", id=" + id + ", salary=" + salary + ", bonus=" + bonus + "]";
    }

}

我个人使用的是吸气剂/ setter方法,但这取决于你。

Personally i'd use the getter/setter method but it's up to you.

编辑:在eclipse中添加到eclipse生成的 toString()
您似乎无法使用getter和setter生成它(只需快速查看,您可以看到一些文档这里
我弄清楚如何编辑代码生成 toString()时使用的模板,因此它包含来自超类的 toString()

Additonal to eclipse generation of toString() in eclipse. You can't seem to generate it with getters and setter (just had a quick look, you can see some documentation here. What I did figure out is how you can edit the Code Template used when generating the toString() so it includes the toString() from the superclass.

当您进入generate toString()对话框时,旁边有一个字段'String Format',其中包含< Default Template> 。当您单击编辑按钮时,您可以创建一个新的代码模板。
此模板自动保存<默认模板> ,看起来像这样:

When you enter the generate toString() dialog there is a field 'String Format' with <Default Template> next to it. when you click the edit button you can create a new Code Template. This template automatically holds the <Default Template> and should look something like this:

${object.className} [${member.name()}=${member.value}, ${otherMembers}]

您必须添加的内容最后是以下内容

only thing you'll have to add is the following at the end

[super: ${object.superToString}]

这样它会显示 toString()组成超类

这篇关于如何使toString()方法返回Super Class私有字段及其实例字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆