Chapter 1 Objects Object Programming Foundation
1.1 Object-oriented programming foundation
The purpose of this chapter is to guide you by object-oriented programming, understanding object-oriented importance. Many advanced languages, such as C and Microsoft Visual Basic, claiming to "support objects", but only a few languages support all principles that make up object-oriented programming. C # is such a language: it is a real face-to-object, based on the components. Therefore, you have to get as much knowledge as possible from this book, you need a good concept. I know that such concept chapters are usually skilled by the readers, they want to be coded immediately, but unless you think that you are "technology genius", I suggest you read this chapter. Those who have been programmed to objects only have a lot of people, you will get a lot of this chapter. At the same time, remember that the terms and concepts discussed in this chapter will be used in this chapter.
As I said, many languages claim to be object or objects, but very few truly. C is different, because it is undeniable, it is deeply transplanted to the C language. Too many OOP ideas must sacrifice C to meet some of the genetic properties of C. Even Java languages, although it is very good, but there are still many restrictions as object-oriented languages. For example, I mentioned that simple types and object types in Java are different processing and action. However, the focus of this chapter is not the correctness of different languages on the principle of OOP. This chapter is preferred to provide an objective and language-independent tutorial for explaining OOP principles.
Before we started, I have to say object-oriented programming is never a market sales phrase (although this is like this), a new grammar, or a new application programming interface (API). Object-oriented programming is a series of new concepts and ideas. It is a way of thinking about a problem with a computer language. It is a way to hug in a more intuitive approach and therefore also productivity.
My first job is to write a record and application of the "Ice Day holiday" box office revenue with Pascal language. When I transferred to other work and applications, I use PL / I and RPG III (and RPG / 400).
After some years, I started programming with C language. In these examples, I can easily apply my knowledge learned from previous experience. For me, a successful language learning curve is shorter, despite the complexity of the language, because until I use C programming, all other languages I have used are just the different grammar.
However, if you just contact objects to objects, I will warn you: Experience in the previous use of non-directional object language is useless! Object-oriented programming is a completely different way of thinking of solutions for designing and writing issues. In fact, research shows that the programming newcomer has learned the object-oriented language more quickly than our original process language such as Basic, Cobol and C.
These people do not need to "forget" habits of any process programming, and these will hinder their understanding of OOP. They start with a clean slate. If you use process language to program for many years, C # is your first object-oriented language, I can give you the best suggestions to keep an open mind, saying your hands, "I can deceive [Insert your procedural language] "Before, executing what I said. Anyone who has a procedure programming background will go to the object-oriented object programming, which is useful. This curve is useful. Use object-oriented language programming The income is unable to calculate, whether it is more efficiently written, or build a system that is more easier to modify and expand after the first writing. It may begin to look at it. However, it has experienced more than 20 years of software (including 8 years of object-oriented languages) showing: When the correct application is, the OOP concept is really as promising. Without how much effort, we will easily find where to come out.
1.2 All things are objects
For the object language, all problem entities are expressed through the concept of objects. (Note In this book I will define "problem domain" in this book - that is, a problem domain is the problem you try to solve, regardless of its special complexity, terms, challenges, and others, you may Guess, the object is the center idea of object-oriented programming. Most of us do not use structures, packets, functions calls, and pointers to think about problems, but through objects. Let us look at an example. If you are writing an invoice management application, you need to record the number of details of the invoice, from the customer's point of view, which method is more intuitive?
l Non-object-oriented methods I will use a data structure to represent the invoice head. The head structure of the invoice will include a double-linked list of the detailed structure of the invoice, each containing a total number of lines. So, in order to get the total number of invoices, I need to define a variable named TotalInvoiceAmount and the initial assignment is 0. Get a pointer to point to the head structure of the invoice, obtain the detailed number of header files, and then through the detailed number of link tables. When I read each detailed linear structure, I will get his member variable - including the value and increment of TotalInvoiceAmount.
l Object-based approach I have an invoice object, I sent a message to this object to ask for the entire quantity. I don't need to know how this information is stored inside, but when I handle the non-object-based data structure, it is necessary. I simply handle this object in a natural way, and ask for a message by sending a message. (The message group that the object can be sent is called the object interface. In the following paragraph, I will explain why the interface thinking is not used, as I do, it is just in an object-based approach).
Obviously, the object-based approach is more intuitive, closer to our way of thinking. In the second solution, the invoice object may be related to a series of specific objects, sending messages to each object. However, if you look at it as a whole, don't care how it is achieved because a basic feature of object-oriented programming is the ability to encapsulate-object hide internal data and methods and provide interfaces for each part. Therefore, component programming is possible. As long as the object is able to execute a task, then the inside of the execution is not important. You only need to provide an interface to the object, then use this interface to let the object to perform a given task for you. (I will explain the concept of packaging and interface later.) The program we use to imitate the specific issues of the real world will be easier to design and write because it allows us to think about problems in a more natural way.
Note that the second method requires a synthetic code that represents you complete the work. Objects contain data not only like structures. Object definitions are methods of containing data and operational data. This means that when considering a problem domain, we can design more things than the necessary data structure. We can also know which method can better combine a given object, enabling the object to be well packaged in accordance with the function. The next and subsequent sections will clarify this concept
note:
The code excerpts in this chapter show the concept of object-oriented programming. Be sure to keep in mind: I am using C # code, but their concept is OOP (object-oriented programming), which is not necessarily valid for any other programming language. For the purpose of comparison, I also used some examples of C, which is not object based.
Let us assume that you are writing a program to calculate the only employee, Amy's salary of your new company. If you use a C language, you may use something similar to the following code to contact your particular data with the employee:
Struct Employee
{
Char szfirstname [25];
Char szlastname [25];
Int IAGE;
Double DPAYRATE;
Here is how you calculate Amy's salary through the Employee structure:
void main ()
{
Double DTOTALPAY;
Struct Employee * PEMP;
PEMP = (struct Employee *) Malloc (Struct Employee);
IF (PEMP)
{
PEMP-> DPAYRATE = 100;
STRCPY (PEMP-> SZFIRSTNAME, "AMY");
STRCPY (PEMP-> SZLASTNAME, "Anderson");
PEMP-> IAGE = 28;
DTOTALPAY = PEMP-> DPAYRATE * 40;
Printf ("Total Payment for% S% S IS% 0.2F",
PEMP-> SZFIRSTNAME, PEMP-> SZLASTNAME, DTOTALPAY
}
Free (PEMP);
}
In this example, the code is based on data in a structure and some external code (relative to this structure) using this structure. So what happens? The main problem is one of the extraction: the user of the Employee structure must know too much data to an employee. why? It is assumed that you want to change the calculation method of AMY salary ratio. For example, when new net income is determined, you need to consider FICA (US Federal Social Insurance Donation Law) and other mixed tax rates. So, you must not only change all customer code that use the Employee structure, and you also need to write the change document - the programmer in your company - the fact is the change of the usage.
Let us look at the C # example
Using system;
Class Employee
{
Public Employee (String Firstname, String Lastname,
Int Age, double payrate
{
THIS.FIRSTNAME = firstName;
THIS.LASTNAME = LastName;
THIS.AGE = AGE;
This.Payrate = payrate;
}
protected string firstname;
Protected string lastname;
Protected int Age;
Protected Double PayRate;
Public Double CalculatePay (int HoursWorked)
{
// Calculate pay here.
Return (payrate * (double) Hoursworked;
}
}
Class EmployeeApp
{
Public static void main ()
{
Employee EMP = New Employee ("Amy", "Anderson", 28, 100);
Console.writeLine ("/ Namy's Pay IS $" Emp.calculatePay (40));
}
}
In the employee application example of the C #, the user simply calls the CalculatePay method of the object, allowing it to calculate its own salary. The advantage of this method is how users don't have to worry about how objects are calculated. If you decide to change the method of salary calculations, change will have no effect on existing code. This level of extraction is a basic benefit of using an object.
Now, you can have a basis for review may be: I can extract the client's C code by creating a function that accesses the Employee structure. However, the truth is that I have to create a function that is completely separated from previous institutions, which is really a headache. When you use object-oriented languages, such as C #, the object's data and method of operation (ie, interface) are usually integrated. 2002-1-13 Keep in mind that there is only the corresponding object method to modify its object variables. As previously seen, each EMPLOYEE member variable is defined as protected, except for the CalculatePay method, it is defined as public (public). Access restrictions are used to define the right to access, decide that a code must have a given class member. In the case of protected protected, the source class can access its members, but the client cannot. Public modifiers PUBLIC enables source and customer code to access the member. I will introduce the access modifier in more detail in Chapter 5, "Class", but the primary thing to remember is that the modifier makes you protect the critical class member not used by wrong.
Object VS class
The difference between the object and the class is a problem that many of the programming of these terms. In order to describe the differences between the two, we still raise an example of EMPLOYEEAPP. This time is not an employee, but an employee of the entire company.
Using C language We can define an array of employee - based on the employee Employee structure - starting again. Because we don't know how many employees have to hire a day, we can create this array with a static number, such as 10,000. However, suppose you only have an Amy an employee now, which is not the most efficient way of using resources. Conversely, in this application, we can define a linked list of an Employee structure and dynamically allocate memory.
My point is that we have done what we should not do. We spend our own energy considering language and machine - how many memory can be assigned, when to assign - not to consider fundamental issues. Use objects, we can concentrate on the logical logic of the problem instead of solving the problem.
There are many ways to define classes, which will be distinguished from the object. You can consider a simple data type (such as a character type, integer, long integer). The object is an example of a type or class. However, my favorite definition is: Class is a blueprint for the object. As a developer, you create a blueprint for engineers to create a program like a blueprint of a house. Once the blueprint is complete, you only have a blueprint for any given house. However, anyone can buy this blueprint to build the same house. Similarly, the class is a blueprint for a given functional function set, which is built in a particular class with all the features owned by all such classes.
Instantiate
As an unique clause of object-oriented programming, instantiation is an instance of simply creation, this instance is an object. In the following example, we do is to create or describe a class for objects. In other words, there is no distribution of memory because we only have a blueprint of the object, not the object itself.
Class Employee
{
Public Employee (String Firstname, String Lastname,
Int Age, double payrate
{
THIS.FIRSTNAME = firstName;
THIS.LASTNAME = LastName;
THIS.AGE = AGE;
This.Payrate = payrate;
}
protected string firstname;
Protected string lastname;
Protected int Age;
Protected Double Payrate; Public Double CalculatePay (INT Hoursworked)
{
// Calculate pay here.
Return (payrate * (double) Hoursworked;
}
}
In order to instantiate this class and use it, we must define this instance similar to the following method:
Public static void main ()
{
Employee EMP = New Employee ("Amy", "Anderson", 28, 100);
}
In this example, EMP is defined as the Employee type, instantiated by the New operator. The EMP variable represents an instance of the Employee class, is considered an EMPLOYEE object. After instantiation, we can contact this object through its public members. For example, we can call the CalculatePay method for the EMP object. If we don't have an actual object, we can't do this. (There is an exception, just when we handle static members. I will discuss static members in Chapter 5 and Chapter 6 "Method".)
Take a look at the C # code below:
Public static void main ()
{
Employee EMP = New Employee ();
Employee EMP2 = New Employee ();
}
There are two instances - EMP and EMP2 from the same EMPLOYEE class. When each object has the same performance, each instance will contain its own instance data, and can be separated separately. Similar to this, we can create a whole array or Employee object set. Chapter 7, "Attributes, Arranges, and Index" will detail the array. However, what I want to state here is that most of the object-oriented languages support the ability to define an object array. This in turn allows you to easily combine objects, by calling a method of an object or a subroutine. Compared with the work of you must handle the work of your linked list, you need to consider combining each item with the previously andiel items.
For more information, see China Soft Test League: www.cnitunion.com China Soft Test League