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!