In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article shares with you the content of sample analysis of Java classes and objects. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
I. what is a class
Class (Class) is the basis of information encapsulation in object-oriented programming (OOP,Object-Oriented Programming). A class is a user-defined reference data type, also called a class type. Each class contains a description of the data and a set of functions that manipulate the data or pass messages. Instances of the class are called objects.
The essence of a class is a reference data type, similar to byte,short,int,char,long, float, double and other basic data types, except that it is a complex data type. Because it is essentially a data type, not data, it does not exist in memory and cannot be manipulated directly. It becomes operable only when it is instantiated as an object.
II. Similarities and differences between the class of Java and the structure of C language.
We can find that the classes in Java are actually very similar to the structures in C, so what's the difference between them?
1. The packaged objects are different
The structure of C language is the packaging of data. Package a bunch of conceptually interrelated data together to facilitate overall processing. The outside world can read and write these data at will.
The object of Java can be a package of data or a package of responsibilities
two。 Storage location is different.
The structure of C language can be stored on the stack or on the heap.
Objects of Java can only exist on the heap
III. Instantiation of classes and classes
Members of a class can contain the following: fields, methods, code blocks, inner classes, interfaces, etc.
It can be vividly understood that a class is a template, and the instantiation of a class is to create objects through this template (a template can create countless objects).
Declaring a class is tantamount to customizing a new type, a reference type
Declaration of class
Basic form:
Class class name {
Fields (member variables / attributes)
Method (member method)
}
The class here is the keyword of the class.
Elements in the class: attributes (inside the class, outside the method)
Methods in class: member methods
The following is an example:
Public class Dog {public String name; public int age; public void bark () {System.out.println ("woof");}
Through the above code, a dog class is created, which has two attributes: name and age, and the behavior is barking.
The public before the property is the access modifier
Member methods are not preceded by static
An instantiated object whose members follow the default value rules
Default value rule: deformation of 0
Special:
Reference type: null
Boolean:false
Char:'\ u0000' (null character)
Instantiate class Dog {public String name; public int age; public boolean are; public static int a; public void bark () {System.out.println ("woof");}} public class Practice {public static void main (String [] args) {Dog dog = new Dog (); / / instantiate an object System.out.println (dog.are); / / pass. To access}
Here, an object is instantiated through new, through the reference of the object. Member variables to access member variables, the common method through the reference of the object. Method name to call the
Static properties (static member variables)
Defined inside the class, outside the method, decorated with static
The code is as follows:
Class Dog {public String name; public int age; public boolean are; public static int a; / / static attribute public void bark () {System.out.println ("woof");}
Note: static properties still follow the default value rule
How to access:
We need to know that static member variables do not belong to the object, but to the class, so we cannot pass the reference to the object. Member variables to access, but the class name. Static properties are accessed, and static methods are accessed through class names.
As follows:
System.out.println (Dog.a)
Example of error:
System.out.println (dog.a)
Access to the static properties of a class does not require an object and can be accessed directly
Note:
Static properties exist in the method area
No matter how many objects there are in new, there is only one static property
Static methods cannot access the values of non-static data members, nor can they call non-static methods.
Static methods do not depend on objects
IV. Construction method
First of all, let's think about, how does an object come into being?
Allocate memory to objects
Call the appropriate constructor
When we use new to instantiate an object, the constructor is automatically called to complete the initialization operation
Create a construction method
Method name must be the same as class name
No return value type declaration
There is at least one constructor for each class (when undefined, the system automatically generates a no-parameter construct)
The code is as follows:
Class Dog {public String name; public int age; public boolean are; public static int a; public Dog () {} public Dog (String name) {this.name = name;} public void bark () {System.out.println (Wangwang);} public class Practice {public static void main (String [] args) {Dog dog = new Dog (Wangcai) System.out.println (dog.name);}}
There is a Dog class in this code, in which there are two constructors, one with parameters and the other with parameters. When creating an object through new, you can see the printed result by using the construction with parameters.
So in fact, you can see which constructor is called depends on the parameters passed by new.
It should be noted that:
When there is no construction method in the class, the system will automatically fill in a no-parameter construction method, but when you write the construction method yourself, you will not fill in this no-parameter construction. if you need to use no-parameter construction, you need to write it manually.
The constructor also supports overloading
This
The role of this
In many cases, someone may mistakenly think that this is the current object, but in fact this is a reference to the current object
Why this is not an object, but a reference:
We need to know that the constructor is used to create the object, so in the process of calling the constructor to create the object, the object still does not exist.
So we can access properties, member methods, and constructors through this (when a constructor is called, it can only be placed on the first line, can only be written in the constructor, and can only be used once)
Thank you for reading! This is the end of this article on "sample Analysis of Java classes and objects". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
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.