今日推薦英文原文：《Can you tell me what a Class is?》
推薦理由：興許在這之前你已經用過 bootstrap 這樣的 CSS 框架了——它為你提供了不少 UI 組件讓你直接拿來用，只需要一些微小的改動。但是如果你希望自己整一個有自己風格的網頁的話，這個 CSS 框架會適合你，它並沒有提供 UI 組件，但是給了你很多非常小的類來調整各種細節，你要做的就是把它們像搭積木一樣組合起來，直到你覺得滿意為止，這就是它最大的不同之處。
今日推薦英文原文：《Can you tell me what a Class is?》作者：Rob Parker
Can you tell me what a Class is?….is the one of the questions I was asked during a phone interview last week.
A Class is something I see and use almost every day when programming, but when asked to describe what a Class is, I struggled a little. Which is interesting because you would think that something you see and use everyday would be easy to describe to someone. I guess I had never really had to think, and then describe in detail, what a Class is.
In my head I understood it and that was that….but in retrospect that was very foolish of me.
So what is a Class?
In Computer Science jargon a Class is described as the following.
In object-oriented programming, a class is an extensible program-code-template for creating objects, providing initial values for state (member variables) and implementations of behavior (member functions or methods).In many languages, the class name is used as the name for the class (the template itself), the name for the default constructor of the class (a subroutine that creates objects), and as the type of objects generated by instantiating the class; these distinct concepts are easily conflated.Pulled from Wikipedia, It』s a solid explanation, and I would be comfortable to give that in response to a question asking what a Class is.
So what if the next question was, what defines the behaviour or structure of a Class…
This PhoneBook Class contains a constructor method to set the variables when the Class is instantiated and a getter and setter method.
Getter functions are meant to simply return (get) the value of an object』s private variable to the user without the user directly accessing the private variable.
Setter functions are meant to modify (set) the value of an object』s private variable based on the value passed into the setter function. This change could involve calculations, or even overwriting the previous value completely.
The behaviour of Class or its instances is defined using methods. Methods are subroutines with the ability to operate on objects or classes. These operations may alter the state of an object or simply provide ways of accessing it.
Let』s see how it all works.Firstly we need to instantiate the Class to create an instance of the Class.
The syntax for Class creation very much depends on the type of Class and the contents of its constructor method.
In my example I create a variable named 「bob」 and using the 「new」 key word I assign it an instance of Phonebook passing through a name and phone number.
Pretty cool right? So we have a variable that contains an instance of the PhoneBook Class we now can access the data stored within the object with the get methods inherited from the Class.
It only gets getter…
By using dot notation we can access the get contactName and contactNumber getter methods by adding the variable name that defined in the constructor method.
Very similar to the dot notation we used to access the data stored in our object variables we update the in same way but assign a value to the variable. This is only possible due the setter methods inherited from the Class.
Classes are awesome, and so is Object Oriented Programming. I would recommend checking out FreeCodeCamp for more content and tutorials on Classes and Objects.