Open chapter 1 of Java core technology: objects and classes, Chapter 2: inheritance, Chapter 2: interfaces and internal classes. Almost 150 pages of object-oriented syntax in Java are concentrated. However, with the profound skill I 've been immersed in C ++ for so many years, it seems quite easy to do so. :) this sentence is undoubtedly a boast, and I have been learning C ++ for a long time, I think people should be more and more modest. However, I still advocate more self-confidence in the boring study :)
Simple encapsulation:
The most basic object-oriented unit is class. In essence, class is only the product of combining data and methods. This combination has a certain degree of inevitability, to put it bluntly, this is the specific application of localization or divide governance in software design. It splits an overly complicated item into multiple simple items independent from each other. Localization is the best weapon to reduce complexity. As a result, software design evolved from process-oriented data structures + algorithms to object-oriented class division and class relationships, the design model is available only when the division of classes and the relationship between classes are too much. Even more, the component-oriented idea (COM) and service-oriented idea (SOA) are not in another sense. A little more nonsense. Write the simplest class directly, that is, the Data + method. In C ++, their terms are called member variables and member functions, in Java, their terms are changed to fields and methods. In Java, there is no need to distinguish between members and non-members, because it only has the concept of members.
Class Person
{
Public int getId ()
{
Return id;
}
Public String getName ()
{
Return name;
}
Public int getAge ()
{
Return age;
}
Public void setage (INT age)
{
This. Age = age;
}
Private string name; // name
Private int age; // age
Private int ID; // ID
}
The corresponding C ++ code is:
Class Person
{
Public:
Int getId ()
{
Return id;
}
String getName ()
{
Return name;
}
Int getAge ()
{
Return age;
}
Void setage (INT age)
{
This-> age = age;
}
PRIVATE:
String name; // name
Int age; // age
Int ID; // ID
};
It can be seen that in addition to the access permission declaration, Java is one-to-one, and C ++ is segmented, there is almost no difference between the two (similar to the end; small issues such as not within the scope of consideration ), in terms of usage:
Java code:
Person p = new Person ();
P. setAge (33 );
C ++ code:
Person * p = new Person ();
P-> setage (33 );
The difference is also very small. The pointer syntax is removed from Java, but the semantics of the pointer is still everywhere, but it is missing * and delete.
Let's look at the concepts of classes and objects. objects are class instances, and there is a one-to-many relationship between classes and objects, only the class name is required between the class and the class to be identified (strictly speaking, the namespace name must be added). Apart from the type difference, objects of the same type also need to be differentiated by identifiers. The concept of identifiers is also an important concept in object-oriented systems. So how to implement a class to an object is simply an extra this pointer, which should be called this reference in Java
Added inheritance:
Class inheritance is an important concept of object-oriented, which is an important method for improving the software reusability of object-oriented systems. It can be used to construct a large derived system tree with no limit to layers from parent classes to child classes, in particular, a single base class derived system like Java is basically a tree. In C ++, the base class is not restricted, so it may be multiple trees.
Java code:
Class employee extends person
{
}
C ++ code:
Class EMPLOYEE: Public Person
{
};
The difference is only about the keyword. Java does not have any restriction on the visibility of the base class. In general, the visibility control in Java is much looser than that in C ++. In addition, Java does not support multi-derivation at all, which is an important aspect simpler than C ++.
In C ++, how to implement inheritance is basically to copy the original base class to the sub-class memory layout. The implementation in Java is not very clear.
Added polymorphism:
Method polymorphism is an important concept of object-oriented, which is an important method for improving software scalability. The syntax is actually too simple. The method of the same identifier is overwritten in the base class and subclass, however, in C ++, you still need to add a virtual keyword before the method of the base class.
In C ++, the implementation of polymorphism relies on virtual function tables, and in Java, it does not need to be so troublesome because it has a complete type identification system, it is similar to a reflection. However, since all the virtual function tables can be done, is it difficult to build a complete type identification system? However, C ++ is destined to focus on memory and efficiency. It leaves the problem to the programmer and can do it on its own. So MFC built a bunch of tables and macros and virtual functions, just to implement the isA and isKindOf functions. So Qt developed the metadata. I resolved it myself and dynamically generated a new file. ATL is a thunk technology. It's really amazing. It's just dizzy. You haven't discussed it. If you're still not dizzy, we have other tricks...
Method overloading:
Methods can be reloaded. The principle is actually very simple. Because the method is identified by the function name + parameter type, the method with the same name can be reloaded. This is also a common concept in C ++, and C ++ even supports Operator overloading, which is not supported by Java. As a result, Java performs some mathematical operations, the code is terrible.
Note that Java does not have a common coverage concept in C ++:
Class person
{
Void testoverload ()
{
System. Out. println ("testoverload ()");
}
}
Class Employee extends Person
{
Public void testOverload (int I)
{
System. out. println ("testOverload (int )");
}
Public void testOverload (double r)
{
System. out. println ("testOverload (double )");
}
}
Employee e = new Employee ();
E. testOverload ();
E. testOverload (1 );
E. testOverload (1.0 );
In C ++, because the testOverload function of the subclass overwrites the function with the same name in the base class, e. testOverload () cannot be compiled at all. In C ++, overloading cannot span the scope. The purpose of the C ++ design is to ensure the simplicity of the overloading rule. It is sufficient to only search for the subclass level, you do not need to search in the parent class because the heavy load matching rules of C ++ are very complicated. This basic principle is similar to waste paper in Java. It is to continue to search in the parent class, and it may also be eligible to do so, because C ++ also supports user-defined type conversion, Java does not have that much burden, because its basic principle is simple enough.
To implement cross-scope overloading in C ++, you can use using to declare it. in Java, it seems that all of them have been declared by default.
Special methods:
Some special methods must be taken into account to make every class a little human sample:
The constructor method in Java is similar to the constructor in C ++. However, constructor in Java can call each other, set a public initialization function in C ++.
The finalize method in Java and the Destructor types in C ++ are unknown in Java because of the garbage collection mechanism, this may be the most uncomfortable place for C ++ programmers. So many resource management tricks are useless. It is really lost, but it is also the greatest gospel for new code beginners.
The equals method in Java is similar to operater = in C ++, that is, how to compare whether two objects have the same content
The clone method in Java is similar to operater = in C ++, but objects in Java are basically referenced semantics and do not have operater = semantics in C ++, therefore, the copy constructor and operater = which are very important in C ++ basically do not exist. In Java, only one object can be generated through the clone method, at last, we kept a place where we could continue to argue about deep cloning and light cloning.
Nested class and department class:
Object-oriented classes are everywhere. classes can be in another class. This is a nested class, and classes can also be in the method. This is a local class. The main purpose of using nested classes and department classes is to control names and permissions. The same concept exists in C ++.
There is no syntax declared in advance in Java, so it can be written naturally:
Class nest
{
Class AA
{
Bb B;
}
Class BB
{
}
}
In C ++, there are hundreds of errors in this code. You must write it like this:
Class Nest
{
Public:
Class BB;
Class AA
{
Bb * B;
};
Class BB
{
};
};
C ++ is subject to its name resolution mechanism and memory layout mechanism. Class BB must be declared in advance, and Class AA must have only BB pointers or references. B In Java is reference at all. Of course it is not that troublesome.
The above is still a small problem. For Nested classes and department classes, Java is extremely radical.
This pointer transmission:
Class Nest
{
Private void funNest ()
{
}
Class AA
{
Private void funAA ()
{
}
Class BB
{
Private void funBB ()
{
FunNest ();
FunAA ();
}
}
}
Public void test ()
{
AA. BB B = new AA (). new BB ();
B. funBB ();
}
}
Pay attention to code AA. bb B = new AA (). new BB (); the Java syntax here is to set nest. this, and AA. this pointer is automatically passed to BB, so BB can freely reference the fields and methods of the outer class. C ++ never allows the compiler to be so self-righteous. To implement the same function, let the programmer honestly pass the pointer of the parent class through the constructor. However, there is no problem with Java's simplification:
Class CC extends nest. AA. bb
{
}
Java cannot be compiled, because when the new CC is used to execute the default constructor of CC, the Base Class BB is also a nested class, and it needs to automatically pass the nest. this, and AA. this pointer cannot be found. The constructor of CC must be written in Java as follows:
Class CC extends nest. AA. bb
{
Public CC (nest. aa o)
{
O. Super ();
}
}
// Create an object
CC c = new CC (new nest (). New AA ());
To be honest, this code is indeed ugly, maybe it will be better to get used to it later :)
Anonymous class:
If the name of the internal class is omitted, it is an anonymous class. Without the name, the constructor cannot be mentioned. This is also a syntax for writing less code:
Employee d = new employee ()
{
Public void setage (INT age)
{
}
};
D. setage (10 );
In fact, an anonymous derived class of employee is constructed and the setage method in employee is rewritten.
Not the end:
I want to continue to discuss Java's permission control, static, final keywords and interface concepts. But I suddenly found that it is better to leave these things in the general article.