In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the relevant knowledge of "how to understand classes and object in java". Many people will encounter such a dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Class introduction
The Java program is composed of several classes, and the class is the concrete realization of the object-oriented programming idea.
The following is the definition of the class:
Public class User {/ / Private property private Long userId; private String name; private Integer age; / / Constructor public User () {} / / disabled constructor public User (Long userId, String name, Integer age) {this.userId = userId; this.name = name; this.age = age } / / three uses of the common method public void say () {System.out.println ("hello world");} / / wrapper attribute public String getName () {return this.name;}} keyword import
Single type import
When we need to use classes under different packages, we need to use import to import packages or classes, which can be used normally. For example, if we want to use ArrayList under java.util, we must use import java.util.ArrayList as follows:
/ / Import ArrayList class import java.util.ArrayList;class Test {public static void main (String [] args) {ArrayList list = new ArrayList ();}}
On-demand type import
If we use more classes under the same package in the same class, we use on-demand type imports.
/ / List, ArrayList and LinkedList classes under the java.util package directory are used / / import java.util.ArrayList;//import java.util.LinkedList;//import java.util.List;//. If you don't want to have too many import on the class, you can directly import the package name. * import java.util.*;public class Test {public static void main (String [] args) {List list = new ArrayList (); List list1 = new LinkedList ();}}
This is only the appearance, in fact, it is also imported one by one, but at the code level it seems to be all poured in at once.
Static import
Import can also import static methods and static domain functionality, such as the following code:
/ / * Precision Import * / / Import specific static variables, constants and methods directly. Note that the import method does not need parentheses to write the method name directly. Import static com.assignment.test.StaticFieldsClass.staticField;import static com.assignment.test.StaticFieldsClass.staticFunction;// or use the following form: / / * * Import on demand * * it is not necessary to indicate how static member names are imported one by one / / import static com.assignment.test.StaticFieldsClass.*;public class StaticTest {public static void main (String [] args) {/ / static members are written directly here without calling System.out.println (staticField) through the class name StaticFunction ();}}
The above code can also be executed smoothly, which is also a fun place for import.
Construction method
A constructor is also called a constructor or constructor, and its function is that the class calls the corresponding constructor when initializing, such as the following code:
Public class User {/ / Private property private Long userId; private String name; private Integer age; / / Constructor public User () {} / / Parametric constructor public User (Long userId, String name, Integer age) {this.userId = userId; this.name = name; this.age = age } / / Common method public void say () {System.out.println ("hello world");} public static void think () {System.out.println ("thinking");} / / wrapper attribute public String getName () {return this.name;}
Five principles of construction method
The constructor must have the same name as the class
There can be no or more parameters for the constructor
The constructor cannot define the return value (the default return type is this class type)
Each class can have one or more constructors
Constructors are always used with new operations.
Note: if the definition constructor is not shown, it defaults to adding a miserable constructor at compile time. The actual development of construction methods is usually public modification, and we want a singleton to do private modification.
Object
The Object class is a special class in Java. It is the parent class of all classes. The classes in Java inherit directly or indirectly from the Object class.
Common methods of the Object class are as follows:
Equals (): compare whether two objects are the same
GetClass (): returns the runtime class of an object
HashCode (): returns the hash code value of the object
ToString (): returns a string description of the object
Wait (): makes the current thread wait
Notify (): wakes up a single thread waiting on this object monitor
NotifyAll (): wakes up all threads waiting on this object monitor
Clone (): clone a new object
About more Object content, such as cloning (deep clone, shallow clone), several common thread methods wait, notify, notifyAll, object comparison, object hashcode value and so on.
Inherit
Only single inheritance is supported in Java: that is, a subclass can inherit only two parent classes, and a parent class can be inherited by multiple subclasses.
Everyone can only have one biological father, and a father can have more than one son.
Usage: use the extends keyword to implement class inheritance. The sample code is as follows:
Class Person {public void say () {System.out.println ("hello");}} public class User extends Person {public static void main (String [] args) {Person user = new User (); user.say ();}}
The execution result of the above program: hello
Pay attention to inheritance
Single inheritance. Multiple inheritance is not supported in Java. It is popular to say that a subclass can only have one parent class, and a parent class can have many subclasses. )
Supports multi-tier inheritance. Inheritance can be passed on forever. The child class has a parent class, and the parent class has a parent class.
If the member of the parent class is decorated with private, the subclass cannot be inherited. (private is only valid for this class)
If a subclass inherits the properties and methods of the parent class, it can also have its own unique properties and methods. (there are not only parent class properties (inheritable) and methods (inheritable), but also their own unique properties and methods. )
When the member variables of the subclass and the parent class have the same name, the subclass takes precedence. (proximity principle)
Inherit and use skills
Extract a public variable or method into a superclass
Do not use inheritance unless all methods have the meaning of inheritance
Do not change the expected behavior of the original method when the method is overridden.
Generally, when writing code, it is found that there is duplicate code in the code, so it needs to be extracted upward and inheritance should be considered.
When the design of a class is very complex, consider inheriting
The advantages of inheritance
Reusability of code.
Subclasses can be easily defined using inheritance.
The properties and methods of the parent class can be used in the subclass (not private decorated).
It becomes easier to design applications.
Heavy use in design patterns
For example: template method pattern, is to use inheritance, subclasses to implement their own business logic.
That's all for "how to understand classes and object in java". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.