Hump naming rules: first letter lowercase, followed by the beginning of each word uppercase money (java all the methods and properties named in camel)
Pascal naming convention: the first letter capitalized, behind the beginning of each word uppercase Money (C # inside classes, methods, properties, java inside class named)
The variable type character dMoney Hungarian rule: the basis of Pascal naming rules, plus a lowercase
static: static belong to the class of all owners do not need to instantiate, only a small memory consumption during the execution of a program in memory assigned space
Non-static: have to belong to the object, you need to instantiate
Person p = new Person ();
Person p1 = new Person ();
Write a police class, there is a method of training and duty, a police officer the name of an attribute of police officers a property number, write a test class
Assignment by the police number and name call training and duty
JAVA inside strict case-sensitive
public-modified class, the class name and the file name must be exactly the same
Singleton Pattern
If a class constructor is private, outside the class can not directly call the constructor to create methods
Outside the class and can not by the object. Way to call
When a class can not be instantiated, we can only call it what method? Can only call the static method
Class class name {
Private constructor
Static public method static
Only such internal call a private constructor and the instance should return out, returned instance must be a member variable
}
Thread safety issues? For inside the static method to instantiate this variable may still exist, when multiple threads at the same time to get the object, creating multiple objects
The simplest single cases of how to achieve
public class PoliceMan {
/ / Constructor private
private PoliceMan () {
}
/ / Member variables private, static, static code is executed only once
private static PoliceMan onePolice = new PoliceMan ();
/ / Static method Total, returns an instance of the private member variables
public static PoliceMan getInstance () {
return onePolice;
}
}
没有评论:
发表评论