First, object-oriented
three main features of object-oriented:1. Encapsulation: Privatization of class properties and provision of public getset methods to the outside2. Inheritance: Inheriting existing classes with a new class can directly use the public methods and properties of existing classes3. Polymorphism: Multiple forms of the same instance overloads of methods, overrides of methods, references to parent classes to entities of subclasses (inheritance, implementation)
Key words
This : 1. Refer to Current object
2, refers to the current class
3, refers to the construction method can only be placed in the first line
Super: 1. Refer to Parent class object
2, refers to the construction method can only be placed in the first line
Final: 1. The final modified class cannot be inherited
2. The variable modified by fianl becomes a constant
3. Methods modified by FIANL cannot be rewritten
Static: 1, Static first born non-static after birth
2, can directly through the class name point out
instanceof: determining which class an object is an instance of
Second, Abstract class
To define abstract classes using the abstract keyword
Syntax format
Abstract class Name {}
Public abstract class Animal {
}
Abstract methods
There is no concrete implementation of abstract methods
Syntax format
Access modifier abstract return value type method name
Public abstract class Animal {
protected abstract void eat ();
}
The relationship between abstract classes and abstract methods
1, abstract class can have no abstract method
2. Classes with abstract methods must be abstract classes
3. Methods with static or final modification cannot be declared as abstract methods
There is no abstract method in an abstract class to limit its inability to be instantiated and can only be inherited
Members of abstract classes
Member variables
Can be a variable, or it can be a constant
Member Methods
Can be an abstract method, or it can be a common method
How to construct an abstract class
Abstract classes have construction methods but cannot be instantiated
Subclasses inherit this abstract class, instantiate subclasses
The subclass of an abstract class either implements the abstract method of the parent class, or the subclass continues to abstract
Third, the interface
Interface definition
Defining an interface using the interface keyword
Grammar
Public interface Flyable {
}
Interface interface Name {}
Interface uses
Implementing an interface using the Implements keyword
Grammar
public class Parrot implements flyable{
}
Class name implements interface name {}
1, the interface can not be instantiated, the use of the interface requires sub-class implementation interface
2, the implementation of the interface
A class that implements an interface must implement all the methods within the interface, otherwise the class must be declared as an abstract class
Member variables
The member variables in the interface can only be constants, the default repair
Modifier public static final
Any class can have direct access to constants in the interface
Member Methods
The methods in the interface can only be abstract methods, the default adornments
Character Public abstract
Member features
Interface has no construction method
The main purpose of the interface is to extend the function, no specific existence
Multiple implementations of interfaces
A class can implement multiple interfaces
Class name implements interface Name 1, interface Name 2, ... { }
Relationship
Class and class inheritance relationships, can only be single-inheritance, multi-tier inheritance
Class and interface implementation relationship, class implementation interface, can be implemented more
interface and interface inheritance, can be single-inheritance, or multiple inheritance
Difference
Abstract class
Member variable-----variable/constant
Member methods------abstract/non-abstract methods
The construction method------have
Relationship------------Inheritance
Interface
Member variable------constant
Member methods-------abstract methods
Construction method-------Not
Relationship-----------Implementation
Iv. Introduction to the String class
String is a reference data type
But the string actually provides us with a class
Note: String class is decorated with fianl so string is called
The value of String once defined cannot be changed
How String is created
1, directly assigned value
String str = "Coco";
2. Through the New keyword
String str1 = new String ("Fdsfs");
How string is compared:
1. Equals (): Compares two string values
2, = =: Compare two string addresses
Common methods of String:
Equals (): Determines whether the values of two strings are equal
Equalsignorecase (): Determines whether the value of two strings is equal and ignores case
StartsWith (): Start with what
EndsWith (): End With what
IsEmpty (): Determines whether the length of the string is 0
Length (): Calculates the lengths of the strings
GetBytes ():
Str.substring (): Truncation of strings
Split (): Split with some sort of rule
Replace (): replacement
ToCharArray (): Turns into a char array
CharAt (): Takes a single character according to the subscript
Contains (): Whether it contains
IndexOf (): Return subscript
toLowerCase (): Turn lowercase
toUpperCase (): Turn capital
ValueOf (): Converts a parameter that satisfies a condition to a string type
Five, Tool class
One: Object class
1. In Java, the object class is the parent class of all other classes, located in the Java.lang package?
2. All classes inherit the object class directly or indirectly, so extends object is omitted.
3. There are several methods available in object that allow subclasses to rewrite or call directly
ToString method for object: Subclass can override ToString (), custom object's string output format
GetClass method: You can get the full name of the real class of the object through the GetName method in class
Hashcode method: The Hashcode () value of the same object must be the same
Equals () Method:
Second, System class
1, the System class provides a lot of systems-level properties and control methods, located in the Java.lang package
2, the System class is the final class cannot be instantiated, all methods and properties are static
Current time:
SimpleDateFormat sd = new SimpleDateFormat ("Yyyy-mm-dd");
Date date = new Date ();
String time = Sd.format (date);
System.out.println (time);
Third, Random class
1. The random class is used to generate pseudo-random numbers, located in the Java.util package?
2. Generation of Random objects
Iv. Math Class
1. The math class provides a way to perform basic mathematical operations, located in the Java.lang package
2, the Math class is the final class can not be instantiated, all methods and properties are static
Five, arrays class
The Arrays class provides a series of methods for manipulating arrays in the Java.util package
Six, Packaging class
The Java language is an object-oriented language, but the basic data types in Java are not object-oriented, which has many inconveniences in practice.
To address this shortcoming, a corresponding class was designed for each basic data type when the class was designed
The classes corresponding to the eight basic data types are collectively referred to as wrapper classes (Wrapper Class)
Vi. anomalies
(Throwable Class)
1. Error errors-cannot be saved
2, Exception exception--small bug in the program, can repair
A) The exception is examined--a red line hint is directed when defining the code in the program.
b) Runtime exception--syntax format is correct, runtime exception;
capture (field discovery onsite processing)
try{
//code for possible exception
}catch (Exception type exception object) {
//print exception information
Span style= "font-size:16px" >//other processing
}
Note: Multiple exceptions may occur for the same code to add multiple corresponding catch blocks
Try......catch execution order
Note: When defining multiple catch blocks Be aware of their order ()
multiple catch blocks cannot define the same exception
1, if the code in the try does not have an exception then skip the catch block directly
2, If the code in the try has an exception then the corresponding exception will be matched in turn according to the exception printed
try......catch......finally
Throw Exception
(generally not throwing exceptions directly in the Main method
Throw an exception must be disposed of)
Formula: Method Header (method definition) throws exception type
public static void Getreuslt () throw ArithmeticException
The use of the thrown method is not to handle the exception just prompt the caller this place will be an exception
Capture and throw:
1. Catch is in handling exception
Thrown is a hint exception
2. Capture: Try Catch
Thrown: Throws
3. Capture is directed to the possible exception of the code directly processing
Thrown: is defined on the method header
The difference between throw and throws:
Throw: defined in the method body
Throws: Defined in method header
Java Basics Summary (iii)