Yahoo Clever wird am 4. Mai 2021 (Eastern Time, Zeitzone US-Ostküste) eingestellt. Ab dem 20. April 2021 (Eastern Time) ist die Website von Yahoo Clever nur noch im reinen Lesemodus verfügbar. Andere Yahoo Produkte oder Dienste oder Ihr Yahoo Account sind von diesen Änderungen nicht betroffen. Auf dieser Hilfeseite finden Sie weitere Informationen zur Einstellung von Yahoo Clever und dazu, wie Sie Ihre Daten herunterladen.
can you give me an example of when Classes or Structures would...?
actually be helpful. because right now to me, they only seem to make things more complicated, perhaps thats just because of what i'm programming.
but i'm kind of having trouble understanding why exactly we need encapsulation in programming also.
4 Antworten
- vor 1 JahrzehntBeste Antwort
Hi,
Classes: reference types
Structures: value types.
Reference types are allocated on the heap, and memory management is handled by the garbage collector.
Value types are allocated on the stack or inline and are deallocated when they go out of scope.
When you can using them?
Defining a structure instead of a class if instances of the type are small and commonly short-lived or are commonly embedded in other objects.
For additional information you can visit the following page:
http://msdn.microsoft.com/en-us/library/ms229017.a...
encapsulation:
The ability of an object to hide its internal data and methods, making only the intended parts of the object programmatically accessible.
why exactly we need encapsulation in programming?
You can visit the following page:
Quelle(n): 1- http://msdn.microsoft.com/ 2- http://www.c-sharpcorner.com/ - no1home2dayLv 7vor 1 Jahrzehnt
I don't use structures any more since I moved to .NET.
A class is great because it uses the "black box" method of programming.
An object can have properties, and actions, and you don't need to know HOW the class works to be able to use it.
For instance, if you have a class that represents a vehicle (maybe you get a job with the police department, and they want to be able to keep track of all the vehicles with tickets).
A vehicle can have a MAKE, a MODEL, a LICENSE PLATE, a STATE, COLOR, NUMBER OF DOORS, etc.
The "VEHICLE" class models a real world vehicle, and the vehicle can DO things (i.e. functions and procedures) built in to the class. "MOVE FORWARD" or "OPEN DOOR", etc.
The actions that take place through the class functions are carried out, and variables that represent different conditions can change.
For instance "MOVING" can be a boolean value. If you call the "MoveForward" procedure, "Moving" is set to True, and "Location" can be changed in the "MoveForward" procedure within the "Vehicle" class.
"Lights" can be a boolean variable to represent the condition of the headlights, "on" or "off", and the procedures "LightsOn" and "LightOff" can change that value accordingly.
You can have a function called "AreLightsOn" and return the value of "Lights" (True or False).
All this is done within the class and the person who uses this class doesn't need to know anything about the internal structure or function of the class to be able to use the "Vehicle"
To use the "Vehicle" class in Visual Basic, you would use this line of code:
DIM Ford as Vehicle = New Vehicle()
Then, something like this:
If Ford.ParkingBreak is True THEN Ford.NotMoving
Or within the class itself, in the "MoveForward" procedure, you can have something like:
If ParkingBreak is True then Sxit Sub
Then continue with the rest of the procedure to simulate what ever you want to do with in the "MoveForward" procedure.
Does any of this help?
One last item is something called "Inheritence"
You can create a basic "Vehicle" class, then create specific classes based on the "Vehicle" class, that has all the attributes of the "Vehicle" class, but then you can add functionality to the new class.
There are things you can do with classes that you can NEVER do in the main program that doesn't use classes.
Also, an Object can include another Object.
A Car can include the Door Class, the Windows Class, and even the Seats class.
- NeunerballLv 4vor 1 Jahrzehnt
Encapsulation:
You use Encapsulation to keep the parts of your code that stay the same separate from the parts that change. With that it's easy to make changes to your code without breaking everything.
OOP (Object-Oriented Programming):
- vor 1 Jahrzehnt
I recall an example in XNA (C# Game Engine) where we created a structure to stores the main players stats (variables). Thats all I remember......