An overview of object-oriented thinking :
Lprogrammers are transformed from performers into conductors. Lwhen the requirement is complete:?go first.Find theRequired Functionsthe object touse. ?if the object does not exist, create an object with the required functionality. ?this simplifies development and improves reuse. Lclass(Class)and theObject(object)is the core concept of object-oriented. ?class is the description of a class of things, isof abstract, conceptual definition?An object is an individual of the actual existence of such a thing, and thus also calledExample(instance).
diagram of the object in memory:
The two main differences between a member variable and a local variable are:
1. Different position of declaration: Member variable: declaration in class, outside method
* Local variables: Declared within a method, the formal parameter part of a method, within the code block
2 initialization value: member variable: If you do not explicitly assign a value at the time of declaration, the different data types will have different default initialization values.
Local variables: Be sure to assign the values explicitly. (Local variables do not have default initialization values)
3 The two locations in memory are different: member variables exist in heap space, local variables: Stack space.
Several ways to manipulate an array:
Inverse of an array
Public int[] Reverse (int[] arr) {
for (int x = 0, y = arr.length-1; x < y; + x, y--) {
int temp = arr[x];
ARR[X] = Arr[y];
Arr[y] = temp;
}
return arr;
}
Sorting an array
public void sort (int[] arr, String desc) {
if (desc = = "ASC") {//Ascend: from small to large
for (int i = 0; i < arr.length-1; i++) {
for (int j = 0; J < arr.length-1-I; j + +) {
if (Arr[j] > arr[j + 1]) {
Swap (arr,j,j+1);
Swap (Arr[j],arr[j + 1]);
}
}
}
} else if (desc = = "desc") {//from large to small
for (int i = 0; i < arr.length-1; i++) {
for (int j = 0; J < arr.length-1-I; j + +) {
if (Arr[j] < Arr[j + 1]) {
Swap (arr,j,j+1);
Swap (Arr[j],arr[j + 1]);
}
}
}
} else {
SYSTEM.OUT.PRINTLN ("You entered the wrong sort Method!") ");
}
}
* Overloading of methods(overload)
* Requirements: 1. In the same class 2. The method name must be the same 3. The parameter list of the method is different (the number of ① parameters differs ② parameter type)
* Supplement: The overloads of the method have no relation to the return value type of the method!
For variable parameters of a method:
/*
* Method of variable number of formal parameters:
* 1. Format: For method parameters: Data Type ... Formal parameter name
* 2. The method of a variable number of parameters forms an overload between the method with the same name
* 3. Variable number of formal parameters at the time of invocation, the number starts from 0, to infinitely many can.
* 4. The use of variable multiple formal parameters is consistent with the use of arrays for method parameters. (Two can not exist at the same time)
public void SayHello (String ... args) {
for (int i = 0;i < args.length;i++) {
System.out.println (Args[i] + "$");
}
System.out.println ("=====");
}
public void SayHello (string[] args) {
for (int i = 0;i < args.length;i++) {
System.out.println (Args[i]);
}
}
In this case, an error will be given.
When called with variable arguments, you can T.sayhello (new string[]{"Hello China", "Hello Beijing"});
can also be T.sayhello ("Hello China", "Hello Beijing");
* 5. If there is a variable number of parameters in the method, then be sure to declare at the end of the method parameter (variable parameter parameters can only be declared at the end of the method parameters)
* 6. In one method, declare at most one variable number of parameters.
Parameter passing in a Java method:
* Method parameter transfer (emphasis, difficulty)
* 1. Formal parameters: method declaration, parameter in method parentheses
* Argument: The value of the actual parameter passed in when the method is called
*
* 2. Rule: Parameter passing mechanism in Java: value passing mechanism
* 1) A parameter is a basic data type: A variable that passes the value of an argument to the base data type of the parameter
* 2) Formal parameters are reference data types: The value of the argument's reference type variable (the first address value of the object entity corresponding to the heap space) is passed to the reference type variable of the formal parameter.
Object-oriented feature one: encapsulation and hiding
* Issue: When you create an object of a class, if you assign a value to the corresponding object property directly through the "object. Properties" method, you may experience a problem that does not meet the actual
* In the event of an accident, we consider not allowing the object to directly act on the property, but rather through the "object. Method" form, to control the object's access to the property. Actual
* In the case, the requirements for attributes can be represented by means of methods.
*
* Solution: (encapsulating Idea) ① the properties of the class, ② provides a common method (setter & getter) to implement the call.
*
* Second, permission modifier: Public Private default protected
* Can be used to modify properties, methods
* Note: 1) permissions are arranged from large to small: public protected default Private
* 2) The permissions of the modifier class are: public default
Used to qualify the class object for access to the class member (member variables, methods, and so on).
*
the third member of a class: constructor (constructor constructor method)
* Constructor: Builder
* Constructor Function: ① Create object ② assign values to properties of the created object
*
* 1. When designing a class, if you do not explicitly declare the constructor of the class, the program will provide a constructor for the null parameter by default
* 2. Once the constructor for the class is explicitly defined, the default constructor is no longer available.
* 3. How to declare a constructor for a class. Format: Permission modifier class name (formal parameter) {}
* 4. Overloading between multiple constructors of a class
*
*
* Second, the order of the property assignment of the class object: The default initialization of the ① property ② the explicit initialization of the property ③ by the constructor to the property class.
*④ Assigning a value to a property by means of the object. method
This keyword:
* 1. Can be used to modify properties, methods, constructors
* 2.this is understood as the current object or the object currently being created. For example: This.name,this.show (); (Use this in the constructor to represent the object currently being created)
*
* 3. The specified constructor can be called in the constructor with the other overloads in this class by means of "this (formal parameter)".
* Requirements: 1. Inside the constructor must be declared in the first line!
* One, object-oriented Characteristics II: Inheritance
* 1. Why design inheritance?
*
* 2. The Class A extends B class implements inheritance of classes.
* Subclass: A Parent Class (or base class superclass): B
*
* 3. After the subclass inherits the parent class, the attributes, methods, and subclasses that are declared in the parent class are available to the child class.
* Explicit: When there is a private property or method in the parent class, the subclass can also be obtained, just because of the encapsulation design, so that the subclass can not directly
* Call it.
* Subclasses can define their own unique components in addition to the structure of the parent class through inheritance.
*
* Extends: Subclasses are "extensions" to the functionality of the parent class, and the explicit subclass is not a subset of the parent class.
*
* Inheritance of classes in 4.java only supports single inheritance: A class can inherit only one parent class. Conversely, a parent class can have more than one child class.
* Second, the method of rewriting---(method overloaded) modifier Returns a value type method name (parameter list) {}
* 1. Prerequisites: There are subclasses inheriting the parent class
* 2. After the subclass inherits from the parent class, if the parent class's method is not applicable to the child class, the subclass can override (override overwrite), overwrite, override the method of the parent class.
* 3. Overriding rules: 1) The "Return value type method name (parameter list)" of a subclass method is required as a method of the parent class
* 2) The modifier of the subclass method cannot be less than the modifier of the parent class method
* 3) * If the parent method throws an exception, the subclass method throws an exception type that cannot be greater than the parent class.
* 4) * The method of the child parent class must be static or non-static.
5) Private method subclasses are not visible, and therefore there is no rewrite. If there is such a private method in the subclass, then this method has nothing to do with the parent class, which is completely independent.
Super Keyword:
l use Super in the Java class to invoke the specified action in the parent class: ? Super can be used to access properties defined in the parent class ? Super can be used to invoke a member method defined in the parent class ? Super that can be used to call the parent class in a subclass construction method. Construction Device L Note: ? especially When a member of the same name appears in the child parent class, you can use the Super make differentiate ? Super is not limited to direct parent class super this this super represents the identity of the memory space of the parent class
(if there is a variable with the same name in the parent class and subclass, the subclass will have two variables).
* 3) Super modifier constructor: explicitly invokes the constructor specified in the parent class by using "super (formal parameter list)" in the subclass.
* > inside the constructor, "super (parameter list)" must be declared in the first row!
* > inside the constructor, "this (parameter list)" or "super (parameter list)" can only appear one!
* > When you do not explicitly invoke either this (formal parameter list) or super (parameter list) in the constructor, the default call is
* the constructor of the parent class NULL argument!
* Recommendation: When designing a class, try to provide a constructor with NULL parameters!
l All of the constructs default null parameter construct l when There are no null arguments in the parent class Span style= "Color:black" > construct construction must through this ( parameter list super ( parameter list ) statement specifies that the corresponding construct must be placed in the constructor's l If the subclass constructs compilation error (In summary: the creation of a subclass object is bound to call the constructor of the parent class.) )
Child class object Instantiation process:
The constructor for all immediate and indirect parent classes of the child class object is called first, from top to bottom, and then to the constructor.
* Object-oriented feature three: polymorphism
* 1. What does polymorphism mean? Polymorphism, which can be understood as a variety of forms of a thing's phenotype.
* 1) method overloads and overrides 2) the polymorphism of the subclass object (parent class reference to child class object)
Virtual method invocation: Refers to the object entity of the subclass through a reference to the parent class, and when the method is called, the child class is actually executing the method of overriding the parent class
3. The program runs into a compilation state and a running state.
* For polymorphism, at compile time, "look left", this reference variable is interpreted as the type of the parent class
* Runtime, "Look right", focus on the entity of the real object: The object of the subclass. Then the method of execution is the subclass rewrite.
4. The polymorphism of the subclass object is not used for attributes.
The parent class and child class have a variable with the same name, and then the parent class refers to the child class object, when the reference calls that variable, the parent class of the call.
(a method subclass can override or overwrite, so polymorphic methods of executing subclasses, but there is no overwrite, there are two variables with the same name in the subclass, because it is a reference to the parent class, the variable of the parent class is called when called.) )
Java Object-oriented