In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
What is the object-oriented feature in Java? I believe many inexperienced people don't know what to do about it. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.
Objects and classes:
1.Date t=new Date (); has two parts, new Date () constructs an object of type Date (Java objects are stored in the heap), and references to this object are stored in the object variable t. Unlike C++ references, Java object variables have no null references in C++, and the application cannot be assigned. Think of the object variable of java as the object pointer of C++. The null reference in Java corresponds to the NULL pointer in C++.
two。 A static method is a method that cannot operate on an object, so you cannot access a domain in a static method.
Use static methods in two cases:
1)。 A method does not need to access the object state, and all the required parameters are provided through formal parameters.
2)。 A method only needs to access the static domain of the class.
3. Do not return a reference to a mutable object in the getXX method. Such as:
Public Date getDate () {return date;}
Private Date date
Reason: break the encapsulation and make a variable such as newDate=A.getDate (), then newDate can modify the private domain of the Date class.
To return a reference, you should first clone: return (Date) date.clone ().
Static domain and static method
1. A static domain is also known as a class variable, that is, all instances share this variable
two。 A static method is a method that cannot operate on an object, so you cannot access the instance field in a static method, but it can access the static field in the class.
Why doesn't the NumberFormat class use the constructor to do this? Two reasons:
1)。 Cannot name constructor. The name of the constructor must be the same as the class name. But here, the currency and percentage instances you want to get use different names
2)。 When using a constructor, you cannot change the type of object being constructed. The Factory method returns a DecimalFormat class object (a subclass of NumberFormat).
Static variables, static methods, or static blocks are all executed when the class is loaded, while initialization blocks are called when the class is instantiated.
For a class to run, JVM does the following things: 1, class load 2, link 3, initialization 4, instantiation, while the initialization phase initializes static variables and executes static methods.
Method parameters (formal parameters)
A method cannot modify the parameters of a basic data type
A method can change the state of an object parameter (the parameter is an object variable).
A method cannot make an object parameter reference a new object.
Note: Java overload (the return type is not part of the method signature).
Polymorphism: an object variable can refer to multiple actual types of phenomena
Dynamic binding: the phenomenon of automatically selecting which method to call at run time.
1. For variables (whether static or instance variables), they are statically bound, which corresponds to the declared object variable type. (compile time)
two。 For methods, private, static, and final methods are statically bound, while others are dynamically bound, which corresponds to object types. (runtime) that is, Father f=new Son (); f.field and f.static_method () call the superclass, the member of Father, and to access the subclass variables, you can use the getX () and setX () methods. Other forms call members of the subclass, that is, Son.
Note: compilation fails when the superclass method is private.
3. When overriding a method, the subclass method cannot be less visible than the superclass method.
Abstract class
1. A class that contains one or more abstract methods must itself be declared as abstract
two。 In addition to abstract methods, abstract classes can also contain concrete data and concrete methods
3. When extending an abstract class, if only part of the abstract methods of the superclass are defined, the subclass should also be abstract; if all are defined, the subclass is not abstract.
4. Abstract classes cannot be instantiated. Therefore, the object variable of the abstract class cannot refer to the object of the abstract subclass.
Note: Abstract [] a=new Abstract []; / / declares an array of abstract objects, not instantiated.
Object important method
1.equals method
In Object, equals compares references (that is, memory addresses), which is equivalent to = =; so, in classes that do not override the equals method, you compare references
Classes that override the equals method generally compare the content, such as the equals method of the String class, according to the specific implementation. = = is always the address.
Refer to the following figure for more details.
2.hashCode
A hash code (hash code) is an integer value derived from an object, and it is irregular. Hash codes are generally different for different objects. If you redefine equals
Method, you must redefine the hashcode method so that the user can insert the object into the hash table. The definitions of Equals and hashCode must be consistent: if
X.equals (y) returns true, then x.hashCode () must have the same value as y.hashCode ().
Interface:
1. All methods in the interface automatically have the public property; the interface cannot contain instance fields (except for the final constant, and the constant property is automatically public static final)
Nor can you implement a method in an interface.
two。 As with inheritance, when implementing an interface, you must provide access no less than one layer above (interface or superclass). So the method must be declared as public.
3. Like abstract classes, interfaces cannot be instantiated, and interface variables can be declared but must refer to class objects that implement the interface.
Interfaces and abstract classes: each class can only extend one (abstract) class, and can implement multiple interfaces (multiple inheritance)
Clone
Clone in the Object class is the protected method and is visible to this package and all subclasses (including outside the package). But I tested it by calling the clone method with a class written by myself.
The runtime throws a CloneNotSupportedException exception and indicates that clone is a local method. Why can't you call it directly? The reasons are as follows:
The Cloneable interface must be implemented and the clone method may not be provided.
Some code examples:
Public class Son extends Father implements Cloneable {public static void main (String [] args) throws Exception {Son s=new Son (); s.A (); / / call A () of Father class, and output string "A" Son S1 = (Son) s.clone (); S1.A ();}}
Output result: an A
Shallow copy: the default clone operation, which does not clone internal objects contained in the object. But if the object in the instance field is immutable or a basic type
(there is no need to redefine the clone method, but it is recommended to redefine and call Object's clone:super.clone ())
Deep copy: on the basis of a shallow copy, clone the internal object in the object (the clone method must be redefined).
For a class to be clone, two things must be met:
*, which must implement the Cloneable interface, otherwise a CloneNotSupportedException exception will be thrown
Second, it must provide a clone method of public, that is, override the Object.clone () method, otherwise the compilation will not pass.
In addition, for classes that have variable domains, you need to copy (deep copy) those variable fields in the clone method.
Note: the Object class itself does not implement the interface Cloneable, so calling the clone method on an object whose class is Object will result in an exception being thrown at run time.
The Cloneable interface does not define any methods, it is only used as a tag, the surface is to be cloned.
Inner class:
1. Member inner class
Brief description: exists as a member of an external class, juxtaposed with the properties and methods of the external class. You can instantiate an inner class object in an external class method to access the inner class method.
The object of the inner class of the member has an implicit reference that references the external class object that instantiates the inner object. Through this pointer, you can access any domain of the external class object and
Method. However, inner classes cannot define static members (except static inner classes).
Special syntax:
External class reference expression OuterClass.this
Internal object constructor: outerObject.new InnerClass (parameters)
Note: outside the scope of the outer class, you can use OuterClass.InnerClass to refer to the inner class.
two。 Anonymous inner class
Syntax format:
New SuperType (construction parameters) {inner class methods and data}
If SuperType is an interface, the inner class will implement this interface; if SuperType is a class, the inner class will extend it.
3. Static inner class: use the inner class just to hide. You can declare the inner class as static and dereferencing the external class. You can think of it as a static member of an external class.
4. Local inner class:
Define a local class in a method. Cannot be declared with public or private access specifiers. Its scope is limited to the block that declares this local class.
And apart from this method, no other method is aware of the existence of a local inner class, but you can call the method with an external class object to access the inner class indirectly. In
Local variables of the inner class (that is, variables within the method) can be accessed in the inner class, but the variables must be final.
After reading the above, have you mastered what the object-oriented features in Java are? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
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.