Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
910 views
in Technique[技术] by (71.8m points)

dart - Default stringify for objects, equivalent to Java's toString?

I have just take a look at the 3o 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.

For example:

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

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>

I would rather do:

    <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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If you have something like this:

class MyClass {
    String data;

    MyClass(this.data);

    @override
    String toString() {
        return data;
    }
}

MyClass myObject = new MyClass("someData");
print(myObject); // outputs "someData", not 'Instance of MyClass'

I think this might be what you are looking for.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...