The Principles of Objects-Oriented Programs(OOP) In Java.

Object-oriented programming (OOP) is a programming paradigm in which programs are designed using 𝗰𝗹𝗮𝘀𝘀𝗲𝘀 𝗮𝗻𝗱 𝗼𝗯𝗷𝗲𝗰𝘁𝘀.

A class is a template or blueprint from which objects are made from. Classes define the properties and methods that an object can have, and objects are unique instances of a class.

𝗢𝗯𝗷𝗲𝗰𝘁-𝗼𝗿𝗶𝗲𝗻𝘁𝗲𝗱 𝗽𝗿𝗼𝗴𝗿𝗮𝗺𝗺𝗶𝗻𝗴 𝗵𝗮𝘀 𝟰 𝗺𝗮𝗶𝗻 𝗽𝗿𝗶𝗻𝗰𝗶𝗽𝗹𝗲𝘀; 𝗲𝗻𝗰𝗮𝗽𝘀𝘂𝗹𝗮𝘁𝗶𝗼𝗻, 𝗶𝗻𝗵𝗲𝗿𝗶𝘁𝗮𝗻𝗰𝗲, 𝗮𝗯𝘀𝘁𝗿𝗮𝗰𝘁𝗶𝗼𝗻, and 𝗽𝗼𝗹𝘆𝗺𝗼𝗿𝗽𝗵𝗶𝘀𝗺.

𝗘𝗻𝗰𝗮𝗽𝘀𝘂𝗹𝗮𝘁𝗶𝗼𝗻 hides internal details but exposes data & methods via a public interface, preventing unintentional changes. E.g. — a player can view a pet’s age but can’t accidentally change it. But they can run methods avail on the public interface like changing a pet’s name.

𝗜𝗻𝗵𝗲𝗿𝗶𝘁𝗮𝗻𝗰𝗲 allows classes to inherit properties and methods from other classes, making code reusable and organized. E.g. — A “SuperPet” class that extends from “Pet “and would inherit “age”, “name”, “eat”, and “speak”; while defining new behaviors like “fly”

𝗣𝗼𝗹𝘆𝗺𝗼𝗿𝗽𝗵𝗶𝘀𝗺 is a principle that enables objects to change their form by extending or overriding existing methods. E.g. A “Dog” & “Cat” class that extended from the “Pet”, shouldn’t share the same “speak” method. You’d override it to have its own logic like “woof” or “meow”

𝗔𝗯𝘀𝘁𝗿𝗮𝗰𝘁𝗶𝗼𝗻 reduces complexity by only surfacing the information needed for a given context or use case. E.g. A “Player” class doesn’t need to know how the “eat” method works in the “Pet” class, it just needs to know how to interact with it — i.e. its input & output.

原文链接:The Principles of Objects-Oriented Programs(OOP) In Java.

© 版权声明
THE END
喜欢就支持一下吧
点赞6 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容