My Thinking In Java Learning Notes (10)

xiaoxiao2021-03-06  71

Chapter 6 Repeats Classes Duplicate Code in the Process Language is just a simple copy code to achieve the purpose of repetition, and in the object-oriented Java program, the reuse of the code is mainly reflected in 2:11, in new The use of existing classes is used in Class, which is called "combination". But this reuse is only a very simple repetition to use some code functions, rather than repeatedly using its form. 2, let the new Class become a class of Class, and add new features as needed without being able to fill the original Class, which is called "inheritance".

The combined grammar is actually a large number of in the previous example, and we use the object handle in the Class is a combination!

Class compost {private string s; "compSption ()"); s = new string ("hello");} public string toString () {Return S;}} public class test { Compost C; // Object handle INT I; public void show () {system.out.println ("int =" i); system.out.println ("compStion =" c);} public static void main String args []) {test t = new test (); t.show ();}} The objects of each non-basic data type have a toString () method, which is used to convert compStion to a string, And other String adds the basic data type in the Class of the class, is initialized to the default, and the object handle will be initialized to NULL. If you want to use this handle, remember to initialize, otherwise you will be aid that the pointer is wrong! Inheritance is an extremely important part of the Java language, using keyword extends, so that automatically allows subclasses to get all the member data and functions in the parent class. All classes in Java even include yourself or the classes that will be defined are inherited from the Object class, implicit inheritance inside the compiler.

Class base {INT i = 10; public void show () {system.out.println ("base method");} public static void main (string args []) // Java allows Class to have respective copies in the same file Main () {new base (). Show ();}} Class Derived Extends Base // Inherited {public void show () // overwrites the Base's function {System.out.Println ("Derived Method"); Super .show (); // Call the BASE function} Public void newMethod () // Subclass newly added functions {system.out.println (i); // Print the data in Base} PUBLIC Static Void Main (String Args []) {derived d = new derived (); d.show (); d.newMethod ();}} The system will initialize the inherited parent class when the subclass is initialized, Java compiles The instrument calls the constructor of the parent class before calling the subclass constructor

Class base {base () {system.out.println ("base method");}} Class Derived Extends Base {Derived () {// Super (); system automatically adds to the parent class call system.out.println ("derived method");} public static void main (string args []) {derived d = new derived ();}}

If your parent class is a class of class, the compiler does not automatically call the constructor, you must use Super to call, otherwise the system will be wrong.

Class base {base (string s) {system.out.println (s);}} class derived extends base {derived () {super ("base method"); / / must be the first line statement system of constructor. Out.println ("derived method");} public static void main (string args []) {derived d = new derived ();}}

When compatible combination and inheritance, we will not only use the combination when writing CLASS, but also inherit. The choice between combination and inheritance If you just want to use both Class's features in the new class, and hide the implementation of the details, then it is preferred to use a combination. If you want to develop a special version for the existing Class, then inherit is fool. If you have both types and new categories (one) relationship, use inheritance, if it is a HAS A (one) relationship, then use the combination protected us to review the meaning of ProteXted: inheritance The subclasses of this class can access the members of the class, and other classes in a package are Friendly

One of the advantages of progressive development inheritance is to support progressive development. In this development mode, you can add a new program code in the program, but does not affect the code of the parent class, due to the relationship of inheritance, in the child All functions of the parent class can be used in the class, and any messages sent to the parent class can also be sent to the subclass.

Class base {protected void show ("Base Method");} protected void getsomene (base b) {b.show ();}} Class Derived Extends base {public static void main (String Args) []) {Derived D = new derived (); D.GetsomeOne (D);}} Note In GetSomeOne's function definition, we define him only to accept a Base class handle, but he actually accepted a subclass. Handle. Because the subclass is not very the same as the parent class, he is a function of the parent class, which is also applicable to the parent class. Of course, it also applies to subclavab. We call this kind of subclass into the parent class is called up. Transformation Upcasting

Class base {public void methodOdone () {system.out.println ("base.Methodone");} public void methodTwo () {system.out.println ("base.methodtwo");}}}}} class derived extends Void methodone () {system.out.println ("derived.methodone");} public void methodTwo () {system.out.println ("derived.methodtwo");} public void methodnew () {system.out.println ("derived.methodnew");} public static void main (string [] args) {base d = new derived (); D.Methodone (); // Although the upward transformation becomes Base, the function is called Class function, display derived.methodone D.MethodTwo (); // 同 上 //d.MethodNew (); because it has been transformed, the subclass-specific function}} is needed to transition? Subclasses are a supercharge of the parent class, so at least the function in the parent class is included in the subclass, and maybe more. However, the upward transformation will make the subclass loss and the parent class, and the up transformation must be safe because it changes from a special type to a common type. Combined VS inheritance When you need to transition, it is the best time to use inheritance.

Key Final What is Final? It is the meaning of "final", which is not to change, we are here discussing 3 Final: Data, Method, Class

Final Data Final's data is constant, constant, which is constant, which is very useful because it 1 can be a compile period constant never change. Compiling period constants can perform certain computations in compile periods, reduce the burden on the execution period, such constants must be the basic data type, use Final modification, the initial value must be given. 2, you can initialize during the execution period, but you don't want to change him again. If a data has both static and final, then he will have a storage space that cannot be changed. When Final is used for objects, Final makes the handle unchanged that it cannot refore to other objects, but the object itself can be changed, this and final data type cannot change its value. Different.

Class value {INT i = 1;} class finalData {// compile period constant Final INT i = 10; Static Final INT II = 20; // Typical constant public static final INT III = 30; // Perform period Final Int IIII = (int)); static final int = (int)) (Math.random () * 20); value v = new value (); Final Value vv = new value (); Static final value vvv = new value (); // array final int [] a = {1, 2, 3, 4, 5, 6}; public void show (String id) {system.out.println (ID ": " " IIII = " IIII ", IIII = " IIII);} public static void main (string [] args) {FinalData fd = new finalData (); //fd.i ; cannot change the value fd.vv. i ; // final object can change its object value fd.v = new value (); // Non-final for (int i = 0; i

Blank Final Final simultaneously allows the data member to declare to Final, but does not initialize, however, you must ensure that the data member is initialized before using the member data. The advantage of doing this is to ensure that the data is constant, and different FINAL data can be produced according to the different objects, with greater flexibility. Class FinalTest {Final String S; FinalTest (string s) {this.s = s; system.out.println (this.s);} public static void main (String [] args) {new finaltest ("Hellte); New FinalTest ("BYE");}}

Final quotes When the core of Final is the basic data type, its value is not allowed to change. When it is an object, the pointing point of the handle is not allowed. Final's function is used in the meaning of the final function. It is 2: 1, locked the function, not allowing the subclass to change, that is, cannot overwrite 2, let the short function increase the performance efficiency

Final and private class's function is actually a natural Final function, because you can't use the private function, of course, you can't override the function, and when you try to override him, it is actually a new function, Put Final on the private function, do not have any meaning!

Final Class When you exactly hope that the Class cannot be inherited, you need to use Final, but regardless of whether the Class is Final, the data in the class can be fina or not in initialization and Class loading java. Only if necessary, it will only load the relevant class. Generally, it is the first time to use and inherit the time. . When this time point of the Class is used, when Static is initialized, when any Static's Class member is loaded, it will initialize according to the order they appear, and only once initialization will only be initialized. Inherit and initialize the following programs show the order inheritance and loading

Class base {INT i = 10; int J; base () //6, execute Base constructor {show ("i =" i ", j =" j); j = 20;} static int m = show ("static base.m init"); // 2, performing a static member initialization to load public static int show (string s) {system.out.println (s); return 30;}} Class Derived Extends base {INT K = show ("derived.kin init"); // 7, initialization Derived () // 8 for basic data type, has completed the initialization action of the data member, start execution of the function {show ("k =" k) SHOW ("j =" j);} static int n = show ("static derived.n init"); // 3, the initialization of static members to load public static void main (String [] args) // 1. Enable DeriveD.MAIN () static method, Class Derived is used, causing loaders to start, discover extends, so load base {show ("Derived Constructor"); // 4, load, start executing Derived D = New Derived (); // 5, starts to generate objects, first generate Base object}} output / * static base.m init static derived.n = 0 derived.kinit k = 30 J = 20 * /

Summary inheritance and combinations allow you to generate new classes based on the already classes. Use a combination to repeat it to have a class to make it a part of the new class; inheritance is used for the multiplexing of the interface. Since the subclass inherits the parent class interface, it can be transformed upwards, which is extremely important. . It is recommended to use a combination, combined with elastic, inherited is flexible.

The exercise solution is actually the difficulty of this chapter, all of which is the intention of knowledge points, everyone can do it, you can do it, you will pay attention to reading! Next chapter, my notes will write to object-oriented essence - polymorphism! Thank you for your attention and support!

If you have any questions, you can contact me like this e-mail.

Molmd@163.net

/ 163.com QQ: 31349283 We can learn progress together! Welcome to my blog,

http://blog.9cbs.com/maoerzuozuo

There are more learning content inside!

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

New Post(0)