Design analysis of the class:
1. Extract attributes as required. (nouns are almost all attributes)
2. Property privatization (private)
3. Generate setter and Getter methods
4. You can add constructors as needed.
5. Draw other methods on demand. (Verbs are almost all methods.)
6. In the method to avoid output statements, the output at the call.
Construction method.
Feature: The method name is the same as the class name. And there is no return type.
Role: Instantiate an object. Initializes the property value of the object.
If you do not create a construction method, the system adds a constructor that does nothing. Once you have customized the construction method,
The system-added construction method disappears.
Overloading of common methods:
In the same class, there are multiple methods with the same method name, different parameters (number, type, order). These methods are overloaded methods.
overloading of constructor methods:
Parameters are different.
The This keyword.
Represents an object of this class. As long as it is a class, there is an object for this class.
1. Call the properties in this class.
2. Constructor methods in this class.
3. Represents the caller object.
Note: When you use the this call to construct a method, you must place the first row of the constructed method.
string class. (Manipulation of object characters) all classes of the system band can be found in the API documentation.
Common methods:
equals (); Compares two strings for the same. Content
Public char charAt (int index): Gets the character at the specified position of the string.
public string concat (String str) connects the specified string to the end of this string.
Public boolean endsWith (string suffix) tests whether this string ends with the specified suffix.
Public boolean startsWith (string suffix) tests whether this string starts with the specified prefix.
Boolean contains (Charsequence s) returns True when and only if this string contains a sequence of specified char values.
public int indexOf (int ch) returns the index of the first occurrence of the specified character in this string, or 1 if there is no such character in the string.
public int lastIndexOf (int ch), which returns the index of the specified character at the last occurrence in this string.
public int Length () returns the length of this string.
public string Replace (char Oldchar,char Newchar) overrides
Public string[] Split (string regex): Splits the string.
public string substring (int beginindex): intercepts a string.
Public char[] ToCharArray (): Converts a character to an array of characters.
static keyword. Static, global
decorated properties. All global variables of the variable. All objects that belong to the class are public. Static memory. The
Adornment method. Represents a static method. The method can only invoke static members. The
calls static members in the class in other classes. 1. Called by the class name. 2. Object invocation. The
calls non-static members in the class in other classes. 1. Object invocation.
Note: If a method is to be called directly by the main method, it must be "public static return type (parameter list) {}" because the main method is a static method and cannot be called directly by a non-static method.
code block {}
Normal code block: {} in method. When the method is executed.
Constructs a code block: {} is defined in the class. When instantiating an object, the construction code block is called before the constructor method is called. And each instantiation of the object is executed.
For example, the public initialization information for multiple objects can be placed in the construction code block.
Static code block: {} in class but with static decoration.
is executed when the class is loaded and is executed only once.
Synchronizing code blocks: {}
privatization of construction methods
A singleton design pattern, which controls the generation of objects, produces only one object, no matter how the program executes.
Instantiated objects cannot be executed by the outside world.
1. Single-case mode. (1. A Hungry man style 2. Lazy type)
1. Privatization of construction methods.
2. Instantiate the object of the class in this example. and use the static modifier.
3. Provides a static method to return objects instantiated in this class
classpeople{/*** A single case design pattern of evil and Han*/ Private StaticPeople p=Newpeople (); Privatepeople () {} Public StaticPeople GetPeople () {returnp; }}classperson{/*** Lazy Single case design mode*/ Private StaticPerson p1; PrivatePerson () {} Public StaticPerson Getperson () {if(p1==NULL) {P1=NewPerson (); returnP1; } returnP1; }}
Java Basic Note 10