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/03 Report--
This article mainly introduces "Java class and object knowledge point detailed explanation", in daily operation, I believe many people have doubts on Java class and object knowledge point detailed explanation problem, small series consulted all kinds of information, sorted out simple and easy to use operation method, hope to answer "Java class and object knowledge point detailed explanation" doubts helpful! Next, please follow the small series to learn together!
directory
V. Encapsulation
(1) Concept and Creation of Package
1> Concept
2> Creation
(2) Use of packages-import packages
(3) Encapsulation definition-permission control access
(4) Supplement some commonly used packages (small expansion)
6. About static members
(1) Modified member variables-distinguish between members and static member variables
(2) Decorate member methods-invoke private variables
(3) Access to private properties
7. Code Block
(1) Common code block
(2) Construct code blocks
(3) Static code block
V. Package (1) Concept and Creation of Package 1> Concept
There are many files on our computer, and we give them different names for the convenience of management.
And then in different folders below to give them a specific division, such as my own music inside the division of musicians: Xu Song, Jay Chou, Xue Zhiqian…
And underneath each person's folder is their own song.
In Java, the same is true for project management.
In Java we have different projects, just like we roughly classify our computer files: music projects, video projects, desktop projects, etc.
Under different projects, we have different divisions---package, which is equivalent to the division of singers under the music folder: Jay Chou package, Xu Song package, Xue Zhiqian package.
Under different packages, we have a distinction-category, such as Xu Song package: "Broken Bridge Remnant Snow" category,"redundant sister" category,"Dragon Eight Division of the enemy" category and so on.
The detailed diagram is as follows:
Then, give a specific definition:
Multiple classes are collected together into a group called a package.
2> Creation
So next we create a package, open IDEA, right click src --> new --> package
Then for the next pop-up window, we named the package TestPackage
(2) Use of packages-import packages
In ordinary code, we can encounter a lot of requirements, such as asking us to find the square root of a number, such as we want to get a time, or we need to use the sorting of arrays, etc., but like these things JAVA already has specific classes to help us achieve.
Let me give you an example. For example, if we want to get a time, we can use java.util.Date to import the Date class in java.util package, but how to import it? Three methods are provided here;
1. Import at the beginning of object creation
public class TestPackage { public static void main(String[] args) { java.util.Date d1 = new java.util.Date();//See here System.out.println(d1.getTime()); }
Pros: High precision, avoiding conflicts to a large extent.
Disadvantages: Undoubtedly greatly increases the complexity of the code, so it is not recommended
2. Use import java.util.* at the beginning of your code (This means using util for all classes in the package)
import java.util.*; public class TestPackage { public static void main(String[] args) { Date d1 = new Date(); System.out.println(d1.getTime()); }}
Advantages: easy to use
Disadvantages: Conflicts may occur, as shown below:
At this time, it is impossible to distinguish whether this Date class is a class under util or a class under sql, so how to solve it? It's also very simple. Just replace this * with the specified class, and support importing in such cases.
3. Static import (preceded by static)
import static java.lang.Math.*; public class TestPackage { public static void main(String[] args) { System.out.println(sqrt(100)); System.out.println(max(1,2)); }}(3) Encapsulation Definition-Permission Control Access
With the foundation of the first two, there is a problem that we need to consider. If one class provides templates, then we can create corresponding objects in another class.
And access some properties of this class? How do we know if it can be created and accessed? Let's start with the template for the second answer:
To see whether a class and its properties and methods can be accessed or used by other classes, it depends on its permission modifiers. If the permissions given by the modifiers allow, then of course you can access them. We create the Student class and Stedent test class under the same package to perform the corresponding tests:
It can be seen that when the permission modifier is different, the situation of whether it can be accessed is also completely different. The variable modified by the private modifier cannot be prompted by IDEA, and an error will be reported if it is forcibly printed. This is an encapsulation!
A specific definition of encapsulation is given below:
An object hides some of its properties and implementation details, and access to internal data is only possible through exposed interfaces. In this way, the object provides different levels of protection against unrelated accidental changes or misuse of internal data.
(4) Supplement some commonly used packages (small expansion)
java.lang: Basic classes (String, Object) commonly used in the system, this package is automatically imported from JDK1.1.
java.lang.reflect:java reflection programming package;
java.net Web Programming Development Kit.
java.sql: Support package for database development.
java.util: is a toolkit provided by java. (Collections, etc.)
java.io:I/O Programming Development Kit.
6. About static members
In the Student class, we create each student object with its own name, age, and gender.
When these students are in the same classroom. If we want to know where these students are attending classes, then what can we do?
Create a corresponding member variable and print it? It seems impossible, because this classroom is public, not private, does not belong to a specific object, but belongs to the whole. Each object contains a copy of the member variables defined in the Student class (called instance variables) because this information is needed to describe the specific student. Now, to represent the classroom where the students are in class, the attributes of this classroom do not need to be stored in each student object, but need to be shared by all students.
Then, give the explanation of the static keyword:
In Java, static members are called static members, or class members, which do not belong to a specific object but are shared by all objects.
(1) Modified member variables-distinguish between members and static member variables
In Java classes, those that are not static are called member variables, and those that are static are called static member variables.
1> Member variables are also called instance variables because they describe the object of a class and are the embodiment of the instantiation of the object of the class. Static member variables are descriptions of classes, they belong to classes, so static member variables are also called class variables.
2> Member variables co-exist with objects, they exist as objects are created and are destroyed as objects are recycled. Class variables co-exist with classes, exist as classes are loaded, and disappear as classes disappear.
3> Member variables are stored in the heap, while static variables are special. Before JDK8, when permanent generation is not cancelled, it exists in the method area, but after JDK8 (inclusive) it also exists in the heap.
4> Member variables belong to objects, so member variables can only be called by objects. Class variables belong to classes, but objects also belong to classes, so class variables can be called by both class names and objects.
Then look at the following code to prove the above conclusion about class variables:
Watch the red!!!
(2) Decorate member methods-invoke private variables
In Java classes, those that are not static are called member methods, and those that are static are called static member methods.
.
1> Member method is the embodiment of object function, it belongs to the object, so it can only be called by the object, while static member variable belongs to the class, it is the function embodiment of the class, so it can be called by the class name, can also be called by the object
2> Static methods have no hidden this reference parameter (emphasis)
We need to discuss this second point in detail.
The first is to call static methods for non-static methods, the most common call:
And then for static method calls to non-static methods:
Because static methods have no hidden this reference parameter, you cannot access any non-static member variables or methods without creating an object.
(3) Access to private properties
When member methods of a class are set to private, how do external classes access them if they want to? Specific measures are as follows:
We add a member method and access private member variables through that member method. So how do I access this member method? Only create an object with this class as a template and access it through the object.
What if I just want to use the class name? What should I do? We should add static.
Question 1: Why do we add static to private member variables?
Answer: Because member variables can only be accessed through objects, they belong to objects, so if you don't add static to private member variables, then you can't get through creating objects anyway.
Question 2: Why static a member method that accesses a private variable?
Answer: Because only objects can access member methods, and if they are static methods, they can be called directly through class names.
7. Code Block
PS: Before writing down the code block, there is a point that needs to be noted here. For the initialization of static member variables, how can we initialize them except for the initial initialization in place? Yes, through code blocks.
So, what is a code block?
The so-called code block is a piece of code defined by { }. According to its defined position and keywords, it can be divided into the following four types.
(1) Common code block
Code blocks defined in methods
public class Persion { public static void main(String[] args) { { int a = 10; System.out.println(a); } int a = 100; System.out.println(a); }}
//Run result:
a = 10;
a = 100;
(2) Construct code blocks
A block of code in a class, without modifiers, generally used to initialize member variables, also called an instance block of code.
Q: Since this constructor block is also used to initialize member variables, what is its connection to constructors?
Let's keep going down.
As you can see, the constructor code block takes precedence over the constructor method execution, and next we open its class file.
As you can see, the compiler places the contents of the constructor block before the first statement of the constructor method when compilation is complete.
(3) Static code block
Code blocks defined using static are called static code blocks. Usually used to initialize static member variables.
From here, we can see
(1) Static code blocks are executed only once when the class is loaded, regardless of the number of objects generated.
(2) The instance code block is executed only when the object is created.
At this point, the study of "Java classes and object knowledge points" is over, hoping to solve everyone's doubts. Theory and practice can better match to help everyone learn, go and try it! If you want to continue learning more relevant knowledge, please continue to pay attention to the website, Xiaobian will continue to strive to bring more practical articles for everyone!
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.