In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Editor to share with you an example analysis of the principles of Java classes and objects. I hope you will gain something after reading this article. Let's discuss it together.
Object-oriented and process-oriented
What is a class?
What is the object?
These are two very abstract concepts!
Before we clarify the concepts of classes and objects, let's talk about what is object-oriented and process-oriented, so as to introduce our concepts of classes and objects.
Process-oriented: take washing as an example: take the basin, put water, put clothes, put laundry powder, rub hands, change water, wring dry, dry clothes, this process is process-oriented.
Object-oriented: take washing as an example: people put clothes into the washing machine, pour in washing powder, wash and dry, do not need to care about how the whole process of washing clothes is completed, just need to find objects, create objects, and use objects. For example, when we use the toString function, we don't care about the specific implementation process of the toString function, we just give it to Arrays to help us implement it, which is object-oriented.
Where did the date come from? The object is from the class, there is a class before the object, people, washing machines, washing powder, are all a separate class. When an object is generated from a class, we call it creating an object. The objects are created, each object has some behavior and some properties, and then the objects complete each other. This is object-oriented and process-oriented.
To be object-oriented, we need to find objects, and objects come from classes. Where do classes come from? We need to be abstract. For example, we are all human beings, and people all have common attributes, such as name, sex, age, eating and sleeping, and everyone will have these, which is equivalent to a template through which a lot of objects can be generated. these objects all have these same properties. It is to abstract this person, who creates the object, and this person is a class. Really want to have such a person, let him be an object, is an entity, need to create him out, through new to create.
Define a class class Person {} / / define a class, this class is called Person, the class name must be big hump class Person {/ / the class contains fields, also known as attributes, also known as member variables / / also contains methods / / A class is a public String name;// property composed of methods and fields public int age;// attribute / / what is a method? Can be interpreted as this person's behavior public void eat () {System.out.println (name+ "eating");} public void sleep () {System.out.println (name+ "sleeping");}}
The process of generating an object from a class is called instantiation, which is instantiated through the new keyword.
Public class TestDemo {public static void main (String [] args) {Person person// defines a variable person, in which case class Person is also equivalent to type, just as the type of int a = 10Magna is int}}.
The variable person defined by the class Person is a reference variable, meaning that its initial value can be given to null
Public class TestDemo {public static void main (String [] args) {Person person = null;}}
Instantiate object
Public class TestDemo {public static void main (String [] args) {Person person = new Person (); / / an object is instantiated by class Person}}
A class can instantiate multiple objects
Public class TestDemo {public static void main (String [] args) {Person person = new Person (); / / an object Person person1 = new Person (); Person person2 = new Person (); Person person3 = new Person (); Person person4 = new Person ();}} is instantiated by class Person.
Ordinary member variable
A field is defined outside the inner method of a class. If it is defined inside a method, it is called a local variable, and when it is defined outside a method, it is called a member variable.
How to access member variables, member variables are divided into two types: (ordinary member variables, static member variables). Ordinary member variables are accessed by reference.
Member variables have a default value when there is no initial value assigned to them.
How to assign values to member variables
If it is an ordinary member variable, its memory is in the object, not that all objects share one. Each object has its own name and age
There are also two kinds of methods: one is called ordinary member method, and the other is called static member method.
The same is true for ordinary member methods, accessing my methods through references to objects.
Static member variable
Variables modified by static are called static member variables, also known as class variables, which are placed in the method area
Static member variables are accessed through the class name. Static member properties / methods.
Do not depend on objects
Class Person {public String name; public int age; public static int count; public void eat () {System.out.println (name+ "eating");} public void sleep () {System.out.println (name+ "sleeping");}} public class TestDemo {public static void main (String [] args) {Person.count++; System.out.println (Person.count) System.out.println ("="); Person.count++; System.out.println (Person.count);}}
Print the result
Count is a static member variable modified by static, which is placed in the method area, does not belong to an object, and belongs to a class. Correct access to static member variables or methods as long as the class name. Static member variables / methods are fine, and only one copy of all static things is stored in the method area, so adding 2 times is the count itself, so the second print result is 2.
Static variables cannot be defined internally in ordinary methods.
The variable defined by 1.static is a class variable, which belongs to the class. (the definition belongs to the method inside the method, so it is wrong.)
The call to the 2.eat method requires a corresponding reference. However, if the variable of static can be defined, the class Person can be called, so the two are directly contradictory, and eat needs a corresponding reference to call. Size can be called only by classes. So it is not possible to define static variables within a common method.
Static methods cannot define static variables.
The variable defined by static is a class variable and belongs to the class. (the definition inside the method belongs to the method, which is impossible, so it is wrong.)
Summary: static member variables cannot be defined in a method
Static methods can be called in ordinary methods.
Class Person {public String name; public int age; public static int count; public void eat () {System.out.println (name+ "eating");} public void sleep () {staticFunc (); System.out.println (name+ "sleeping");} public static void staticFunc () {System.out.println ("static::func") }} public class TestDemo {public static void main (String [] args) {Person person = new Person (); person.eat (); person.sleep ();}}
Print the results:
Normal methods cannot be called inside static methods
Static internal methods cannot be called, because static methods do not depend on the object and can be called through the class name, but ordinary methods cannot be called without the object.
Why the main function is static
A reference cannot point to multiple objects at the same time
Are references sure to be on the stack? Definitely not.
Look at the green line, draw and analyze.
Lowercase person belongs to the member variable of the class TestDemo
Final modification
Private implementation encapsulation
The keywords private/ public mean "access control"
Member variables or member methods modified by public can be used directly by callers of the class.
Member variables or member methods modified by private cannot be used by callers of the class.
Provide an interface
Print the results:
This represents a reference to the current object
Construction method
Constructor: the method name and class name are the same, and the constructor is special and does not return a value
What is the construction method? We need to know that the generation of an object (object instantiation) is divided into several steps.
1. Allocate memory to objects
two。 Call the appropriate constructor
This is a method of construction.
The previous access modifier can be public
Call the constructor:
Print the results:
Construction method without parameters and with one parameter and two parameters
Call the constructor:
The difference between this
The first kind:
The second kind:
The eat method that represents the current object
The third kind:
Call the constructor without parameters
Print the results:
The this function must be placed on the first line and can only be stored in the constructor
Code block
Local code block:
Two parentheses
Example code block and static code block:
No matter how many objects are generated, the static code block is executed only once and first. After the static code block is executed, the instance code block (constructor block) executes, followed by the constructor execution.
After reading this article, I believe you have some understanding of "sample Analysis of Java classes and object principles". If you 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.