简体   繁体   English

如何在Delphi对象或字符串中隐式使用ToString()?

[英]How to use ToString() implicitly in Delphi objects or strings?

Delphi is like an England Queen Guard. 德尔福就像英格兰女王卫队。 It does not like ambiguity and may even kill to protect the hard code. 它不喜欢歧义,甚至可能会杀死以保护硬代码。 But Java is almost a street corner woman. 但Java几乎是街角女性。 When I use this is java: 我用的时候是java:

 Button button = new Button();
 String a = "This is a " + button;

I get This is a button 我知道This is a button

But if I do that in Delphi: 但如果我在Delphi中这样做:

 ShowMessage('This is a ' + Button1);

I get an error, because Delphi has toString() method (now) but it does not implicitly calls it. 我得到一个错误,因为Delphi有toString()方法(现在),但它没有隐式调用它。 Because literal strings are not objects in OP. 因为文字字符串不是OP中的对象。 The correct use is: 正确的用途是:

 ShowMessage('This is a ' + Button1.toString());

Is there any way to override this behave so it works like Java? 有没有办法覆盖这种行为,所以它像Java一样工作?

For reference: How an object will call toString method implicitly? 供参考: 对象如何隐式调用toString方法?

There's no way to enforce an implicit cast or method call on an object instance. 无法对对象实例强制执行隐式强制转换或方法调用。

If this was a record that you controlled then you could implement an Implicit class operator that would perform a cast to string . 如果这是您控制的记录,那么您可以实现一个Implicit类运算符,该运算符将执行强制转换为string

The issue discussed link that you refer to is simply an implementation detail of PrintStream.println() . 讨论的问题链接只是PrintStream.println()的实现细节。 That relies on the ability of String.valueOf() to come up with a string representation for any object which in turn relies on Object.toString() . 这取决于String.valueOf()为任何对象提供字符串表示的能力,而该对象又依赖于Object.toString() In fact the discussion there concerning println() is unrelated to the way the + operator in Java works which is the pertinent issue in your question. 事实上,关于println()的讨论与Java中+运算符的工作方式无关,这是你问题中的相关问题。

Delphi's TObject has a virtual ToString method that could be used to perform the same purpose. Delphi的TObject有一个虚拟的ToString方法,可用于执行相同的目的。 So it would be easy enough to use the exact same technique as PrintStream.println() in Delphi code. 因此,在Delphi代码中使用与PrintStream.println()完全相同的技术就足够了。

If you had a special function that worked like Format , you could get that behavior. 如果你有一个像Format一样工作的特殊函数,你可以得到那种行为。 I wrote a function like that several years ago, back when the built-in function didn't support Unicode. 几年前我编写了一个函数 ,当内置函数不支持Unicode时。 I've now updated it to support calling ToString on an object when it's passed as the argument for the %s format string. 我现在更新它以支持在对象作为%s格式字符串的参数传递时调用ToString You'd call it like this: 你这样称呼它:

ShowMessage(WideFormat('This is a %s', [Button1]));

I'm sorry the code hasn't really been touched in several years, so it might not work as-is in newer Delphi versions. 对不起,代码在几年内还没有真正被触及,所以它可能不适用于较新的Delphi版本。 You'll have to decide whether it's worth the effort to update that code, although it was included in Project Jedi long enough that it did get a few nominal updates to support UnicodeString and 64-bit compilation. 您将不得不决定是否值得更新该代码,尽管它已经包含在Project Jedi中足够长,以至于它确实获得了一些名义上的更新以支持UnicodeString和64位编译。

Too bad that it works only on ARM compiler and Delphi.NET Great article by Nick Hodges in http://edn.embarcadero.com/article/34324 太糟糕了,它只适用于ARM编译器和Delphi.NET很棒的文章,Nick Hodges在http://edn.embarcadero.com/article/34324

TMyClass = class
    class operator Add(a, b: TMyClass): TMyClass; // Addition of two operands of type TMyClass
    class operator Subtract(a, b: TMyClass): TMyclass; // Subtraction of type TMyClass
    class operator Implicit(a: Integer): TMyClass; // Implicit conversion of an Integer to type TMyClass
    class operator Implicit(a: TMyClass): Integer; // Implicit conversion of TMyClass to Integer
    class operator Explicit(a: Double): TMyClass; // Explicit conversion of a Double to TMyClass
end;

// Example implementation of Add class operator 
TMyClass.Add(a, b: TMyClass): TMyClass;
begin
  ...
end;

var
x, y: TMyClass
begin
  x := 12; // Implicit conversion from an Integer 
  y := x + x; // Calls TMyClass.Add(a, b: TMyClass): TMyClass 
  b := b + 100; // Calls TMyClass.Add(b, TMyClass.Implicit(100)) 
end;

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

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