繁体   English   中英

如何强制toString()打印“undefined”而不是“null”

[英]How to force toString() to print “undefined” instead of “null”

我正在尝试使用toString方法在Fraction对象为null时打印“undefined”。 以下是我的类文件和测试文件中的位:

public class Fraction
{
    private int num;//Short for "numerator"
    private int den;//Short for "denominator"
    public Fraction(int a,int b)
    {
        num=a;
        den=b;
    }
    //Sample method that may result in a "null" object
    public Fraction divide(Fraction frac)
    {
        if(den!=0&&frac.den!=0)
            return new Fraction(num*frac.den,den*frac.num);
        return null;
    }

    public String toString()
    {
        simplify();
        if(this==null)
            return "undefined";
        if(den==0||this==null)
            return "undefined";
        if(den==1||num==den)
            return num+"";
        return num+"/"+den;
    }
}

public class FractionTest
{
    public static void main(String[]args)
    {
        Fraction f1=new Fraction(8,0);
        Fraction f2=new Fraction(2,1).divide(f1);
        System.out.println(f2);
        System.out.println(f2==null);
    }
}

不幸的是,代码产生了这个输出:

null
true

我该怎么做才能解决这个问题?

在Java中, this 永远不会为 null 如果一段代码在null对象上调用toString() ,则运行时将抛出NullPointerException

因此,您需要更改堆栈中较高的代码,将可能null Fraction转换为字符串。 这是实现目标的唯一途径。

如果没有提供divide方法,很难说,但是这段代码将f2设置为null:

Fraction f2=new Fraction(2,1).divide(f1);
System.out.println(f2);

我们还可以看到System.out.println(null)将打印null ,因此输出为null的原因是因为您将f2设置为null

正如其他帖子所提到的,你不能在null引用上调用toString :该引用没有你可以调用的方法。 因此,如果要打印不同的东西,则需要创建一个将null值转换为所需的undefined字符串的函数。

然而! 传递null是不好的做法。 应该做的是定义Function以具有未定义的状态:

public class Fraction
{
    private enum STATE {
        DEFINED, UNDEFINED
    };

    //No reason to shorten private names
    private int numerator; 
    private int denominator;
    private STATE state;

    //Make your variable names meaningful!!!
    public Fraction(int numerator,int denominator)
    {
        this.numerator=numerator;
        this.denominator=denominator;
        this.state=STATE.DEFINED;
    }

    //Sample method that may result in a "null" object
    public Fraction divide(Fraction fraction)
    {
        if (fraction.denominator == 0){
            this.state = STATE.UNDEFINED;
            return this;
        }
        ...
    }

    public String toString()
    {
        simplify();
        if(this.state.equals(STATE.UNDEFINED)){
            return "undefined";
        } else {
            ...
        } //Note: you should always use braces for ifs
    }
}

在这个公式中,您利用Java的面向对象特性让Fraction对象本身知道它是否是在您定义的数学空间上定义的。 我们知道分数没有为0分母定义,因此这是分数可以变为未定义的一种方式。 然后,当你需要分数报告它的状态时,它可以。 否则,程序中的每个其他类都需要知道如何处理未定义的(即“null”)分数,并且需要知道一个空的分数与一个完全没有定义的分数之间的区别(如除以零)。 这很快变得无法管理。

你不能这样做,因为this不能为null ,如果对象为null你不能调用toString方法,因为toString方法必须与object相关。

但是你可以通过外部方法或使用包装器类来间接地检查传递的对象是否为null然后打印undefined

实际上,使用this意味着对象存在,因此它不是null的解决方案将是,以限定的静态实例FractionFraction类表示一null Fraction对象。 让我们说:

public final static Fraction NULL_FRACTION = new Fraction (); // don't forget adding default constructor

在您的devide方法替代

return null;

通过

return Fraction.NULL_FRACTION;

toString方法中替换测试:

this==null

通过:

Fraction.NULL_FRACTION.equals (this)

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM