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)06/03 Report--
Today, I will talk to you about how to analyze the principle and usage of Java constructor, which may not be well understood by many people. in order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.
Guide reading
Constructors are powerful components of programming. Use them to unlock the full potential of Java.
In the field of open source and cross-platform programming, Java undoubtedly (? Is an undisputed heavyweight language Although there are many great cross-platform frameworks, few are as unified and straightforward as Java.
Of course, Java is also a very complex language with its own subtleties and conventions. One of the most common questions about constructor constructor in Java is: what are they and what are they for?
In short: a constructor is an operation that is performed when a new object object is created in Java. When the Java application creates an instance of the class you write, it checks the constructor. If a constructor exists, Java runs the code in the constructor when the instance is created. There are a lot of technical terms in these sentences, but it will be clearer when you see it in practice, so make sure you have Java installed and ready for demonstration.
Developers without constructors on a daily basis
If you are writing Java code, you are already using a constructor, even though you may not know it. All classes in Java have a constructor, because even if you don't create a constructor, Java will generate one for you when the code is compiled. For demonstration purposes, however, ignore the hidden constructor provided by Java (because the default constructor does not add any additional functionality) and observe that there are no explicit constructors.
Suppose you are writing a simple Java dice application because you want to generate a pseudo-random number for the game.
First, you can create a dice class to represent a dice. You played Dungeons and Dragons for a long time, so you decided to create a 20-sided dice. In this sample code, the variable dice is the integer 20, which represents the maximum number of dice that can be rolled (the number of dice on a 20-sided dice cannot exceed 20). The variable roll is the placeholder for the final random number, and rand is used as the seed of the random number.
Import java.util.Random;public class DiceRoller {private int dice = 20; private int roll; private Random rand = new Random ()
Next, create a function in the DiceRoller class to perform the steps that must be taken for computer simulation mold scrolling: take an integer from rand and assign it to the roll variable, considering that Java counts from 0 but the dice on 20 sides have no 0 value, roll adds 1, and then prints the result.
Import java.util.Random;public class DiceRoller {private int dice = 20; private int roll; private Random rand = new Random ()
Finally, generate an instance of the DiceRoller class and call its key function Roller:
/ / main looppublic static void main (String [] args) {System.out.printf ("You rolled a"); DiceRoller App = new DiceRoller (); App.Roller ();}}
As long as you have installed a Java development environment (such as OpenJDK), you can run your application on the terminal:
$java dice.javaYou rolled a 12
In this case, there is no explicit constructor. This is a very effective and legitimate Java application, but it has some limitations. For example, if you put aside the game Dungeons and Dragons and play some "speedboat dice" at night, you will need six-sided dice. In this simple example, it won't be too much trouble to change the code, but it's not a realistic choice in complex code. One way to solve this problem is to use constructors.
The function of constructor
The DiceRoller class in this sample project represents a virtual dice factory: when it is called, it creates a virtual dice and then "scrolls." However, by writing a custom constructor, you can have the dice-rolling application ask what type of dice you want to simulate.
Most of the code is the same, except that the constructor accepts a numeric parameter that represents the number of faces. This number does not exist yet, but it will be created later.
Import java.util.Random;public class DiceRoller {private int dice; private int roll; private Random rand = new Random (); / / constructor public DiceRoller (int sides) {dice = sides;}
The function to simulate scrolling remains the same:
Public void Roller () {roll = rand.nextInt (dice); roll + = 1; System.out.println (roll);}
The main part of the code provides any parameters provided when the application is run. It's true that this can be a complex application, and you need to parse the parameters carefully and check for unexpected results, but for this example, the only precaution is to convert the parameter string to an integer type.
Public static void main (String [] args) {System.out.printf ("You rolled a"); DiceRoller App = new DiceRoller (Integer.parseInt (args [0])); App.Roller ();}
Start the application and provide the number of faces you want the dice to have:
$java dice.java 20You rolled a 10$ java dice.java 6You rolled a 2$ java dice.java 100You rolled a 44
The constructor has accepted your input, so when creating an instance of the class, the sides variable is set to any number specified by the user.
Constructors are powerful components of programming. Practice using them to unlock the full potential of Java.
After reading the above, do you have any further understanding of how to analyze the principle and usage of Java constructor? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
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.