From C ++ to Java - Understanding Object-Oriented is the key

zhaozj2021-02-11  244

From C to Java - Object-oriented understanding of where the key is: Zhang Yan Published: 2001/04/06

Thesis:

If you are not exiled into the island, then you will hear a potential impact of Java's appearance to developers and users. In this article, I want to emphasize that some C and Java are different. The purpose is not to teach the Java programming method, but to let the reader noticed some potential issues that may encounter from C when Java. This article will provide a concise interpretation of these concepts instead of providing some deeper or how to use problems. Remember, this is only some of the main differences raised by my experience in Java.

text:

From C to Java - Understanding Object-oriented is the key if you are not exiled into island, then you will hear a potential impact of Java's appearance to developers and users. In this article, I want to emphasize that some C and Java are different. The purpose is not to teach the Java programming method, but to let the reader noticed some potential issues that may encounter from C when Java. This article will provide a concise interpretation of these concepts instead of providing some deeper or how to use problems. Remember, this is only some of the main differences raised by my experience in Java. Java runs Java source code on a virtual machine is not compiled into ordinary machine code. Instead, it is translated into a code that the virtual machine can perform. A Java interpreter finally executes these code. This is there where the process is not connected; explains the dynamic loading of some classes when needed; as shown: Java is a full-faced object of Java is a language that is fully object-to-object. This means that your movements you do for any Java object are achieved by a method. The first thing is that there is no idea such as the main function. Instead, you must start using an object to see a program, a class object. But what is this object? Most Java programs simply achieve what they need by inheriting Java base class Object, but you can save time by creating a program base class for multiple feature similar applications. Strict object-oriented provisions means that the original C / C code cannot be used directly without modification; the system call is also the case. In C , you can use the original C process call by declaring extern "C" outside the C normal namespace, including system calls. In Java, there is only one similar way of secure backtracking, but it is not a very simple method. You must define a local method that provides an interface for the C language and then provides a connection. The Java environment provides tools for completing this task, but the entire process and the externs provided in C are slightly insignificant, and the process of completing the use of C classes is more complicated, because this will introduce the excuses of C and C functions and C-functions and C member functions. . Fortunately, many common system utilities have been provided in the system class, but these obviously do not include those and processes that have been created after many years. So, you should go to study when you need it. There is no independent header file in Java in Java, and everything about the class is put in a separate file. The location of the method can only appear in one place, and the implementation of a method must be carried out simultaneously during its definition. This is the advantage that it is not easy to fail because the file is implemented because of the non-synchronous error of the file, or gets a declaration that is not implemented. The statement of the class can be obtained by the Java interpreter or even from a compiled unit, so no longer need a header file, as long as there is a compiled file. The disadvantage of this is related to the process of our programming. Many C programmers like to use the header file instead of documentation. To see an interface parameter of a member function, just need to see the statement in the header file. You can read the header to learn how to use this class. In Java, there is no such summary. Because the code of the realization method must appear when the method is defined, and the code for a single function is often occupied and even more. In this way, it is difficult to read how the class is used by reading Java's code. You must prepare enough documents for classes you need. It goes without saying that the document is extremely lacking when dealing with non-commercial class libraries. Two tools are provided in the first Java environment to compensate for these, javap to print the class ID, Javadoc Provides an HTML document for the embedded program.

