OOP_03
Just a note
I'm going to read Wiki
of Object-oriented programming today. Hope to gain some valuable knowledge for answering all questions that I collected in #OOP_02.
(I also added wiki knowledge about 4 features of OOP in #OOP_01)
Đọc hiểu
Class Vs Instance
- Class variables – belong to the class as a whole; there is only one copy of each one
- Instance variables or attributes – data that belongs to individual objects; every object has its own copy of each one
- Member variables – refers to both the class and instance variables that are defined by a particular class
- Class methods – belong to the class as a whole and have access only to class variables and inputs from the procedure call
- Instance methods – belong to individual objects, and have access to instance variables for the specific object they are called on, inputs, and class variables
Prob:
So, what is copy here? What really are class variables and instance variables ? And how they different with each other?
Answer:
What are the differences between class variables and instance variables in Java?
It is explained here (with an example Bicycle class with class variable numberOfBicycles and instance variables cadence, speed, gear & id):
Sometimes, you want to have variables that are common to all objects. This is accomplished with the static modifier. Fields that have the static modifier in their declaration are called static fields or class variables. They are associated with the class, rather than with any object. Every instance of the class shares a class variable, which is in one fixed location in memory. Any object can change the value of a class variable, but class variables can also be manipulated without creating an instance of the class.
A class variable (declared static) is a location common to all instances.
In the example, numberOfBicycles is a class variable (since it is declared static). There is only one such variable (i.e. location) common to all instances and to the class. So if you modify numberOfBicycles in one method, other methods would see the new value (even for different Bicycle objects)
In contrast gear & id are instance variables (because their declaration has no static modifier). Every Bicycle object has its own one. If you modify gear for some Bicycle a, and if b is another instance, modifying a.gear has no effect on b.gear
Example: [Java] (also referred in above link)
// INSTANCE VARIABLE
public class Main {
public static void main(String[] args) {
Product prod1 = new Product();
prod1.Barcode = 123456;
Product prod2 = new Product();
prod2.Barcode = 987654;
System.out.println(prod1.Barcode);
System.out.println(prod2.Barcode);
}
}
public class Product {
public int Barcode;
}
Output will be:
123456
987654
Bởi vì property Barcode
được khai báo là instance variable (không có static), nên mỗi object (instance của class) sẽ có mỗi Barcode
khác nhau. <- (its own copy)
//CLASS VARIABLE
public class Main {
public static void main(String[] args) {
Product prod1 = new Product();
prod1.setBarcode(123456); // 1
Product prod2 = new Product();
prod2.setBarcode(987654); // 2
System.out.println(prod1.getBarcode());
System.out.println(prod2.getBarcode());
}
}
public class Product {
public static int Barcode;
public int getBarcode() {
return Barcode;
}
public void setBarcode(int value){
Barcode = value;
}
}
The output will be:
987654
987654
Why?
Bời vì class variable là cố định cho mọi object của class đó (kể cả khi không có object nào) vì vậy khi in Barcode
của prod1
và prob2
phải giống nhau.
Cụ thể ở //1
biến Barcode
của riêng prod1
là 123456
. Nhưng khi //2
được set thì Barcode
của prod2
là 987654
, đồng thời Barcode
của prod1
cũng chuyển thành 987654
.
Nhưng vì sao phải có hai hàm get/set?
Vì:
Class variables are referenced by the class name itself, as in
Bicycle.numberOfBicycles
This makes it clear that they are class variables.
Note: You can also refer to static fields with an object reference like myBike.numberOfBicycles but this is discouraged because it does not make it clear that they are class variables.
Nên:
Used non-static methods to get and set the value of Barcode to be able to call it from the object and not from the class.
Prob: Class method and instance method?
Ans: Static methods, which have the static modifier in their declarations, should be invoked with the class name, without the need for creating an instance of the class, as in
ClassName.methodName(args)
Note: You can also refer to static methods with an object reference like
instanceName.methodName(args)
but this is discouraged because it does not make it clear that they are class methods.
A common use for static methods is to access static fields. For example, we could add a static method to the Bicycle class to access the numberOfBicycles static field:
public static int getNumberOfBicycles() {
return numberOfBicycles;
}
And, instance methods on the other hand require an instance of the class to exist before they can be called, so an instance of a class needs to be created by using the new keyword. Instance methods operate on specific instances of classes. Instance methods are not declared as static.
Composition over Inheritance
Still confuse, better read more in tomorrow!
Mixin
Still confuse, better read more in tomorrow!
Coupling
Still confuse, better read more in tomorrow!
Class-based vs prototype-based
Still confuse, better read more in tomorrow!
Languages related to OOP:
Simula (1967) is generally accepted as being the first language with the primary features of an object-oriented language.
Smalltalk (1972 to 1980) is another early example, and the one with which much of the theory of OOP was developed. Concerning the degree of object orientation, the following distinctions can be made:
- Languages called "pure" OO languages, because everything in them is treated consistently as an object, from primitives such as characters and punctuation, all the way up to whole classes, prototypes, blocks, modules, etc. They were designed specifically to facilitate, even enforce, OO methods. Examples: Python, Ruby, Scala, Smalltalk
- Languages designed mainly for OO programming, but with some procedural elements. Examples: Java, C++, C#, Delphi/Object Pascal, VB.NET.
- Languages that are historically procedural languages, but have been extended with some OO features. Examples: PHP, Perl, Visual Basic (derived from BASIC), MATLAB, COBOL 2002, Fortran 2003, Ada 95, Pascal.
- Languages with abstract data type support which may be used to resemble OO programming, but without all features of object-orientation. This includes object-based and prototype-based languages. Examples: JavaScript, Lua
Tham khảo:
Upcoming:
- Đọc hết 2 Wiki và các khái niệm liên quan
- Trả lơi các câu hỏi ở #OOP_02