This is one of OOP’s boring academic questions, but it’s not homework. I got a question from a novice programmer about one of these silly examples of OOP tutorials.
Imagine that you are designing a square class and a Cube class that should inherit which?
I see a relationship, but what it is, I really don’t see!
Could you give me a logical argument in the mind of OOP.
None! Since a square is not a cube, and a cube is not a square, none of them should inherit from the other. A square can inherit from a polygon, and a cube can inherit from a polyhedron, but both of them are mutually exclusive.
. - "is-a" (, "is-a" , ). , - .
, , . - , 6 ( , 6 , ), , . "is-a" , ...
, , , . Square extends Rectangle. :
, . changeArea (double area) getSide(), Cube. , 6 ( 6 ).
, Square Cube: setVolume ( ), ,
, , GeometryObjectWithEqualSides, .:)
.
struct Square // Rectangle actually { Square( int dx, int dy ) : dx(dx), dy(dy) {}; int dx; int dy; int floor_area() { return dx*dy; }; }; struct Cube : Square // Cuboid actually { Cube( int dx, int dy, int dz ) : Square(dx, dy), dz(dz) {}; int dz; int cube_area() { return floor_area()*2+dx*dz*2+dy*dz*2; }; };
, .
"Hypercube", (0 ), (1 ) , . Hypercube ( , , n- ).
/, , , .. .
. Hypercube .
. , - . , .
, , ;)
, . . , : . , Square ? ? , : Square Cube? , : " , ", " , ", - , , . , , , .
, " 6 " - , .
Source: https://habr.com/ru/post/1712894/More articles:Pad byte [] up to 16 bytes for AES Encryption - c #PHP syntax error - phpSQLite3 in C # .NET - c #Rails: how to use self.inherited effectively - ruby-on-railsWhat is the best way to make one instance application in .net? - c #Как сделать easy_install развернуть пакет в каталоги, а не один файл яйца? - pythonIE7 Back Button loses history in .NET application - .netПроверка CVS: проверка проекта в каталоге, отличном от того, в котором вы сейчас находитесь? - cvsLoading a DataTable Slow when bound to a DataGridView.Datasource - c #MPI Signal Processing - pythonAll Articles