델파이 클래스에서 속성을 선언 할 때 다른 종류의 결과를 가질 수 있습니까?Delphi 속성 읽기/쓰기
는예 :
property month: string read monthGet(
문자열 예에서) write monthSet(
정수);
, 내가 원하는, 부동산 달, 그 때 READ, 나는 문자열을 얻을; SET, 정수를 설정합니다;
델파이 클래스에서 속성을 선언 할 때 다른 종류의 결과를 가질 수 있습니까?Delphi 속성 읽기/쓰기
는예 :
property month: string read monthGet(
문자열 예에서) write monthSet(
정수);
, 내가 원하는, 부동산 달, 그 때 READ, 나는 문자열을 얻을; SET, 정수를 설정합니다;
재산에 대해 그렇게 할 방법이 없습니다. 속성에는 단일 유형이 있습니다.
목표를 달성하는 확실한 방법은 직접 사용하는 getter 및 setter 기능을 사용하는 것입니다.
function GetMonth: string;
procedure SetMonth(Value: Integer);
호출 코드의 혼동을 줄이기 위해 이름의 형식 부분을 만들 수도 있습니다. GetMonthStr
및 SetMonthOrd
라고 말하십시오.
이러한 기능을 두 개의 별도 속성으로 표시 할 수 있습니다. 하나는 읽기 전용이고 다른 하나는 쓰기 전용입니다.
"이러한 기능을 두 개의 별도 속성으로 표시 할 수 있습니다."좋은 아이디어 ;-) –
@JanDoggen 정확히 내가 제안한 해결책입니다. : D – mg30rg
그건 불가능합니다. 그러나 속성이 직접 내부 저장 장치에 해당하지 않기 때문에 당신이 할 수 있습니다
private
FMonth: Integer;
function GetMonthName: string;
...
property Month: Integer read FMonth write FMonth;
property MonthName: string read GetMonthName;
...
procedure TMyClass.GetMonthName: string;
begin
// code that finds name that corresponds to value of FMonth and returns it in Result.
end;
을 즉,이 두 가지 속성을 사용해야합니다, 하나 개의 쓰기 전용 (또는 일반), 하나는 읽기 전용 .
죄송합니다 당신의 답변이 나오기 전에 제 대답을 쓰기 시작했습니다. – mg30rg
문제 없습니다.해결책은 꽤 분명합니다. 그래서 두 개 또는 그 이상이 결론에 도달했습니다. –
당신은 직접 델파이에서 그렇게 할 수 없습니다.
당신이 같은 '캐스팅 속성을 "데 수행 할 수 있습니다
private
//...
intMonth: integer
//...
public
//...
property StrMonth: string read GetStrMonth write SetStrMonth;
property IntMonth: integer read intMonth write intMonth;
//...
end;
function YourClass.GetStrMonth: string;
begin
case intMonth of
1: Result := "January";
//...
end;
end;
procedure YourClass.SetStrMonth(Value: string);
begin
if StrMonth = "January" then
intMonth := 1;
//...
end;
end;
당신이 얻을 수있는 가장 가까운 Operator Overloading을 사용하는 것입니다하지만 게터/세터가 동일한 유형이어야합니다. 그것을 바꿀 방법이 없습니다.
program so_26672343;
{$APPTYPE CONSOLE}
{$R *.res}
uses
System.SysUtils;
type
TMonth = record
private
FValue: Integer;
procedure SetValue(const Value: Integer);
public
class operator implicit(a: TMonth): string;
class operator implicit(a: Integer): TMonth;
property Value: Integer read FValue write SetValue;
end;
TFoo = class
private
FMonth: TMonth;
public
property Month: TMonth read FMonth write FMonth;
end;
{ TMonth }
class operator TMonth.implicit(a: TMonth): string;
begin
Result := 'Month ' + IntToStr(a.Value);
end;
class operator TMonth.implicit(a: Integer): TMonth;
begin
Result.FValue := a;
end;
procedure TMonth.SetValue(const Value: Integer);
begin
FValue := Value;
end;
procedure Main;
var
LFoo: TFoo;
LMonthInt: Integer;
LMonthStr: string;
begin
LFoo := TFoo.Create;
try
LMonthInt := 4;
LFoo.Month := LMonthInt;
LMonthStr := LFoo.Month;
finally
LFoo.Free;
end;
end;
begin
try
Main;
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
end.
나는 이것이 가능하다는 것에 동의하며, 나는 그것에 대해서도 생각했다. 그러나 ISTM은 두 속성이나 setter와 getters를 직접 호출하는 것에 비해 너무 적었다. –
응용 프로그램의 전체 개념을 보면 너무 지나치지 않습니다. 도메인 유형 (ValueObjects)은 한 번만 정의되며 응용 프로그램 전체에서 사용되었습니다. 이 작은 샘플은 실제로 새에 로켓을 발사하는 것처럼 보입니다. –
응용 프로그램에 월이 중앙 유형 중 하나 인 경우 의미가 있습니다. 그렇지 않으면 그것은 너무 많이, IMO입니다. 암시 적 변환기를 사용하여 유형을 레코드로 정의하는 것은 유익한 개념이지만 의심의 여지가 없습니다. –
아니요. 이것이 AsString과 같은 속성과 함수를 형 변환하는 이유입니다. 최신 버전의 Delphi가 있으면 'TMonth'와 같은 유형을 정의하고 다양한 유형의 캐스팅에 대한 도우미 함수를 작성할 수 있습니다. 델파이의 버전은 무엇입니까? – TLama
그래서 어떻게 할 수 있습니까? –
http://docwiki.embarcadero.com/RADStudio/en/Operator_Overloading_(Delphi) –