+ 9
Classes. I understand the concept but i didn't quite grasp it....
Can someone please explain to me what exactly it is and how it works? Thanks Appreciate it!!!😀
11 ответов
+ 14
A class allows you to make a object with a new data type. This object contains certain data and does certainly behaviors.
For example, you could make a Card class to make card games. It would have fields for the card's Value and its Suit. It would have behaviors to retrieve its value and its suit. Every Card object is separate from one another. You could have one that's a 7 of Clubs, and another that's a 4 of Hearts.
You could make enough Card objects for a whole Deck. So in a Deck class, you could have an array of 52 Card objects. And you could have methods to initialize the Deck, or shuffle it, or deal out 5 random Cards, or something of that nature.
This Deck could be used for a Game, with a score, players, money, etc.
Basically, it's a very natural way of structuring a program, thinking of the separate pieces/classes you'll need and mapping out how they behave and interact.
+ 9
You create a class let say Fruit, where you specify that it has a color and taste. Than you can create an object of that class by using the new keyword i.e: apple, orange...
+ 7
The example here are...not very good at explaining how classes and objects are useful, just the syntax to make and use them. It trips people up a lot.
+ 5
Oh. ._. Lots of people don't really get why they're a thing so that's my fault.
+ 4
Class is the derived data type which is used to create variable of that data type
class->data type
variable-> Object
asume it hope you understand
+ 3
Just to add, classes encourage encapsulation and define a structure by which we can have interrelated classes, divide and conquer by having a class as a functional unit that then can be subclassed or if it's an interface, defines strict ways in which you can use the interface either way it's all about divide and conquer
+ 3
Class - Let's consider an example consider "Dog"
what he can do bark(),eat(),sleep() <- these r the things he can do so these are methods.Now talk about his colour,shape,breed <- these are attributes we had put these methods and attributes in a single box called class that is dog which means class is a collection of similar kind of objects & object if a class is just a remote of T.V u have to press the button then your channel is going to change similarly if dog object is 'ob' so to make our dog bark we will use ob.bark();
I hope u understand👍
+ 2
By default your programming language just knows the primitive data types: int,float, char, double, boolean etc.
But it does not know what a Room, Person, Bag, Chair, Furniture, Face, Bed, Game, Snake, House, Animal is.
So the concept of class was introduced to give you the ability to define these unknown things using the primitive data types above so that your code can now be understood by the compiler!
+ 1
a project it's formed by a lot of class, so when you run the code it runs all the classes and you can do more classes and not write a code very long
0
Thanks
I understood that before.
What's troubling me is how that works in code (mainly java).
My bad, my question wasn't clear enough
0
what do you mean???