Why subclass & superclass in OOP?
이전 댓글 표시
I am a beginner in OOP. So far I found it very powerful. However, there is one concept, subclass & superclass, I do not understand very well. And do not know how to implement it but I am thinking I can utilize for my problem.
Could somebody explain to me with some example? What are the advantages?
Here is the problem I am dealing with. It is o describe objects in a room which contains many objects(child) like desks, tables, and other furniture. The room and the furniture have their own properties. I use a class for each of them as shown below. Calculations are to be done using the properties.
Some calculation to be done for subclass (child class like desk) needs to use properties of the superclass room. Some calculation to be done for superclass (child class like desk) needs to use properties of the subclass room.
So I am thinking the advantage of defining this type super/sub class relation is to help the reference to the properties of each other. What are other good applications. Otherwise, I can just define them as parallel class and include each other as input argument for function definitions. What difference does it, declaring super/subclass, make?
Thanks...
classdef room
properties
length
width
depth
end
end;
classdef desk < room
properties
size
color
weight
end
end
채택된 답변
추가 답변 (1개)
per isakson
2013년 10월 11일
편집: per isakson
2013년 10월 11일
1 개 추천
Your question requires more than an answer here.
See the Matlab documentation.
There are many introductions to oop on the net (however, not Matlab specific). Google.
The first two thirds of this might be helpful: Introduction to Object Oriented Programming Concepts (OOP) and More. See especially
- "What is the difference between Association, Aggregation and Composition?".
- "What is Inheritance?"
Understand "is-a" and "has-a".
desk is not a specialization of room; not a type of room; thus not an appropriate subclass of room.
카테고리
도움말 센터 및 File Exchange에서 Construct and Work with Object Arrays에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!