event.ClassInstanceEvent クラス
名前空間: event
スーパークラス: event.EventData
InstanceCreated
イベントと InstanceDestroyed
イベントのイベント データ
説明
作成
MATLAB® は、InstanceCreated
または InstanceDestroyed
イベントをトリガーするときに、event.ClassInstanceEvent
オブジェクトを作成します。
プロパティ
作成または削除されているオブジェクト
属性:
GetAccess | public |
SetAccess | private |
GetObservable | true |
SetObservable | true |
データ型: object
イベントのソースである matlab.metadata.Class
オブジェクト。matlab.metadata.Class
オブジェクトとして返されます。
属性:
GetAccess | public |
SetAccess | private |
GetObservable | true |
SetObservable | true |
イベントの名前。InstanceCreated
または InstanceDestroyed
である文字ベクトルとして返されます。
属性:
GetAccess | public |
SetAccess | private |
GetObservable | true |
SetObservable | true |
データ型: char
例
matlab.metadata.Class
イベントを使用して、オブジェクトが作成または破棄されたときにインスタンスの数をカウントします。
InstanceCreated
イベントと InstanceDestroyed
イベントのコールバック関数を使用してクラスを作成します。eventCallback
静的メソッドは永続変数を使用して、存在するクラスのインスタンス数を保存します。addEventListeners
メソッドは、リスナーを CountInstances
クラスの matlab.metadata.Class
オブジェクトに追加します。
classdef CountInstances methods (Static) function eventCallback(~,eventData) % Callback for InstanceCreated and InstanceDestroyed persistent instanceCount if ~isempty(instanceCount) switch eventData.EventName case "InstanceCreated" instanceCount = instanceCount + 1; case "InstanceDestroyed" if ~instanceCount == 0 instanceCount = instanceCount - 1; end end else instanceCount = 1; end fprintf('%s %d \n',... 'Number of Instances: ',instanceCount) end function addEventListeners(mc) % Add listeners addlistener(mc,"InstanceCreated",... @(src,evnt)CountInstances.eventCallback(src,evnt)); addlistener(mc,"InstanceDestroyed",... @(src,evnt)CountInstances.eventCallback(src,evnt)); end end end
CountInstances
クラスの matlab.metadata.Class
オブジェクトを作成し、InstanceCreated
イベントと InstanceDestroyed
イベントのリスナーを追加します。この例では、両方のイベントに同じコールバックを使用します。
mc = ?CountInstances; CountInstances.addEventListeners(mc)
CountInstances
クラスのオブジェクトを作成または破棄するたびに、イベントによって instanceCount
永続変数が更新されます。
obj1 = CountInstances;
Number of Instances: 1
obj2 = CountInstances;
Number of Instances: 2
clear obj1
Number of Instances: 1
matlab.metadata.Class
イベントを使用して、オブジェクトの作成および破棄を確認します。
InstanceCreated
イベントと InstanceDestroyed
イベントのコールバック関数を使用してクラスを作成します。eventCallback
静的メソッドは、両方のイベントのコールバック関数として機能します。addEventListeners
メソッドは、リスナーを ClassInstanceEvent
クラスの matlab.metadata.Class
オブジェクトに追加します。
classdef ClassInstanceListeners properties Prop end methods function obj = ClassInstanceListeners(p) obj.Prop = p; end end methods (Static) function eventCallback(~,eventData) % Callback for InstanceCreated and InstanceDestroyed I = eventData.Instance; S = eventData.Source; E = eventData.EventName; dashLine = sprintf('%s\n','--------------------'); fprintf('%s',dashLine) fprintf('%s \n',['Class: ',class(I)]) fprintf('%s %d \n',[S.PropertyList.Name ': '],I.Prop) fprintf('%s%s \n','Event: ', E) fprintf('%s',dashLine) end function addEventListeners(mc) % Add listeners addlistener(mc,"InstanceCreated",... @(src,evnt)ClassInstanceListeners.eventCallback(src,evnt)); addlistener(mc,"InstanceDestroyed",... @(src,evnt)ClassInstanceListeners.eventCallback(src,evnt)); end end end
CreateInstanceListeners
クラスの matlab.metadata.Class
オブジェクトを作成し、InstanceCreated
イベントと InstanceDestroyed
イベントのリスナーを追加します。この例では、両方のイベントに同じコールバックを使用します。CreateInstanceListeners
クラスのインスタンスを作成し、プロパティ Prop
に識別子を割り当てます。
mc = ?ClassInstanceListeners; ClassInstanceListeners.addEventListeners(mc) obj = ClassInstanceListeners(1334);
-------------------- Class: ClassInstanceListeners Prop: 1334 Event: InstanceCreated --------------------
同じ変数に割り当てられた別のインスタンスを作成すると、新しいオブジェクトが作成され、古いオブジェクトが破棄されます。
obj = ClassInstanceListeners(7335);
-------------------- Class: ClassInstanceListeners Prop: 7335 Event: InstanceCreated -------------------- -------------------- Class: ClassInstanceListeners Prop: 1334 Event: InstanceDestroyed --------------------
クラス定義を変更すると、matlab.metadata.Class
オブジェクトはクラスの有効な説明ではなくなるため、MATLAB ではこのオブジェクトが削除されます。クラスを変更した後に、新しい matlab.metadata.Class
オブジェクトを作成して、リスナーを追加しなければなりません。
バージョン履歴
R2008a で導入
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)