C # Inner Class vs. Java Inner Class comparison

xiaoxiao2021-03-06  37

Author: leafwiz www.ASPCool.com Time: 2004-11-6 15:50:57 Views: 1811

Today, I asked why INNER CLASS in C # Inner Class is not able to access the external class's non-static members, as such a code, there is such a problem: public class testouter {public static void outer2 () {} public void outer2 () { } INTERNAL CLASS A {public voidtest () {outer (); // You can call external static methods, unable to call instance methods Outer2 (); // This sentence cannot be compiled}}} in Java, the following code can be very Normal use: public class testinner {static public void main (string [] args) {Testinner Tester = new testinner (); testinner.inner inner = testinner (); inner.testinner (); inner.testinner ();} public void test ()} Class inner {public void testinner () {test ();}}} Compared, the internal class in C # can use the type and static method defined by the external class, but cannot directly use the example method of the external class. Direct view, the external class is more like a namespace, in C #, always (as long as access control allows) Testouter.a instance = new testouter.a (); Create an instance of an internal class This instance does not have any direct relationship with any instance of the external class. Similar to static internal classes in Java. In Java, non-static internal classes can access all external classes and variables. So the construction of the internal class in Java also depends on the external class, such as: Outer.inner Inner = Outer.new Inner (); such syntax to define an instance of the internal class to ensure that he is with a certain external class The object corresponds to the object. In C #, class is divided into NESTED CLASS and NOT-NESTED CLASS, the former is a class that is declared inside other data types. The latter is a class directly defined in a certain namespace. Non-embedded categories only allow access control of public and internal, and built-in classes allow all five access controls, private, protected, internal protected. The internal class can also access all methods of the external class, including the Instance method, and private methods, but it is necessary to explicitly pass an instance of an external class.

Such as: public class testouter {public void outr2 () {} internal class a {public a (testouter obj) {{outer_this = obj} public void test () {obj.outer2 ();} Testouter Outer_this;}} C # This The reason is mainly to avoid the syntax of Outer.new, maintain a consistent object creation method. Although a new method and variable is created compared to Java, the creation process of the object is more direct, and the relationship between the implicit internal class and the external class is avoided. If we analyze the implementation level of the CLR, we can also know that the internal classes of the C # should not contain the virtual function tables of the external class, but only contain static method tables, all the external classes of the external classes of. The specific mechanism may be more complicated. However, in addition to this, the internal class is not treated as a special type, but must be created as the same mechanism with other ordinary objects. The internal class of the C # provides a capable of overwriting the implementation of the internal classes of the same name in a subclass of the class containing the internal class. Such as: Class A1 {Class B1 {}} Class A2: A1 {New class b1 {}} The internal class in C # overrides the external class of the same name. This method will be overwritten if any method name B1 () is defined in the above A1. Some of the INFORMATION for internal classes: A purpose of creating internal classes is to exist in a state of an external class, or the internal class is only in a particular context of an external class. Or hidden implementation, by setting the internal class to Private, you can set only the external class can access the class. Another important purpose of the internal class is when the external class needs to work as a particular class, and the external class has inherited to another class, because Java does not support multi-inheritance, so create a corresponding internal class as an external class. A façade is used. Typically, the motivation for creating internal classes is one or more of the above. And the most common destination is more than the first two. Let's take a look at the code of Java's processing: public class demo1 extends jPanel {class icon implements icon {}} This example corresponds to the first motivation above, ie, the motivation of a particular state or a particular implementation. ICondemo is only used in Demo1, and the specific icon is drawn.

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

New Post(0)