c 1 introduction to programming and the c language

c 1 introduction to programming and the c language


DisciplinaFísica Básica I830 materiais10.237 seguidores
Pré-visualização50 páginas
internal:
internal class Coin
Such a class can be instantiated from classes in the same assembly (dll or exe) as the class itself. If you 
do not set visibility for a class the default is internal. Class members also have a visibility that is one of 
the following:
Download free eBooks at bookboon.com
Click on the ad to read more
C# 1 Introduction to programming and the C# language 
78 
Classes
\u2022	 public
\u2022	 private
\u2022	 internal
\u2022	 protected
\u2022	 protected internal
Preliminary I will mention only the first three. public means that a member can be referenced by methods 
in other classes, while private means that a member can only be referenced from the class\u2019s own methods. 
internal means that a member can be referenced by all methods in the same assembly. Members visibility 
can not override the type visibility. If, for example a class has internal visibility, you can not reference a 
public method from another assembly.
Both variables and methods can be assigned visibility, but usually instance variables are defined private 
(or protected). It is a principle often called data encapsulation and ensure that it is the one who writes 
the class that determines what access the outside world, have to the class\u2019s instance variables. There 
is only access to instance variables through the class\u2019s methods and properties, and thus through the 
services that the class provides. If, for example you look at the class Coin above, the variable value are 
only changed in the method Throw(), and hence we can be sure that a Coin always have a legal value. If 
the value was published, a user could write some thing like
Coin c = new Coin();
c.value = 'X';
© Agilent Technologies, Inc. 2012 u.s. 1-800-829-4444 canada: 1-877-894-4414
Teach with the Best. 
Learn with the Best.
Agilent offers a wide variety of 
affordable, industry-leading 
electronic test equipment as well 
as knowledge-rich, on-line resources 
\u2014for professors and students.
We have 100\u2019s of comprehensive 
web-based teaching tools, 
lab experiments, application 
notes, brochures, DVDs/ 
CDs, posters, and more. See what Agilent can do for you.www.agilent.com/\ufb01nd/EDUstudents
www.agilent.com/\ufb01nd/EDUeducators
Download free eBooks at bookboon.com
C# 1 Introduction to programming and the C# language 
79 
Classes
That would mean both that the object now has illegal value, but what is worse, such an error can be very 
difficult to find. Another reason to always declare a instance variable as private is that a variable in this 
way, is an internal characteristic of the class, as users of the class, in principle, does not know. In the class 
Coin has variable value the type char. If for one reason or another you want to change this type to a string:
public class Coin
{
private string value;
you can do it without affecting the applications that use the class. There are no directly refer to the 
variable value.
The conclusion is that it is a principle OOP to define instance variables private (or protected).
Comment
When you create an object of a class, the class\u2019s instance variables are created, and then the class\u2019s 
constructor are executed. If a class has no constructor, a default constructor will automatically be created, 
that is a constructor without parameters. A constructor is characterized by the fact that it is a method 
which has the same name of the class and do not have any type. A constructor is a method, but it can\u2019t be 
called explicitly and are executed only when an object are instantiated. The Coin class has a constructor, 
a default constructor, but a class may well have several constructors, as applicable to the general rules 
for overriding the methods.
Coin is a reference type, and the following statements create two objects:
Coin c1 = new Coin();
Coin c2 = new Coin();
They are as mentioned, not pushed on the stack, but are created on the heap. c1 and c2 are usual variables 
on the stack, but does not contain the objects, but instead references to the objects on the heap. It\u2019s 
rare that you as a programmer have to think about it, but in some situations it is important to know 
the difference between an object allocated on the stack and the heap. The picture below illustrate how 
it looks in the machine\u2019s memory with the two variables on the stack that refer to objects on the heap:
Coin objekt
value
Coin objekt
value
c1
c2
Download free eBooks at bookboon.com
C# 1 Introduction to programming and the C# language 
80 
Classes
If, for example you writes
c1 = c2;
this means that there is no longer any reference to the object before referring to as c1, but there are two 
references to the object referring to by c2.
Coin objekt
value
Coin objekt
value
c1
c2
If then you writes
c1.Throw();
c2.Throw();
this means that the you throw the same coin twice, because both references refer to the same object.
When there no longer are any references to an object, it also means that the object is automatically 
removed of the heap manager and the memory that the object has used, is released.
Download free eBooks at bookboon.com
Click on the ad to read more
C# 1 Introduction to programming and the C# language 
81 
Design of classes
9 Design of classes
A class is a type, but it is also a design concept. The class defines the objects in the form of instance 
variables, how objects are created and what memory that is allocated to objects. Objects have at any 
given time a value in form of the instance variables content and an object\u2019s value is usually referred to as 
its state. The class also defines in terms of its methods, what you can do with the object, and thus how 
to read and modify the object\u2019s state.
Every C# program consists of a family of classes that together define how the program would operate 
and a running program consists of a number of objects instantiated in the context of the programs 
classes, objects that work together to accomplish the thing which the program must do. The work of 
writing a program is thus to write the classes that the program will consist of. Which classes are, on 
the other hand not very clear, and the same program can typically be written in many ways built up of 
classes that are completely different. Work to determine which classes a program should consist of and 
how these classes should look like in terms of variables and methods is called design. In principle, one 
can say that if a program gets the job done correctly, it may be irrelevant, which classes it consists of, 
but inappropriate classes means
Get Help Now
Go to www.helpmyassignment.co.uk for more info
Need help with your
dissertation?
Get in-depth feedback & advice from experts in your 
topic area. Find out what you can do to improve
the quality of your dissertation!
Download free eBooks at bookboon.com
C# 1 Introduction to programming and the C# language 
82 
Design of classes
\u2022	 that it becomes difficult to understand the program and thus to identify and correct errors
\u2022	 that in the future it will be difficult to maintain and modify the program
\u2022	 that it becomes difficult to reuse the program\u2019s classes in other contexts
Therefore are design and choice of classes a very central issue in the context of programming and one are 
speaking of program quality (or lack of the same). A class is more than just a type, but it is a definition 
or description of a concept within the program area of concern. When you have to define which classes 
the program will consist of, one must therefore largely be focusing on classes as a tool to define the 
essential concepts more than on classes as a type in a programming language.
An object is characterized by four things:
\u2022	 a unique identifier that identifies a particular object in relation to all other
\u2022	 a