Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the this keywords and singleton patterns?

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "what are this keywords and singleton patterns". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn what this keywords and singleton patterns are.

I. this keyword

1. Suppose that the int type member variable age age is defined in the class, and a (age=a) is used in the constructor, which results in poor readability, so you need to uniformly name the variables representing the age in a class and declare them as age. At this point, when the member variable, like the local variable name, causes other members of the object to be inaccessible in the constructor. The this keyword is referenced in the java program at this time

The 2.this keyword can generate a corresponding address for the object that called the method, thus obtaining a reference to the object that called the method. When a method needs to access member variables of a class, you can use a this reference to indicate the object to operate on.

3. To solve the problem of member variables and local variable names, we can use the this keyword to access the member variables of a class.

For example:

Public class Person1 {public String name;// member variable-age public int age;// member variable-age public Person1 (String name, int age) {this.name = name;// assign name attribute this.age = age;// assign age attribute} / / say () method public void say () {System.out.println ("Hello! I am" + this.name + "," + this.age + "this year.") ;} public static void main (String [] args) {/ / TODO Auto-generated method stub Person1 p1 = new Person1 ("Li Hua", 20); / / create the first object Person1 p2 = new Person1 ("nickname", 19); / / create the second object p1.say (); / / call the object's method p2.say (); / / call the object's method}}

The result of the output is:

Hello! I'm Li Hua. I'm 20 years old. Hello! I'm Xiao Ming. I'm 19 years old.

In the above code, the parameter of the constructor is defined as (String name,int age), which is a local variable, and the member variables name and age are defined in the class. If the constructor uses name and age to access the local variable, the constructor uses this.name and this.age to access the member variable.

4. Call the member method through the this keyword:

Public class Person1 {public String name;// member variable-Age public int age;// member variable-Age public Person1 (String name, int age) {this.name = name;// assigns the name attribute this.age = age;// assigns the age attribute} / / print () method public void print () {System.out.println ("*") } / / say () method public void say () {this.print (); / / call method System.out.println ("Hello! I am "+ this.name +", and I am "+ this.age +" this year. ;} public static void main (String [] args) {/ / TODO Auto-generated method stub Person1 p1 = new Person1 ("Li Hua", 20); / / create the first object Person1 p2 = new Person1 ("nickname", 19); / / create the second object p1.say (); / / call the object's method p2.say (); / / call the object's method}}

The result of the output is:

* Hello! I'm Li Hua. I'm 20 years old. * Hello! I'm Xiao Ming. I'm 19 years old.

In the above code, the member method is called through the this keyword, and a print () method is written to call the member method in say () and output. 5. The constructor is automatically called by the java virtual machine (JVM) when the object is instantiated. When the program cannot call the constructor like other methods, it can use this ([parameter 1, parameter 2, parameter n]) in the constructor to call other constructors.

For example:

Public class Person2 {public String name; public int age; / / defines the no-parameter constructor public Person2 () {System.out.println ("the no-parameter constructor is called.");} / / defines two parameterized constructors public Person2 (String name,int age) {this (); / / calls the no-parameter constructor this.name=name; this.age=age; System.out.println ("Hello! My name is "+ this.name+" this year "+ this.age+" year!);} public static void main (String [] args) {/ / TODO Auto-generated method stub Person2 p=new Person2 ("Li Hua", 19); / / instantiate Person2 object}}

The result of the output is:

The no-parameter constructor is called. Hello! My name is Li Hua. I'm 19 years old.

In the above code, after instantiating the Person2 object, two parametric constructors are called, in this method, the parameterless constructor is called, and the "parameterless constructor is called." in the instantiation of the Person2 object, the value of the parameter is passed in, and finally output "Hello! my name is Li Hua, 19 years old!" both constructors have been called.

2. Singleton model

1. What is the singleton pattern?

The singleton pattern is that there is only one instance in a class and provides a global access point to it.

two。 In single thread, according to the different time of instantiating object, the singleton mode is implemented by two classic cases: hungry Chinese singleton and lazy singleton.

3. The static private variable of the hungry Han type singleton definition class is instantiated at the same time.

A single case of hungry Han style:

Public class Singleton {/ / create an object private static Singleton singleton = new Singleton (); / / Private constructor private Singleton () {} / / provides a static method public static Singleton getInstance () {return singleton;} public static void main (String [] args) {/ / TODO Auto-generated method stub Singleton s1=Singleton.getInstance (); Singleton s2=Singleton.getInstance (); System.out.println (s1==s2) }}

The output is: true

From the above code, declare a static private class variable and instantiate it immediately, once. The private modifier is used in the constructor of the class, so that new cannot be used outside the class to create instantiated objects. Private constructors are designed to prevent external instantiation. If you want the outside of the class to get the instance object of the class, provide the getInstance () method in public to get the singleton instance.

4. A lazy singleton is to delay loading and create an instance when you need to use it, not actively.

A single case of lazy type

Public class Singleton1 {/ / declares the private variable private static Singleton1 singleton1 = null; / / Private constructor private Singleton1 () {} / / provides a static method public static Singleton1 getInstance () {/ / passive creation that returns this object. Create if (singleton1 = = null) {singleton1 = new Singleton1 ();} return singleton1 only when you really need to use it. } public static void main (String [] args) {/ / TODO Auto-generated method stub Singleton1 s1=Singleton1.getInstance (); Singleton1 s2=Singleton1.getInstance (); System.out.println (s1==s2);}}

The output is: true

From the above code, the singleton instance is delayed, and an object is instantiated for its own use only when it is really needed.

Thank you for your reading, the above is the content of "what is the this keyword and singleton pattern". After the study of this article, I believe you have a deeper understanding of what the this keyword and singleton pattern are, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report