开发者

Simple object-oriented exercises for grasping the basics [closed]

开发者 https://www.devze.com 2023-04-11 00:02 出处:网络
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references,or expertise, but this question will likely solicit debate, a
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 11 years ago.

I'm teaching a few friends some basic object-oriente开发者_如何学God concepts in PHP and i wanted to give them some real-world examples, but simple, so they can grasp the syntax and the basics of OO. I already gave them the following exercise: create a small class that abstracts HTML form creation. You can create objects for each form field, each type of field has a class. I also have a form class which receives form field objects.

Do you guys have any other ideas of examples or exercises? Thanks!


My blog post about Objects in PHP might be useful:

http://agiletoolkit.org/blog/how-to-use-object-oriented-programming-in-php/

It gives example how to macerate "geometry" classes, triangle, vector, square etc.

When I was learning OOP, I was reading a book with similar examples in C++, but I forgot the name.


A simple exercise I've always enjoyed for getting into an object-oriented way of thinking is to take some simple real-world concept and model it into objects. These concepts can be anything:

  • A coffee maker
  • A chicken
  • A bicycle
  • etc.

It's very language-agnostic, platform-agnostic, etc. The idea is to abstract out all of the implementation details (things like PHP and HTML) and focus on object-oriented thinking. How does a model for such an object look? What are its attributes? What are its constraints? How does it behave? How does it interact with other objects?

You can effectively design your abstract types by their external observable characteristics and behaviors, their interactions, etc. and then implement those abstractions in PHP or any other language of choice. But the point is to separate out the object-oriented thinking from the specific implementation.


Some things that are usable/practical:

  • Almost everybody seems to start with a database abstraction library. Very simple to do
  • Abstraction for different caching libraries (memcache, apc, file cache)
  • Perhaps a simple router


Try a table generation class, where they receive an array and, depending on it's contents, a table is displayed.


It depends on how much they know already, if they have experience of programming in C++/C and are starting OO, then examples from Databases, forms, vector etc would work, if they have relatively little experience, then you have to start with abstract or real life examples, examples quoted in another reply

A coffee maker
A chicken
A bicycle
etc.

will work great (despite you have mentioned you do not like such examples) in understanding the concept of Object Oriented and the theory behind it. They have to see how everything works together to form a machine, how each component (class) works and interacts (public methods and interfaces) while hiding its own functionality, and providing a set of services to other components (classes).

A very popular examples teachers give is of a radio, where the user doesn't know what is inside the radio and how it works, the user only knows what it does, and the radio makes its features available to the user for use through the buttons on the panel.

These basic examples work well for beginners, and then immediately there should be a programming assignment related to it. Once they understand the basics, programming and application examples instead of abstract examples should be used in my opinion.

0

精彩评论

暂无评论...
验证码 换一张
取 消