My Thinking In Java Learning Notes (2)

xiaoxiao2021-03-06  128

Create the visibility of the first Java program name ???????? Name problem Some people feel that it is not a big problem, I started to think that is not a big problem, but when you have written a lot of classes a day When you find that the name is actually really important. If you have such a class, he needs to call other classes. When these are the same name, the problem will appear, then ?????? ?? How do you distinguish between them and make their names not conflict? After all, we can't remember the names of each statement, so that when you declare other names next time, it is distinguished. At this time, we need to use the package (package), a package is a namespace. The rules of the namespace are: reversing the Internet domain name, for example, my blog is blog.9cbs.com/maoerzuozuo You can use package maoerzuozuo.com.9cbs.blog at the beginning of the class, so it is good ~ Each package Is a unique namespace. Need to note: When using package to explain a package, the hierarchy of the package must be the same as the structure of the file directory! I want to c: / maoerzuozuo / COM / 9CBS / BLOG? Use other components ??????? After the package is defined, be used, how to use it? In the beginning of the file, use the import keyword, if I want to import a Hello.Class component called I develop, I only need import maoerzuozuo.com.9cbs.blog.hello; introduce the package I have created, of course you can also lazy Import maoerzuozuo.com.9cbs.blog. *; The compilation speed of the package is slow. Keyword Static (static) ?????????? A class only When you use New to generate an object, his function can be used by the outside, but there are 2 in the case that the above method cannot be implemented. of. ?????????? 1, no matter how much object or no object generates objects, some specific data storage only one copy; ?????????? 2, one of the classes The function does not want to bind together, I don't want to generate objects, but I still want to use a function in the class; ?????????? Under normal circumstances, you want to use a non-static (non-static) For members, you must first create an object to use objects to call this data or function, so you have to know which object belongs to this function, because static is available without generating objects, so in static functions Functions or variables of Non-Static cannot be called directly. ?? Solve 1, putting the Static keyword before the member variable or function definition can make them static.

???????????? Class StaticDemo ??????????????? static int i = 888; ??????? ??? public static void main (string args []) ???????????????? staticDemo SD1 = new staticDemo (); ???????????? staticDemo sd2 = new staticDemo (); ????????????? system.out.println (sd1.i); // 888 ??? ??????? ??? system.out.println (sd2.i); // 888 ??????????? ??? staticDemo.i ; ????????? ? ??? system.out.println (sd1.i); // 889 ??????????? ??? system.out.println (sd2.i); // 889 ????? ????? ?????????????????} ????????????????????? N staticDemo objects, but i only one original, no matter whether you are sd1.i or sd2.i, it is the same i, all references from this staticDemo.i, so as long as you change the data stored in StaticDemo.i, These SD1 and SD2 I will change because their handles points all about STATICDEMO.I, which is equivalent to work in a company, also part-time parting in B, and he can't die. At the same time, it affects 2 companies. That is, this means, the same reason is also suitable for Static's function ??? Solution 2, if you want to generate an object, you can use functions, which need to add Static ????? before the function ????? ??? Class StaticDemo ????????????? {?????????? ???? public static void show (String S) ???? ?????? ???? {?????????? ????? system.out.println (s); ??????????????} ??????????? ???? public static void main (String args []) ?????????????? {?????????? ? ??? show ("one"); // call method 1 ??????????? ????? staticDemo.show ("two"); // call method 2 ?????? ???? ????? new staticDemo (). show ("three"); // call method 3 ?????????? ????} ????????? ? ???} ?????????? ???????? For the calling method 1, because the still static main method is also called another function in the function, there is no need to specify the object Or class ?????????????????? For the calling method 2, use the form of classname.method (), this is the typical form of STATIC's function call, Non- STATIC's function cannot be called using such a method! ??????????????????? For the calling method 3, this method is the general method of calling the function, create an object, called the object, this method For Static and Non-Static It is equally applicable.

When a member variable is declared to static, its established approach has a large change, but the function of Static changes is not large, and the STATIC function is the point where the use of the STATIC function is that the function can call the function, just like We often see the main function. Your first Java program ??????? Java program default back to the package of java.lang into the file you created, we don't have to use Import to import. The name of a public class must be the same as the file name, and a Class is only allowed to have a public Class Class, and the attorney is compiled. If you want to run this class, then you must have a main () function in this class, his fixed format is public static void main (String args []), where PUBLIC means that this is a function that is public to the outside world. And is a Static's not allowed return value, and the parameters that are incoming main () must be an array of String objects, even if you have never passed, this Args [] String array can store the parameters of the CMD. ?????????????????????????????? Compile and execute ????? Use the Javac file name .java to compile the Java file, use Java ClassName to perform classname, here should pay attention 2 points, one is when you perform a class when you use the java command, this class must have a main function; the second className is not necessarily equal to the file name, and if there is a public class in a java file, then this name must be and file The same name, other situations you can do with your own mean Note and embedded documentation ?? // Note single line /*.......*/ Note Multi-line, multi-line comments can not nested multi-line notes . ?? The formal file is in the comment ???? javadoc command can extract the document in the program code to generate a file in the HTML format, and you can view it using your browser. ?? Syntax ???? I jump over, interested friends, look at it, very simple coding style ?? When writing, the first letter should be capitalized, if the name is made up of multiple These words are written together, and their first letters are capitalized. For example, public class maoerzuozuo summary: ?? This chapter is finally Ko, huh, it is not difficult, everyone It is necessary to read the book carefully, and feel the essence of Tij. This chapter is generally not difficult. Only one question is to use the knowledge of learning, we will use him as a problem to deal with it? " A program that enables him to accept 3 quotes incorporated by the command line. To do this, you need to index the String array representing the number of command lines "?? public class solid void main (String Args []) ??? {???? system.out.println (args [0]); ???? system.out.println (args [1]); ???? system.out.println Args [2]); ???} ??} ?? This program can accept 3 quotes, and show him, some people will ask: How do we pass the quotes come in? Everyone is running, add a space behind Java Solve, plus three quotes, each with space separation, such as Java Solve AAA BBB CCC, this program will accept these three quotes, and display him.

转载请注明原文地址:https://www.9cbs.com/read-127189.html

New Post(0)