MATLAB Object-Oriented programming class and object creation _matlab

Source: Internet
Author: User

Before this, not much knowledge of object-oriented programming, let alone matlab object-oriented programming. However, in the first job experience still encountered a bit of use, but I was not very clear at that time. That was used to implement the use of the model in Simulink.

Today it's simple to do a class build and object creation. First implement the definition of a class, the code is as follows:

ClassDef person

Properties

Name

Age

Birth_year;

End

Methods

function obj = person (n,a,b)

Obj.name = n;

Obj.age = A;

Obj.birth_year = b;

End

function Show (obj)

DISP ([' Name: ', Obj.name]);

Disp ([' Age: ', Num2str (obj.age)]);

Disp ([' Birth_year: ', Num2str (obj.birth_year)]);

End

End

End

The definition of a class first needs to specify a keyword as the name of the class, and the following is divided into attributes and methods. Where the same function as the class name in the method is the constructor of the class. The use of classes is demonstrated as follows:

>> Tom = person (' Tom ', 10,2007)

Tom =

Person Withproperties:

Name: ' Tom '

Age:10

birth_year:2007

>> Tom.show ()

Name:tom

Age:10

birth_year:2007

By using this method, you can implement a method that creates an object through a class that has already been defined and invokes the object. Generally speaking, these methods are not very difficult. Later in the use, to see if you can live or work in the application of object-oriented technology in place.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.