Convert code built around structure to code built around class

조회 수: 1 (최근 30일)
James Taylor
James Taylor 2021년 4월 2일
답변: Brahmadev 2024년 2월 14일
What must I keep in mind if I want an entire repository built around creating, manipulating, and writing structure arrays to files converted to a repository which defines a class and methods?
I've noticed that checking for field existence is no longer helpful. Is there a checklist for this kind of thing anywhere? Or do people recommend that I build again from the ground up?

답변 (1개)

Brahmadev
Brahmadev 2024년 2월 14일
Converting a codebase that uses structures to one that uses classes involves several considerations. If the codebase is small enough, I would recommend using building the class from ground up and resuing code for functions wherever possible. Here's a checklist and some tips to help you with the transition:
  1. Determine the attributes (formerly fields in structures) for each class.
  2. Decide on the methods (functions) that will operate on the objects of these classes.
  3. Ensure that the class attributes are private or protected if necessary, to enforce encapsulation.
  4. Provide public getters and setters if external access to these attributes is required.
  5. Implement constructors to initialize object attributes and replace any setup code that was previously used with structures.
  6. Convert functions that manipulate structures into methods within the appropriate class.
A few other things can be considered such as adding error handling for class methods, testing the setters and getters, and updating the dependency properly.
Hope this helps in getting you started!

카테고리

Help CenterFile Exchange에서 Construct and Work with Object Arrays에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by