Thinking In Java Learning Notes (2) (ZT)

xiaoxiao2021-03-06  94

Create the first Java program

?

Name visibility

???????? Name problem Some people think that it is not a big problem, I also think that is not a big problem, but when you write a lot of classes a day, you will find that the name is really true. Important, if you have such a class, he needs to call several other classes. When these are the same name, the problem will appear, then

???????? How to 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: reverse the internet domain name, for example, my blog is

Blog.9cbs.com/maoerzuozuo You can use the beginning of the class

Package maoerzuozuo.com.9cbs.blog, this is fine ~ 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! Think

C: / Maoerzuozuo / COM / 9CBS / Blog

?

Use other components

???????? After the package is defined, be sure to use, how to use it? In the beginning of the file, use the Import keyword, if I want to import a Hello.Class component, I just need

Import maoerzuozuo.com.9cbs.blog.hello; introduced the package I created, of course, you can also lazy, use

Import maoerzuozuo.com.9cbs.blog. *;

?? Import all the packages in this folder (maybe you have a package not at all), but there is a shortcoming, it is to compile the compilation speed than the direct expression.

? Keyword stat (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.

?????????? 1, some specific data storage only one copy regardless of how much objects or abrasions are generated;

?????????? 2, a function in the class does not want to bind together with objects, I don't want to generate objects, but I still want to use a function in class;

?????????? Under normal circumstances, if you want to use a non-static member, you must first generate an object to use the object to call this data or function, so you have to know this function / The data belongs to which object is only line, because static is available without generating an object, so the Non-Static function or variable cannot be invoked directly in the Static function.

?? 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 ?????????? ?????? ???????????} ???????????????????????? Now you produce n staticDemo objects, but i only original one Series, 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 the data stored in StaticDemo.i, these SD1 and SD2 I will change because they The handle of i is all about STATICDEMO.I, which is equivalent to, there is a person working in a company, and also part-time job between B, and he can't die, it will affect 2 companies. The same truth is also suitable for STATIC's function ??? Solve 2, if you want to generate an object, you can use a function, so you need to add Static in front of 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");

//

Calling method 3 ??????????????

} ?????????? ???}

?????????? ???????? For the call method 1, it is actually hidden a key, that is, this, his complete form should be

This.Show ("one"), this THIS will be learned later

??????????????????? to the calling method 2, is used

ClassName.Method (), this is the typical form of Static's function call, and 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 there must be a main () function in this class, his fixed format is

Public static void main (string args []), where the public indicates that this is a function that is public to the outside world, and is a Static's not allowed return value, and the parameter that is introduced into main () must be an array of String object, even You have never passed, this Args [] String array can store the parameters of the CMD. ??????????????????

? Compilation and execution

????? Using the JavaC file name .java to compile the Java file, use Java ClassName to perform classname, here you should pay attention to 2 points, one is when you use the java command to execute a class, 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 class name must be the same as the file name, and you can follow your own meaning.

Notes and embedded documents

?? // Note Unlink /*....... Note Multi-line, multi-line comments cannot be nested multi-line comments.

?? 寓 文 档 档 注 注

???? javadoc command to extract the document in the program code to generate a file in the HTML format, you can view it using your browser.

??

grammar

???? I jumped over this section, I have interest, I'm very simple.

Encoding style

When the class name is written, the first letter is capitalized. If the name is composed of multiple words, these words are written together, and their first letter is capitalized? For example,

Public Class Maoerzuozuo

This chapter summarizes:

?? This chapter finals KO, huh, huh ~ In fact, this chapter is not difficult, everyone is to read the book carefully, understand the essence of Tij

This chapter is a problem that this chapter is generally difficult. Only one question is to use the knowledge of learning, and we will put him as a problem.

?? "Writing a program so that he can accept 3 quotes incorporated by the command line, for this, you need to index the String array representative of the command line quotes"

?? 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 Asked: 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. When you enter the quotes and the number of received quotes, you will hold

ArrayIndexOutofboundsexception, I hope everyone pays attention!

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

New Post(0)