对象的默认字符串化,等同于Java的toString? [英] Default stringify for objects, equivalent to Java's toString?

查看:53
本文介绍了对象的默认字符串化,等同于Java的toString?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只看了dart的3º教程,创建了评级组件.我想知道是否在对对象进行字符串化时调用相同的方法,类似于Java的toString.

I have just take a look at the 3º tutorial from dart, creating the rating component. I was wondering if there is same method which is called when stringifying an object, something similar to Java's toString.

例如:

MyClass myObject = new MyClass();
System.out.println(myObject);

如果被覆盖,将调用MyClass.toString(),否则将调用它的父级,直到到达java.lang.Object并提供默认的toString.

Will call MyClass.toString() if overwriten, else will call it's parent until java.lang.Object is reached giving a default toString.

我发现这样做很丑陋(完全主观):

I find kind ugly (completely subjective) doing:

<span ng-repeat="star in cmp.stars" > {{star.toString()}} </span>

我宁愿这样做:

    <span ng-repeat="star in cmp.stars" > {{star}} </span>

并给出我希望它以平均写法显示的方式的实现.这可能吗?

And give the implementation of how I want it to display at an averwritten method. Is this possible?

推荐答案

是的,它对于print,字符串插值或Angular胡子如此工作.

Yes it works like this for print, String interpolation or Angular mustaches.

通过覆盖对象上的String toString()方法,显示的值将是此toString()调用的结果.如果在类层次结构中没有定义toString(),则将调用ObjecttoString()(它将为class MyClass{}返回Instance of 'MyClass').

By overriding the String toString() method on your object the displayed value will be the result of this toString() call. If there's no toString() defined in the class hierarchy the toString() of Object will be called (which will return Instance of 'MyClass' for class MyClass{}).

这篇关于对象的默认字符串化,等同于Java的toString?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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