Reinforcing, interface and abnormal processing

zhaozj2021-02-11  172

Reinforcement, interface and abnormal handling 2000-12-23 · adding translation · Yesky

-------------------------------------------------- ------------------------------

Multiple inheritance and interface

Some object-oriented languages ​​allow a class to inherit from multiple base classes, while others only allow inheritance from one class, but can be inherited from several interfaces or pure abstract classes. A pure abstract class is a class that contains only pure virtual functions.

Only C supports multiple interfaces, and many programmers have a mistake. The concept of virtual base classes caused by multiple interfaces is not easy to master, but its function is powerful. C has no interface concept.

Like Object Pascal, Java has no multiple inheritance, but has fully supported multiple interfaces. The method of the interface supports polymorphism. When an interface object is required, an interface can be implemented by an object. A class can inherit or expand a base class that has multiple interfaces. E.g:

Public interface canfly {public void fly ();} public class bat extends animal imports canfly {public void fly () {// the bat flies ...}}

The introduction of Object Pascal has a similar concept similar to Java interface, but these concepts and COM are mapped very well. The interface of DEPHI 3 is separated from the inheritance of the class, but the Java class can not only inherit from the base class, but also multiple interfaces.

A strong type of object-oriented language compiles you must need all types of checks, but sometimes you must keep some types and classes of information when running the program. Based on this consideration, both Java, C and Object Pascal supports check in higher or smaller Identification or Information run types. RTTI is used for running type checks for both Identification and Information.

The C is started at the beginning that rtti is not supported, and later, add Dynamic_cast to get some type of information.

Object Pascal supports many RTTI in its visual environment. It not only supports type checks and IS or AS operations, but also produces a wider range of RTTI for each public element. In fact, the Published this keyword affects the generated RTTI information. The concept, stream mechanism, Delphi environment, and object detection of all properties depends on the RTTI. TOBJECT has a class variable that ClassName and ClassType method, ClassType returns a class variable, an instance of a special specified class.

As with Object Pascal, Java also has a base class to keep track of class information. Object's getClass () is used to return a type of class class (used to describe the object of the class), and the getName () function returns the character string of the class. You can also use InstanceOf to focus on the process of visualization environment and components.

The following example will illustrate the differences in the above three languages:

// c dog * mydog = Dynamic_cast (myanimal); // javadog mydog = (dog) myanimal; // Object pascaldog mydog: = myanimal as dog;

Unusual place

The basic idea of ​​exception handling is the simplified error code that provides a standard detection mechanism for program key. Because the abnormal content is too much, only the main elements and differences are discussed here.

C uses the throw keyword to generate an exception, the TRY keyword is used to detect the block, the catch key is used to fill in the exception handling code. An exception can be generated by an object of a certainty or derived class. C can release the stack and clear all the objects in the stack. Object Pascal uses and C similar keywords Raise, Try and Except, and have the same performance, but can't release the stack because there is no object in the stack. Of course, you can also select the Finally keyword to make the program code have been executed, and an abnormal generation is ignored. The Delphi exception class is derived from Exception.

Java uses the same keyword as C , but some behaviors and object pascal are consistent, including Finally keywords. This may be because Java uses object reference models and background garbage recycling mechanisms. The class of Java objects must be derived from the Throwable class.

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

New Post(0)