程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> Delphi >> Delphi語言學習6-類成員(字段和方法)

Delphi語言學習6-類成員(字段和方法)

編輯:Delphi

  1.Class FIElds

//例1成員變量
type
 TAncestor=class
   Value:Integer;
 end;
 TDescendant=class(TAncestor)
   Value:string;  //hidestheinheritedValuefIEld
 end;
var
 MyObject:TAncestor;
  
begin
 MyObject:=TDescendant.Create;
 MyObject.Value:='Hello!'  //error
 (MyObjectasTDescendant).Value:='Hello!' //works!
end;
//例2靜態字段
type
  TMyClass=class
   public
    classvar    //IntroduceablockofclassstaticfIElds.
     Red:Integer;
     Green:Integer;
     Blue:Integer;
    var       //Endstheclassvarblock.
     InstanceFIEld:Integer;
  end;
//靜態字段可以單獨調用
TMyClass.Red:=1;
TMyClass.Green:=2;
TMyClass.Blue:=3;
//靜態字段也可以通過對象實例調用
var
  myObject:TMyClass;
myObject.Red:=1;
myObject.Green:=2;
myObject.Blue:=3;

  2.Method Binding

//StaticMethods
//例1
type
  TFigure=class
   procedureDraw;
  end;
  TRectangle=class(TFigure)
   procedureDraw;
  end;
var
  Figure:TFigure;
  Rectangle:TRectangle;
     
  begin
      Figure:=TFigure.Create;
      Figure.Draw;       //callsTFigure.Draw
      Figure.Destroy;
      Figure:=TRectangle.Create;
      Figure.Draw;       //callsTFigure.Draw
      TRectangle(Figure).Draw; //callsTRectangle.Draw
      Figure.Destroy;
      Rectangle:=TRectangle.Create;
      Rectangle.Draw;     //callsTRectangle.Draw
      Rectangle.Destroy;
  end;

//虛方法和動態方法
type
  TFigure=class
   procedureDraw;virtual;
  end;
  TRectangle=class(TFigure)
   procedureDraw;override;
  end;
  TEllipse=class(TFigure)
   procedureDraw;override;
  end;
var
 Figure:TFigure;
 begin
  Figure:=TRectangle.Create;
  Figure.Draw;   //callsTRectangle.Draw
  Figure.Destroy;
  Figure:=TEllipse.Create;
  Figure.Draw;   //callsTEllipse.Draw
  Figure.Destroy;
 end;
//例2
type
 T1=class(TObject)
   procedureAct;virtual;
 end;
 T2=class(T1)
   procedureAct; //Actisredeclared,butnotoverridden
 end;
var
 SomeObject:T1;
begin
 SomeObject:=T2.Create;
 SomeObject.Act;  //callsT1.Act
end;
//類方法ClassMethod
//OrdinaryClassMethods
//ThedefinitionofaclassmethodmustbeginwiththereservedWordclass.Forexample,
type
 TFigure=class
 public
  classfunctionSupports(Operation:string):Boolean;virtual;
  classprocedureGetInfo(varInfo:TFigureInfo);virtual;
  ……
 end;
//Thedefiningdeclarationofaclassmethodmustalsobeginwithclass.Forexample,
classprocedureTFigure.GetInfo(varInfo:TFigureInfo);
begin
  ……
end;
//ClassStaticMethods
type
 TMyClass=class
  strictprivate
   classvar
   FX:Integer;
    
  strictprotected
  
   //Note:AccessorsforclasspropertIEsmustbedeclaredclassstatic.
   classfunctionGetX:Integer;static;
   classprocedureSetX(val:Integer);static;
  public
   classpropertyX:IntegerreadGetXwriteSetX;
   classprocedureStatProc(s:String);static;
 end;
//調用
TMyClass.X:=17;
TMyClass.StatProc('Hello');
  3.函數重載

type
 T1=class(TObject)
  procedureTest(I:Integer);overload;virtual;
 end;
 T2=class(T1)
  procedureTest(S:string);reintroduce;overload;
 end;
 ……
SomeObject:=T2.Create;
SomeObject.Test('Hello!');   //callsT2.Test
SomeObject.Test(7);       //callsT1.Test

  4構造函數

type
 TShape=class(TGraphicControl)
  private
   FPen:TPen;
   FBrush:TBrush;
   procedurePenChanged(Sender:TObject);
   procedureBrushChanged(Sender:TObject);
  public
   constructorCreate(Owner:TComponent);override;
   destructorDestroy;override;
   ……
 end;
constructorTShape.Create(Owner:TComponent);
begin
  inheritedCreate(Owner);  //Initializeinheritedparts
  Width:=65;     //ChangeinheritedpropertIEs
  Height:=65;
  FPen:=TPen.Create; //InitializenewfIElds
  FPen.OnChange:=PenChanged;
  FBrush:=TBrush.Create;
  FBrush.OnChange:=BrushChanged;
end;

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved