jython and toString
Walter S. Leipold
leipold at ace-net.com
Mon Oct 16 15:15:48 EDT 2006
toString() isn't supposed to be a static method. When you call
x.toString(), you're accessing x's non-static version of toString(), which
is inherited from Object.
-- Walt
ivansh (ishijak at gmail.com) wrote:
> For one java class (Hello) i use another (HelloPrinter) to build the
> string representation of the first one. When i've tried to use this
> from within jython, HelloPrinter.toString(hello) call gives results
> like Object.toString() of hello has being called. The example below
> shows this behaviour.
> Could somebody explain this?
>
> // Hello.java
> package jythontest;
> public class Hello {
> private String name;
> public Hello(String name)
> {
> this.name = name;
> }
> public String sayHello()
> {
> return "Hello, "+name;
> }
> }
>
> // HelloPrinter.java
> package jythontest;
> public class HelloPrinter {
> public static String toString(Hello h)
> {
> return h.sayHello();
> }
>
> public static String toMyString(Hello h)
> {
> return h.sayHello();
> }
> }
>
> # calljava.py
> from jythontest import *
> h = Hello("theName")
> print h
> print HelloPrinter.toString(h)
> print HelloPrinter.toMyString(h)
>
> OUTPUT:
> jythontest.Hello at 523ca2 // GOOD
> jythontest.Hello at 523ca2 // WRONG
> Hello, theName // GOOD
>
>
> Jython 2.1 on java (JIT: null)
>
> java version "1.5.0_03"
> Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_03-b07)
> Java HotSpot(TM) Server VM (build 1.5.0_03-b07, mixed mode)
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
More information about the Python-list
mailing list