MATLAB - 使用者自定義類



MATLAB 傳統上以其強大的矩陣運算和內建函式而聞名,但它也支援面向物件程式設計 (OOP)。MATLAB 中的使用者自定義類允許您建立自定義資料型別,這些資料型別封裝了資料和對該資料進行操作的函式(方法)。這種方法可以幫助組織程式碼,提高可重用性,並更自然地對複雜系統進行建模。

什麼是類?

類是建立物件(例項)的藍圖。它定義了物件將具有的屬性(資料)和方法(函式)。

定義類

在 MATLAB 中,您可以使用 classdef 關鍵字定義類。以下是一個簡單的示例:

classdef MyClass
    properties
        Property1
    end
    
    methods
        function obj = MyClass(val)
            if nargin > 0
                obj.Property1 = val;
            end
        end
        
        function output = myMethod(obj, input)
            output = obj.Property1 + input;
        end
    end
end

示例的分解

1. 類定義

classdef MyClass

此行聲明瞭一個名為 MyClass 的新類。

2. 屬性塊

properties
    Property1
end

屬性是儲存類物件的變數。在此示例中,MyClass 有一個屬性 Property1。

3. 方法塊

methods

方法是定義類行為的函式。它們對類的物件進行操作。

4. 建構函式方法

function obj = MyClass(val)
    if nargin > 0
        obj.Property1 = val;
    end
end

建構函式方法是 MATLAB 在建立新物件時呼叫的特殊函式。建構函式方法的名稱與類名稱相同。在此,如果提供了輸入值 val,則建構函式會初始化 Property1。

5. 其他方法

function output = myMethod(obj, input)
    output = obj.Property1 + input;
end

這是一個名為 myMethod 的常規方法。它接受兩個輸入:obj(物件本身)和 input。它返回 Property1 和 input 的總和。

建立物件

要建立 MyClass 的物件,您可以使用建構函式方法

obj = MyClass(10);
disp(obj.Property1);  % Here Output: 10

使用方法

您可以使用點表示法在物件上呼叫方法

result = obj.myMethod(5);
disp(result);  % Here Output: 15

使用類的優勢

  • 封裝 - 將相關的資料和函式組合在一起,使程式碼更模組化,更易於管理。
  • 可重用性 - 定義類後,可以在專案的不同部分或不同專案中重用它。
  • 抽象 - 隱藏內部實現細節,僅公開必要的介面。
  • 繼承 - 建立繼承現有類的屬性和方法的新類,從而促進程式碼重用和層次結構類結構。

示例 1:點類

以下是一個更實用的示例,定義一個類來表示二維點。

classdef Point
    properties
        X
        Y
    end
    methods
        function obj = Point(x, y)
            if nargin > 0
                obj.X = x;
                obj.Y = y;
            end
        end
        
        function obj = set.X(obj, x)
            if x >= 0
                obj.X = x;
            else
                error('X must be non-negative');
            end
        end
        
        function obj = set.Y(obj, y)
            if y >= 0
                obj.Y = y;
            else
                error('Y must be non-negative');
            end
        end
        
        function distance = distanceToOrigin(obj)
            distance = sqrt(obj.X^2 + obj.Y^2);
        end
    end
end

使用點類

p = Point(3, 4);
disp(p.X);  % Output: 3
disp(p.Y);  % Output: 4

dist = p.distanceToOrigin();
disp(dist);  % Output: 5

在此示例中 -

  • 點類具有 X 和 Y 屬性來儲存座標。
  • 建構函式初始化這些屬性。
  • 設定方法(set.X 和 set.Y)驗證座標是否為非負數。
  • distanceToOrigin 方法計算從原點到點的歐幾里得距離。

示例 2:圓形類

此類將包括圓的半徑和中心的屬性,以及計算面積、周長以及檢查點是否在圓內的的方法。

圓形類定義

classdef Circle
    properties
        Radius
        Center
    end
    methods
        % Constructor
        function obj = Circle(radius, center)
            if nargin > 0
                obj.Radius = radius;
                obj.Center = center;
            end
        end
        
        % Method to calculate area
        function area = area(obj)
            area = pi * obj.Radius^2;
        end
        
        % Method to calculate circumference
        function circumference = circumference(obj)
            circumference = 2 * pi * obj.Radius;
        end
        
        % Method to check if a point is inside the circle
        function isIn = isPointInside(obj, point)
            distance = sqrt((point(1) - obj.Center(1))^2 + (point(2) - obj.Center(2))^2);
            isIn = distance <= obj.Radius;
        end
    end
end

圓形類的分解

1. 類定義

classdef Circle

2. 屬性

properties
    Radius
    Center
end

圓形類有兩個屬性:Radius(圓的半徑)和 Center(一個 2 元素向量,表示圓中心的 x 和 y 座標)。

3. 建構函式方法

function obj = Circle(radius, center)
    if nargin > 0
        obj.Radius = radius;
        obj.Center = center;
    end
end

如果提供了輸入引數,則建構函式會初始化 Radius 和 Center 屬性。

4. 面積方法

function area = area(obj)
    area = pi * obj.Radius^2;
end

此方法使用公式 𝜋 x Radius2 計算圓的面積。

5. 周長方法

function circumference = circumference(obj)
    circumference = 2 * pi * obj.Radius;
end

此方法使用公式 2 x x Radius 計算圓的周長。

6. 點在圓內檢查方法。

function isIn = isPointInside(obj, point)
    distance = sqrt((point(1) - obj.Center(1))^2 + (point(2) - obj.Center(2))^2);
    isIn = distance <= obj.Radius;
end

此方法檢查給定點是否在圓內。它計算點到圓中心的距離,並檢查此距離是否小於或等於半徑。

使用圓形類

要建立圓形物件並使用其方法,您可以執行以下操作。

% Create a Circle object with radius 5 and center at (0, 0)
c = Circle(5, [0, 0]);

% Calculate the area
a = c.area();
disp(['Area: ', num2str(a)]);  % Output: Area: 78.5398

% Calculate the circumference
circ = c.circumference();
disp(['Circumference: ', num2str(circ)]);  % Output: Circumference: 31.4159

% Check if a point is inside the circle
point = [3, 4];
isInside = c.isPointInside(point);
disp(['Is the point inside the circle? ', num2str(isInside)]);  % Output: Is the point inside the circle? 1

% Check if another point is inside the circle
point = [6, 0];
isInside = c.isPointInside(point);
disp(['Is the point inside the circle? ', num2str(isInside)]);  % Output: Is the point inside the circle? 0
廣告

© . All rights reserved.