First, the beginning of nagging
In the last chapter, the relative importance is to the abstract and interface-oriented programming, both of which embody the opening and closing principle.
This issue continues to review the basics in Java.
Second, study notes
(i) Inner class and Exception class
1. Inner class:
- The member variables of an inner class's outer-nested class are still valid internally, and methods within the inner class can also call methods in an outer-nested class
- class variables and class methods cannot be declared in the class body of an inner class. An inner class can be used to declare an object as a member of an outer-nested class in the class body of an outer-nested class.
- The inner class is used only by its outer-nested classes, and other classes cannot declare objects with the inner class of a class.
It is worth mentioning that the internal class compiled format, the code is as follows:
Public class A {class b{ }}
The compiled bytecode file has the following name format:
2. Anonymous class:
- Anonymous classes can inherit or override methods of the parent class
- Anonymous class must be an internal class
- Anonymous classes can access member variables and methods in an outer-nested class, and cannot declare static
- To create anonymous classes directly using the parent class construction method
3. Exception class: The General IDE tool will prompt "should be abnormal here", the system comes with the exception is not much to say. The only notable thing is that when an exception occurs in the Try section, execution ends immediately and the catch portion is executed instead.
To see how to customize the exception:
Defines an exception class that inherits the Exception class
Public class extends exception{ public String warnmess () { return "poison"; }}
throws The custom exception class in the method name that may produce the exception, and in the specific method weight Throw An instance of a custom exception
public class count {publicvoid Count (intthrows totalexception{ if(a==10) { thrownew Totalexception ();} }
Wrap A try-catch statement when using this method
Public Static void Main (string[] args) { count c=new Count (); Try { C.count (ten); Catch (totalexception e) { System.out.println (e.warnmess ()); } }
4. Assert: Assert is used in the code debugging phase to use assertions when the program is not prepared to handle errors by catching exceptions.
Define an assertion (the expression is true to continue execution, false to execute from the endpoint of the assertion)
int a=10; assert a!=10: "A cannot be 10";
The Java interpreter closes the assertion statement by default and is opened manually with the -ea command
(ii) Common practical classes
1.String:
Important methods:
String (CharA[])//constructed from a character array Public intLength ()//Get length Public BooleanEquals (String s)//do you have the same entity Public BooleanStartsWith (String s)//whether to start with S Public BooleanEndsWith (String s)//whether to end with S Public intCompareTo (String s)//Compare Public BooleanContains (String s)//Whether it contains S Public intIndexOf (String s)//index of the first occurrence of S PublicString substring (intStartPoint//Intercept PublicString Trim ()//Remove Spaces
To see a classic equals and = = question:
Public Static void Main (string[] args) { string a=new String ("1111"); String b=new string ("1111"); System.out.println (A.equals (b)); System.out.println (a= =b); A= "1111"; b= "1111"; System.out.println (A.equals (b)); System.out.println (a= =b); }
The output is true false true to see if the Equals method compares the entities of the current string object with the entity specified by the parameter, and = = compares the references .
2. Regular expression: The regular expression is very strong, is a must grasp of the big pit. The meta-character of the direct Baidu Encyclopedia. Here is an example of using regular expressions in Java:
Public Static void Main (string[] args) { String regex= "[a-za-z]+"; Stringin = "Dhasdd4alidhskjl"; if (In.matches (regex)) { System.out.println ("All English letters"); } Else { System.out.println ("Not all English letters");} }
3.date:date can get the current time, usually need to format
Public Static void Main (string[] args) { dateformat df=new simpledateformat ("Yyyy-mm-dd HH:mm:ss"); Date Date=new date (); System.out.println (Df.format (date)); }
4.Math: Encapsulates a series of mathematical methods
5.Class: The legendary Java reflection technology
Get the Apple class class with Class.forName (Sting s) first
Then use the class object to invoke the Newinstance () method to instantiate an Apple class object
Public Static void Main (string[] args) { try { Class clazz=class.forname ("Apple"); Apple Apple=(apple) clazz.newinstance (); Apple.setnumber (1); System.out.println (Apple.getnumber ()); Apple Apple2=new Apple (); Class Class1=apple2.getclass (); System.out.println (Class1.getname ()); Catch (Exception e) { e.printstacktrace (); } }
Third, the end of the nagging
Before looking at the Great God's blog has no feeling, now he began to write to find the difficulties.
So far, the main purpose of my blog is to facilitate the consolidation of their knowledge, written very scattered, belonging to a book on the content of the short plate in accordance with the ultra-concentration compression.
So there is no place to read absolutely not your question, welcome to discuss.
Summary of Java Fundamentals (II.)