In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
Most people do not understand the knowledge points of this article "Java object-oriented characteristics and methods of use", so the editor summarizes the following contents, detailed contents, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "Java object-oriented characteristics and methods of use" article.
The difference between process-oriented and object-oriented
Process-oriented: when the event is relatively simple, use process-oriented, pay attention to the specific steps and process of the event, pay attention to the specific behavior in the process, take the function as the minimum unit, consider how to do it.
Object-oriented: focus on finding "participants", encapsulating functions into objects, emphasizing objects with functions, taking classes / objects as the smallest unit, and considering who will do it.
Case study: Xiao Ming takes things from the refrigerator
Process oriented:
Object-oriented:
Process-oriented-> object-oriented, is actually a transition by the executor-> commander.
The relationship between class and object
Everything is an object.
Object: specific things, specific entities, specific examples, specific products under the template
Class: the part that extracts the image from the object up, publishes the part, and forms the class. The class is abstract and is a template. Generally, when you write code, you write the class first, and then create the corresponding object according to the class.
A class is the abstraction of an object, and an object is the instantiation of a class.
Creation of classes and objects
1. Property (field member variable)
Properties are used to define the data or static characteristics that this class or class object contains. The scope of the property is the entire class body.
Attribute definition format:
[modifier] attribute type attribute name = [default]
two。 Method
Method is used to define the behavioral characteristics and functional implementation of this class or an instance of this class. A method is an abstraction of the behavioral characteristics of classes and objects. The method is very similar to a procedure-oriented function. In the process-oriented process, the function is the most basic unit, and the whole program is composed of function calls. In object-oriented, the basic unit of the whole program is the class, and the method is subordinate to the class and object.
Method definition format:
The [modifier] method returns the type method name (formal parameter list) {
/ / Java statement
}
Let's take the creation of Arena of Valor's hero class as an example:
/ / create heroic class public class Hero {/ / attribute String Name;// heroic name int Survive;// Survivability int Attack;// attack injury int Skill;// skill effect int Difficulty;// difficulty / / skill public void Kill (int number) {/ / release several skills System.out.println (Name+ "release" + number+ "skill!") ;} / / output the hero's attributes public void print () {System.out.println ("Hero:" + Name); System.out.println ("Survivability:" + Survive); System.out.println ("attack damage:" + Attack); System.out.println ("skill effects:" + Skill); System.out.println ("difficulty:" + Difficulty);}} object creation
Next, let's create an object from the class we created.
Public class TestCode01 {/ / main method, entry to the program public static void main (String [] args) {/ / create an object (hero)-- > Marco Polo Hero make = new Hero (); make.Name = "Marco Polo"; make.Survive = 4; make.Attack = 6; make.Skill = 6; make.Difficulty = 5 / / create another hero-- > Lan Hero lan = new Hero (); lan.Name = "Lan"; lan.Survive = 3; lan.Attack = 5; lan.Skill = 4; lan.Difficulty = 6; lan.Kill (1); / / output the attributes of two heroes make.print ()
For example, there are more than a hundred heroes in the King, each with different characteristics.
The process of creating an object:
(1) when you encounter a class for the first time, load the class only once.
(2) create objects and open up space in the heap
(3) initialize the object, and the attribute assignment is the default initial value.
(4) the new keyword calls the constructor, executes the constructor, and reassigns the attribute in the constructor.
Constructor
Objects are all from new, and the new keyword is actually calling a method called a constructor.
When you call a constructor, if there is no constructor written in your class, you will be assigned a constructor (empty constructor) by default.
Construction method format:
[modifier] Constructor name () {
}
The difference between constructors and methods:
There is no method return value
There can be no return statements inside the method body.
The name of the constructor is special and must be consistent with the class name
The purpose of calling the constructor is to assign values to attributes.
Note: we don't usually initialize in an empty constructor, because then the properties of each object will be the same.
The following is an example:
Class Hero {/ / attribute String Name;// hero name int Survive;// Survivability int Attack;// attack injury int Skill;// skill effect int Difficulty;// difficulty public Hero () {Survive=4; Attack=5; Skill=6; Difficulty=7;} public void print () {System.out.println ("Hero:" + Name) System.out.println ("Survivability:" + Survive); System.out.println ("attack damage:" + Attack); System.out.println ("skill effect:" + Skill); System.out.println ("difficulty:" + Difficulty);}} public class TestCode01 {public static void main (String [] args) {/ / create two heroic objects Hero make = new Hero () Make.Name= "Mark"; Hero lan=new Hero (); lan.Name= "Lan"; / / output two attributes make.print (); lan.print ();}}
Because we assign values in the constructor, the properties are the same when we create the object
In fact, all we have to do is to ensure the existence of the empty constructor, there is no need to write inside, we need to use the constructor assignment, we have to overload the constructor
Constructor overload
Generally, the existence of empty constructor is guaranteed, and the assignment of attributes is not generally carried out in empty constructor.
Normally, we overload the constructor and perform attribute assignment in the overloaded constructor.
After reloading the constructor, if the empty constructor forgets to write, the system will not assign you the default empty constructor, then you will make an error if you want to call it. So when we reload the constructor, we usually keep the default constructor.
When formal parameter names and attribute names are duplicated, the proximity principle appears: add this before you want to represent the properties of the object. Because this represents the object you created
The use of this
This refers to the current object.
This can modify attributes
The proximity principle occurs when attribute names and formal parameters are duplicated, or when attribute names and local variables are duplicated, so if I use the variable name directly, I mean the nearest parameter or local variable. At this time, if I want to represent the attribute, I will add: this. Modifier (if there is no duplicate name problem, you can actually omit this if you access the property.)
This modification method
In the same class, methods can call each other, this. You can omit it.
This can modify the constructor
Constructors in the same class can call each other with this. Note: the this decorating constructor must be placed on the first line
Static modification
Static can be modified: attributes, methods, code blocks, inner classes
Static decorate attribut
When the class is loaded, the static content is also loaded into the static domain of the method area, the static content exists before the object, and the static content is shared by all objects of the class.
Add the static field in the method area when the class is loaded
Exists before the object
Access method: object name. Attribute name class name. Attribute name (recommended)
Static decorated attribute application scenario: some specific data wants to be shared in memory, only one piece-- > in this case, you can use static decorated attributes.
Static modification method:
Static and public are modifiers. They are juxtaposed in no order. You can write whoever you write first and then write who you want.
The this keyword cannot be used in static methods
Non-static methods cannot be accessed in static methods
Non-static properties cannot be accessed in static methods
Static methods can use object names. The method name can also be called with the class name. Method name (recommended)
Static methods can be called directly in the same class
Code block
Classification of code blocks: ordinary blocks, construction blocks, static blocks, synchronous blocks (multithreaded)
Code block execution order: first execute static block-> then execute building block, (not commonly used)-> then execute constructor-> execute ordinary block in method again
Public class Test {/ / attribute int a; static int sa; / / method public void a () {System.out.println ("- a"); {/ / ordinary block limits the scope of local variables System.out.println ("this is a normal block"); System.out.println ("- 000000") } / / static block static {System.out.println ("- this is a static block"); / / methods can only be used in static blocks: static attributes, static method System.out.println (sa); b ();} / / constructor public Test () {System.out.println ("this is an empty constructor") } public Test (int a) {this.a = a;} / / this is a main method that is the entry to the program: public static void main (String [] args) {Test t = new Test (); T.A (); Test T2 = new Test (); T2 ();}} packet (import)
The role of the package: in order to solve the role of duplicate names, to solve the problem of permissions
Definition of package name:
Names are all lowercase
Use it in the middle. Separate
Usually the company domain name is written backwards: com.jd, com.taobao
Add the module name: com.taobao.login
Cannot use the keyword in the system: null
The location of the package declaration is usually on the first line of uncommented code
Guide package:
(1) Guide package is required to use classes under different packages, for example: import java.util.Date
(2) after guiding the package, if you want to use other classes with the same name under the package, you must manually write your own package.
(3) classes under the same package do not need a guide package and can be used directly.
(4) classes under the java.lang package can be directly used without guide packages.
(5) you can import * directly:
Static import:
/ / static import: import static java.lang.Math.*;// import: all static content in the Math class under java.lang public class Test {/ / this is a main method and is the entry to the program: public static void main (String [] args) {System.out.println (random ()); System.out.println (PI); System.out.println (round (5.6)) } / / after static import, when there are the same methods in the same class, the self-defined methods will be preferred. Public static int round (double a) {return 1000;}} above is the content of this article on "what are the object-oriented features and usage of Java". I believe you all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.
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.