Originale-mail to me for new edition

 

The as operator

 

The as operator performs checked typecasts. The expression

object as class

returns a reference to the same object as object, but with the type given by class. At runtime, object must be an instance of the class denoted by class or one of its descendants, or be nil; otherwise an exception is raised. If the declared type of object is unrelated to class, that is, if the types are distinct and one is not an ancestor of the other, compilation error results. For example,

with Sender as TButton do

begin

  Caption := '&Ok';

  OnClick := OkClick;

end;

The rules of operator precedence often require as typecasts to be enclosed in parentheses. For example,

(Sender as TButton).Caption := '&Ok';

 

Topic groups

 

See also

About class types

Class operators: Overview

Class-reference types

Interface querying

The is operator

 

 

译文

 

as运算符

 

as运算符执行类型转换检查。其用法表达式如下:

object as class

表达式返回与object相同的对象的引用,但是是以class给出的类型。在运行时,object必需是class表示的类或其后裔类的实例,或者是nil;否则引发异常。如果object的声明类型与class无关,即它们二者各自独立并且其中任意一个不是另一个的祖先,那么将导致编译错误。例如,

with Sender as TButton do

begin

  Caption := '&Ok';

  OnClick := OkClick;

end;

运算符优先规则通常要求as类型转换被封装在圆括号中。例如,

(Sender as TButton).Caption := '&Ok';

 

主题组

 

相关主题

关于类类型

类运算符:概述

类引用类型

接口查询

is运算符