将C#Set / Get类方法改编成MATLAB类

时间:2016-02-08 15:19:18

标签: matlab

以下面的自定义C#类为例,它代表了一个观点:

public class Point
{
    // Attributes:

    double x;
    double y;

    // Constructor:

    public Point(double x, double y)
    {
        this.x = x;
        this.y = y;
    }

    // Methods:

    public void SetX(double x)
    {
        this.x = x;
    }

    public void SetY(double y)
    {
        this.y = y;
    }

    public double GetX()
    {
        return this.x;
    }

    public double GetY()
    {
        return this.y;
    }
}

我想将这个相同的类改编成MATLAB。我的目标是实现Set和Get方法以及构造函数,尽可能与C#方式类似。

我知道MATLAB不允许自我引用,正如我在this question上所读到的那样。但是,我知道我应该想要使用以下标题启动该类:

classdef Point < handle

我读到了这个herehere。该语句将我的自定义Point类声明为MATLAB handle类,使其继承自句柄超类。我知道这将允许我实现我试图模拟的Set / Get功能,以及构造函数。

这是我到目前为止所做的:

classdef Point < handle

    % Attributes
    properties (Access = private)
        x = [];
        y = [];
    end

    % Methods
    methods

        % Constructor: First method, must have the same name as the class.
        function Point(x,y)

        end

        % Set and Get methods:
        function SetX(x)

        end

        ...

    end
end

从现在开始,我不知道如何继续。

2 个答案:

答案 0 :(得分:4)

首先,如您对问题的评论中所述,首先让我们使用properties简化您的C#:

public class Point
{
    public Point(double x, double y)
    {
         this.X = x;
         this.Y = y;
    }

    public double X { get; set; }
    public double Y { get; set; }
}

现在要在Matlab中创建类似的类,你确实需要创建一个classdef文件,如果你想模仿传递引用,你必须从handle派生出来,其他对象将是通过价值传递。因此,使用属性您的maltab类将如下所示:

classdef Point < handle

    methods
        function [this] = Point(x, y)
            this.X = x;
            this.Y = y;
        end
    end

    properties
        X;
        Y;
    end

end

现在假设您需要实现用于验证的setter / getter,例如。您的C#类将如下所示:

public class Point
{
    public Point(double x, double y)
    {
         this.X = x;
         this.Y = y;
    }

    private double x; // Back-store
    public double X // Setters and getters
    { 
         get 
         { 
             return x; 
         } 
         set 
         {
             if (value < 0.0) { throw new InvalidArgumentException("X must be positive"); }
             x = value;
         } 
    }

    ... same for 'Y' ...
}

您可以使用dependent properties concept在Matlab中实现类似的getter / setter。语法非常类似于C#one,而有点过于冗长:

classdef Point < handle

    methods
        function [this] = Point(x, y)
            this.X = x;
            this.Y = y;
        end
    end

    % Back-store as in C#
    properties(SetAccess=private, GetAccess=private)
        x;
        y;
    end

    % Setters and getters using dependent properties
    properties(Dependent)
        X;
        Y;
    end

    % Definition for setters and getters
    methods
        function [value] = get.X(this)
            value = this.x;
        end
        function [] = set.X(this, value)
            if (value < 0.0), error('X must be positive'); end
            this.x = value;
        end

        ... Same for 'Y' ...

    end

end

为了进一步检查面向对象编程概念中的相似性和差异以及Matlab和C#之间的实现,我建议您阅读Mathworks网站上的documentation

答案 1 :(得分:0)

您可以执行以下操作:

function obj Point(x,y)
     obj.x=x;
     obj.y=y;
end

function setX(obj,x)
     obj.x=x;
end

function getX(obj)
     return obj.x;
end

与其他方法类似的东西。

然后你可以像这样使用它:

p1=Point(5,4);
p1.setX(3);
xp1=p1.getX;