In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article focuses on "Java reuse class example analysis", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Now let the editor to take you to learn "Java reuse class example analysis" it!
1.@Override note: when you want to override a method, the compiler generates an error message if it is not overloaded because it is inadvertently overloaded.
two。 One of the clearest ways to judge whether to use combination or inheritance is to ask yourself if you need to make an upward transition from a new class to a base class. If you have to make an upward transition, inheritance is necessary, but if not, you should consider whether you need inheritance.
3. All private methods in the class are implicitly specified as final, and since you cannot access the private method, you cannot override it, so you can add a final modifier to the private method, but this does not add any additional meaning to the method.
4. If a method is private, it is not part of the interface of the base class, it is just some program code hidden in the class (the private method belongs to the final method and can be automatically considered to be a final method, shielded from the derived class), but with the same name. However, if a public, protected, or package access method is generated in the exported class with the same method name, the method does not produce the "only the same name" situation that occurs in the base class. At this point you don't override the method, you just generate a new method. Because the private method is unreachable and effectively hidden, nothing needs to be taken into account except that it exists because of the organizational structure of the class to which it belongs.
5. When designing a class, it should be wise to name the method as final, which will keep others from overriding your method. However, it is generally difficult to foresee how classes will be reused, and if not carefully considered, it may prevent other programmers from reusing your classes through inheritance.
6. The first thing that happens when running java on Beetle is an attempt to access Beetle.main () (a static method), so the loader starts and finds the compiled code for the Bettle class (in a file called Beetle.class). During loading it, the compiler notices that it has a base class (which is known by the keyword extends), so it continues to load the base class This will happen whether you plan to generate an object of a base class or not. If the base class has its own base class, the base class of that base class will also be loaded, and so on. Eventually, the static initialization of the base class is performed, followed by the next derived class, and so on, which is important because the static initialization of the exported class may depend on whether the base class members are initialized correctly.
7. When designing a system, the goal should be to find or create classes, each of which has a specific purpose and is neither too large (too functional to reuse) nor too small (it cannot be used without adding other methods). If your design becomes too complex, it is usually helpful to add more objects by splitting the existing class into smaller parts.
8. Introduction to nouns:
Encapsulation: create new data types by merging features and behaviors.
Implementation hiding: separate the interface from the implementation by privatizing the details.
Polymorphism: used to eliminate coupling between types.
Binding: associates a method call with a method body.
Prophase binding: implemented by the compiler and linker to bind before the program is executed. It is the default binding method in process-oriented languages without selection.
Late binding: the runtime binds based on the type of object. Also known as dynamic binding or runtime binding. If a language is to implement late binding, it must have some mechanism so that it can determine the type of the object at run time and thus call the appropriate method, that is, the compiler has never known the type of the object, but the method invocation mechanism can find the correct method body and call it. The mechanism of late binding varies from programming language to programming language, but some kind of "type information" must be placed in the object anyway.
All methods in 9.Java are late-bound except for the static method and the final method (the private method belongs to the final method). So when you declare a method as final, you can not only prevent others from overriding the method, but also "turn off" dynamic binding, so the compiler can generate more efficient code for final method calls. (Java was possible in its early days, but it is no longer needed now, so it is best to decide whether to use final based on design, rather than for performance purposes.)
9. The upward transition, that is, moving up in the inheritance hierarchy, is safe because the base class does not have an interface larger than the exported class, so messages sent through the base class interface are guaranteed to be accepted.
10. The constructor of the base class is always called during the construction of the exported class and is gradually linked up according to the inheritance hierarchy so that the constructor of each base class can be called. This makes sense because the constructor has a special task to check whether the object can be constructed correctly. The exported class can only access its own members, not the members of the base class (because it is usually of type private). Only the constructor of the base class has the appropriate knowledge and permissions to initialize its own elements. Therefore, all constructors must be called, otherwise it is impossible to construct the complete object correctly, which is why the compiler forces every part of the exported class to call the constructor. In the constructor body of the exported class, if it is not explicitly specified to call a base class constructor, it will call the default constructor if there is no default constructor The compiler reports an error (if a class does not have any constructor, the compiler automatically synthesizes a default constructor).
Example:
Class Meal {Meal () {System.out.println ("Meal ()");}} class Bread {Bread () {System.out.println ("Bread ()");}} class Cheese {Cheese () {System.out.println ("Cheese ()");}} class Lettuce {Lettuce () {System.out.println ("Lettuce ()") }} class Lunch extends Meal {Lunch () {System.out.println ("Lunch ()");}} class PortableLunch extends Lunch {PortableLunch () {System.out.println ("PortableLunch ()");}} public class Sandwich extends PortableLunch {private Bread b = new Bread (); private Cheese c = new Cheese (); private Lettuce l = new Lettuce (); public Sandwich () {System.out.println ("Sandwich ()") } public static void main (String [] args) {new Sandwich ();}}
Running result:
At this point, I believe that everyone on the "Java reuse class example analysis" have a deeper understanding, might as well to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.