The operations of classes in Java are essentially the same as in C + +, but some operations that are more complex but actually less useful, such as multiple inheritance, are represented here with interfaces.
1 Package HelloWorld;2 3 Public classClass {4 Private intN;5 PublicClass ()6 {7 This(1);//equivalent to public Class (1) {this.n=n;}8 }9 PublicClass (intN)Ten { One This. n=N; A } - - voidprint () the { -System. out. println (n); - } - } + - classClasstemp extends Class + { A intm; at - /* - * Initialization of classes - */ - PublicClasstemp (intN) - { in This. m=N; - } to + /* - * Add a custom Function the */ * Public voidRun () $ {Panax NotoginsengSystem. out. println ("Hello world!"); - } the + /* A * Cover the * @see helloworld.class#print () + */ - @Override $ voidprint () $ { -System. out. println (m); - } the}
The inheritance of a class needs to be represented by extends, and the override of the function needs to be declared (or not added) with @override before the function. Subclasses inherit all fields of the parent class field, non-private method. However, the constructor method is not inherited.
There are overloads in the class, that is, when the function has the same name, different functions can be distinguished by variable names. Here are two different functions.
1 void print (String str) 2 {3 System. out. println (str); 4 }5 6 void print (String str1,string str2) 7 {8 System. out. println (str1+str2); 9 }
In the compiler, the compiler automatically generates the following name for reserving space for the data that will be stored. The same function names in the code are actually not the same in the compiler.
$print _string$print_string_string
There is also a keyword--super similar to this pointer in Java. Super can be used to point to the fields, methods, and constructors of the parent class, but not to subclasses.
The similarities and differences between Super and this: 1.this points to keywords, methods contained in this class, and super is just a keyword, method, constructor that points to the parent class. 2.this pointers can be difficult to distinguish between subclasses and the parent class, including the name of the field, the names of the methods, and super allows the double-named fields and methods to assign values to each other.
3. The construction method of the subclass reduces the effort by calling the constructor of the parent class with super. 4.super when you access a keyword or method of a parent class, its permissions are protected or public, otherwise it cannot be accessed. Neither 5.this nor super can coexist with the static field.
In addition, when you use this and the super pointer, you need to place it in the first row of the method, otherwise an error occurs.
The following are some of the more programs that are listed above:
1 Package HelloWorld;2 3 Public classClass {4 Private intN;5 protected intp;6 PublicClass ()7 {8 This(1);//equivalent to public Class (1) {this.n=n;}9 }Ten PublicClass (intN) One { A This. n=N; - } - the voidprint () - { -System. out. println (n); - } + } - + classClasstemp extends Class A { at intm; - intN; - - /* - * Initialization of classes - */ in PublicClasstemp (intAintBintc) - { toSuper (a);//use Super to call the constructor of the parent class +m=b; - This. n=C; theSuper.p=c;//access to the parent class field requires the use of protected or public * } $ Panax Notoginseng /* - * Add a custom Function the */ + Public voidRun () A { theSuper.print ();//using super to call a function of the parent class +System. out. println ("Hello world!"); - } $ $ /* - * Heavy Duty - * @see helloworld.class#print () the */ - @OverrideWuyi voidprint () the { -System. out. println (m); Wu } - About}
Java Learning (iv)