今日推荐开源项目:《搭积木 tailwindcss》
今日推荐英文原文:《Can you tell me what a Class is?》
今日推荐开源项目:《搭积木 tailwindcss》传送门:GitHub链接
推荐理由:兴许在这之前你已经用过 bootstrap 这样的 CSS 框架了——它为你提供了不少 UI 组件让你直接拿来用,只需要一些微小的改动。但是如果你希望自己整一个有自己风格的网页的话,这个 CSS 框架会适合你,它并没有提供 UI 组件,但是给了你很多非常小的类来调整各种细节,你要做的就是把它们像搭积木一样组合起来,直到你觉得满意为止,这就是它最大的不同之处。
今日推荐英文原文:《Can you tell me what a Class is?》作者:Rob Parker
原文链接:https://medium.com/@into.the.parker/can-you-tell-me-what-a-class-is-61d8f61e284a
推荐理由:关于类的定义和工作方式
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…
Let’s take a look at a Class in JavaScript.
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.
Setters….
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.
下载开源日报APP:https://openingsource.org/2579/
加入我们:https://openingsource.org/about/join/
关注我们:https://openingsource.org/about/love/