Using Package to break down a problem that Java namespace often encounter in large C project is a namespace - how to ensure that some of the programmers of the project will not create classes with other programmers? Worse, the vendor may provide a library that contains the same name as your class. There are a number of ways to solve this problem, but it is likely that the project has been started before the problem is found, and the error is needed to pay many pain. Java solves this problem with the concept of "package", and Package effectively divides namespaces through the collection class. The classes of the two are still different in different packages. The key issue has become a class to be placed in the corresponding package. Remember, Java does not solve the problem of naming conflicts. Extend a base class and causing a conflict of derived class. For example, if your favorite supplier provides some classes, then you use the class class and derive a category that provides a foo method, it may appear when the supplier provides a new version of the class, if supplies A method of foo is also provided in the new class. Abnormal is an important feature of Java in C , abnormal and abnormal processing is very profound; many C programmers have never been processed without knowing what they are. An exception is an unpredictable error that occurs during normal processes, so they do not return from the method, or as parameters; however, they cannot be ignored! An example here is a method of calculating a square root of a book. The normal interface form is to incorporate a positive as a parameter, then the method returns a positive number as a result, and the method can verify these and throw an exception when an exception is generated. In most systems, the programmer is not to do so, so that an unusual exit can be exited from normal. In Java, exception has become a very mature part of the language. The method of the method contains an abnormal information, and the program processor also enforces the test If you use a method that can produce an exception, you must check if the exception occurs. Almost all Java programmers will encounter an abnormal situation because many of the very useful libraries will throw an exception. It is not difficult to deal with abnormal treatment, but it is necessary to pay attention to it. A method of documentation indicates the type of abnormality thrown. If you forget, don't tighten, the compiler will remind you. The string is no longer the object of the character array Java, and is a constant. The string is not like a character array, although it can be simply constructed from one character array. You should use a string to replace the character array as much as possible because they will not be overwritten because of false operation. Java limits constant objects and methods in C , you can formally declare a function parameter or function return value is const type, which can effectively prevent unfair modifications to parameters or return values. Alternatively, you can declare a member function to const, indicating that it cannot modify any other object. Java supports a constant operator, read-only variable, which is implemented by Final keyword. However, Java does not support forced to make a write-written variable in the function transfer, and return to read only. Or define a constant method that does not operate the object. In Java, this omission bricles is very small in C , which is largely due to the difference between string variables and character arrays, but this also brings a hidden danger that causes errors. In particular, there is no way to verify that a method can change the object. Java does not have a pointer to understand the concept of a pointer is a problem that C or C programmers is the most difficult to cope. The pointer is also a major source of errors. There is no pointer in Java, and the handle of the object is passed directly as a parameter, not the pointer. In addition, you must use an array by an index. This is not a big problem. However, there is no pointer to cause a lot of trouble when writing a system with a function pointer or a member function pointer.

This problem is more significant when the callback function is processed. Java does not have a parameterized type parameterization type provides a method of processing many similar programs with a program. An example is a way to open a square root, which can operate int or FLOAT. In C , this feature is provided by the template. The equivalents of templates in C are not included in Java. If you often use templates to simplify programs, such as constructing a number of functions that use similar parameter types, this is a disaster. This means more use of replication, paste the process to manually complete. However, if you use templates to generate classes, there is no simple way. Java uses garbage recycling in the language of garbage collection, the runtime environment has always monitored which memory is not used. When a piece of memory is not used, the system automatically reclaims memory. For example, an object is generated in a method, but it is not called returning or not stored as global variables, and cannot be used outside the method. The system will know which variables are not available, which are available. Therefore, you don't have to worry about recovering memory for damage objects. In C , there are many debugging times to check in the inscribed holes. This method of Java has largely reduced the possibility of this error. But he still can't handle logical chaos, they can't be recycled. The destructor in many C classes is used to release the memory references. Java makes garbage recycling facts that are not required in Java. But don't mean you can forget your classification system. For example, an object opens a network connection and must be properly cleaned to close this connection. In Java, the destructor is referred to as the "Finalization" method. Java does not support multiple inheritance in any complex object-oriented system, achieving a new category that has more methods is very often encountered. For example, a Manager class requires a header of a meter, but a Manager must be an EMPLOYEE. There are many ways to deal with such problems. One method is to allow inheritance from multiple classes. In this example, Manager needs to inherit from Linked List and EMPLOYEE. Java has no multiple inheritance. But you can declare the interface - to describe programming interfaces that implement some features. A class can be implemented by multiple interfaces, including his unique features. Different classes can be implemented by the same interface. The parameters of the method can be declared as a class or as an interface. If it is an interface, the class that implements the interface can be used as a parameter incoming method. The concept of interfaces is easier to understand than many inheritance, but he has certain limitations. In particular, you must encode to re-implement the functionality when you implement the interface. Java supports multi-threaded multithreading allows you to write programs that complete multiple tasks at the same time. For example, you can allow users to edit the already read part between complete the reading of a large file. You need to divide the program into multi-threaded. for safety reasons. Your program is to be carefully designed because there may be more than one thread needs to access data, modify. Java starts support multithreading. Classs and interfaces are used to break down a program to become different threads. Language Simple Synchronize or Lock Processing for important data. Java includes some packets that are implemented from Java basic classes by some predefined classes. These allow you to write some useful programs soon, these packages are as follows: java.awt: Many applications today rely very dependent on GUI, Java provides an Abstract Window Toolkid, which allows you to do not consider running the platform Handle the GUI object. Java.applet: The main purpose of Applet is to provide browsing related content. It itself is the word class of AWT components and supports other features, such as sound, rendering, etc. Java.io :java.io provides a convection, document, and pipeline read and write operations.

转载请注明原文地址:https://www.9cbs.com/read-5013.html

New Post(0)