All properties and methods defined using the public access modifier define the class contract. That is to say the class make a promise that it will provide a defined properties and methods to any other code in the application that need them.
Class is a user-defined blueprint or prototype from which objects are created. Basically, a class combines the fields and methods into a single unit Object is an instance of a class that is created dynamically. Object is also a keyword that is an alias for the predefined type System.Object in the .NET framework.
1 : The type initializer for 'MyClass' threw an exception.
2 : The type initializer for 'MyClass' threw an exception.
3 : The type initializer for 'MyClass' threw an exception.
拋出的原因是第一次執行靜態建構子時, 因為拋出例外, 導致 Class 沒有正常初始化完成. 所以此時的程式是不存在 MyClass 這個型別(模具)的, 故之後任何使用到 MyClass 的運作都會拋出例外. (錯誤訊息為 The type initializer for 'MyClass'). 沒有模具, 何來實體 !?
Class is a syntactic i.e. A set or category of things having some property or attribute in common and differentiated from others by kind, type, or quality.
Entity is a semantic i.e. relating to meaning in language or logic. An entity is something that exists in itself, actually or potentially, concretely or abstractly, physically or not. It needs not be of material existence.
Object is a in-memory value referenced by identifier, it is an instance of a Class.
An entity usually refers to something, anything really, that has a unique and separate existence.
A class is a template for creating objects. Not all OO languages use classes (see Self, Javascript). Typically classes are implemented as objects.
An object is a bundle of data that is packaged with functions that act on that data (called methods). Calling a class's constructor allocates memory for the object and initializes its member variables.
An entity is an object that represents something that has an identity that the system is interested in tracking. Typical examples are Customers and Accounts.
Abstraction
Abstraction is the process of defining classes by simplifying reality, ignoring extraneous details, and focuing on what is important for purpose.
Encapsulation is a way to hide or encapsulation the data and implementation details whithin a clas, thus hiding complexity
Encapsulation allows the objects in the application to work together without each object knowing the details of other object's implementation
Encapsulation hides the data and implementation within the class
Protects the data
外界不能直接存取你的 data
Allows for authorization before getting the data
Allows for validation before setting the data
屬性是語法糖, 實際上是 Set or Get Method.
Help manage complexity
由類別介面看不出到底實作了什麼, 故可藉由分散實作邏輯到複數個類別
e.g. A class 有一個方法 DoA()
執行細節 - DoA 方法執行三個動作 , DoA1 , DoA2 , DoA3.
外界不知道有三個動作, 外界只在乎 DoA() 執行完後的結果.
未來可透過抽方法到外部, 降低類別的複雜度.
Only the class needs to understand the implementation
Implementation can be changed without impacting the application
Coupling
Coupling is the degree to which classes are dependent on other classes or external resources
The fewer dependencies the class is , the easier it is to wirte , test , maintain , updte over time
If there are too many dependencies in a class , consider moving some of those dependencies to another class
Low Coupling
there is a reduced probability that changes to one class adversely affects other classes ,making maintenance easier
testing more straightforward because the class has minimal dependencies on the other classes
Cohesion
Cohesion is a measure of how related everything in a class is to the purpose of the class
If there are properties or methods in a class that are not truely related to the purpose of the class , they should be moved to another class
High Cohesion
there is a higher probability that a feature request will affect only a smell number of classes , simplifying maintenance
helps produce a focused , well-defined , and complete class , making it easier to understand and test
Object-Oriented Programming
OOP is a approach to designing and building application that are Flexable/Natural/Well-Crafted/Testable by focusing on objects that interact clearly with one another
Inheritance is a feature of object-oriented programming languages that allows you to define a base class that provides specific functionality (data and behavior) and to define derived classes that either inherit or override that functionality.
Define classes that are a more specialized version of another class
A class can only have one parent class
there can be any number of inheritance levels
Only build an inheritance relationship between calsses if the derived classes have specialized properties or behaviors that require unique code and you can significantly leverage code reuse from the base class <– 繼承別亂用 (LSP)
Define a base class with common functionality , and derived class inherit from that class to reuse its functionality
Polymorphism (Inheritance-base)
Polymorphism allows us to work with groups of classes in a uniform way
Polymorphism means that the sender of a stimulus does not need to know the receiving instance’s class. The receiving instance can belong to an arbitrary class.
If an instance sends a stimulus to another instance, but does not have to be aware of which class the receiving instance belongs to, we say that we have polymorphism.