Java Object-Oriented analysis

Source: Internet
Author: User

1. (understanding) Object-oriented vs. process-oriented example: people open the door; pack the elephant in the fridge

2. Object-oriented programming focuses on class design!
1) A project or project, no matter how large, must have a class composition.
2) The class is abstract, like a blueprint for making a car.
and a specific vehicle, which is made according to the drawings, is actually an instantiation of the class.

3. The idea of completing a project (or function)
1) Whether the object of the class that corresponds to the function to be completed exists.
2) If present, it is possible to invoke the property or method in the corresponding class directly through the object.
3) If it does not exist, you need to create the object of the class. Even if the class does not exist, you need to design the class.

4. Three main lines of object-oriented programming:
1) Composition of classes and classes: Attribute method constructor code block inner class
2) Object-oriented programming features: Encapsulation inheritance polymorphism (abstract)
3) Other keywords: This super package import static final abstract interface ...

1. About the design of the class

2. Composition of the class:
1) attribute (member variable, Field)
2) Method (Member method, function, method)

2.1 Properties:
* member variable vs local variable
* same point: 1. Follow the format of the variable declaration: Data type variable name = initialization value
* 2. There are scopes
* different points: 1. The location of the declaration differs: Member variable: declaration in class, outside method
* Local variables: Declared within the method, the formal parameter part of the method, within the code block (the main method is also a method, so the variables inside the main method are also)
* 2. The modifier for the member variable has four: Public private protected Default
* local variable has no modifier , same as the method modifier where it is located.
* 3. Initialization value: There must be an 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.
* byte short int long ==>0
* float double ==>0.0
* Char ==> space
* Boolean ==>false
* Reference type variable Volume ==>null
* local variable: Be sure to assign the value explicitly. (local variable has no default initialization value)
* 4. The two locations in memory are different: member variables exist in heap space; local variables: Stack space
*
* Summary: About variables: 1) by Data type: Basic data Type (8) & Reference data type
* 2) varies by declared location: member variables & local variables

2.2 Method: To provide a function of the implementation of the Main method is also a method, the main method inside the property must also be assigned value.
* 1) Example: public void Eat () {//Method body}
* Public String GetName () {}
* public void SetName (String n) {}
* Format: Permission modifier return value type (void: no return value/specific return value) method name (formal parameter) {}
*
* 2) About return value type: void: Indicates that this method does not require a return value
* Method with return value: At the end of the method there must be a return + variable corresponding to the return value type
* Memory: Void and return can not appear in a method at the same time. Like a pair of "friends."
*
* 3) You can call other methods or properties of this class within a method, but you cannot define a method within a Method!


3. Object-oriented programming idea of the ground rule one:
1) Design and create the components of classes and classes
2) Instantiate the object of the class
3) complete a function through the form "object. Properties" or "object. Method"

4. Initialization of class memory parsing
4.1 Structure of the memory partition:
Stack: Local variables, reference names for objects, reference names for arrays
Heap: New "things" (e.g. entities of objects, entities of an array), with member variables
Method Area: Contains string constants
Static domain: A variable declared as static

4.2 Understand the basics of creating a class object in memory to run.

* 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!

The following four methods form an overload
Defines the and of two variables of type int
public int Getsum (int i,int j) {
return i + j;
}
Defines the and of three variables of type int
public int getsum (int i,int j,int k) {
return i + j + k;
}
Defines the and of two double type data
Public double getsum (double d1,double D2) {
return D1 + d2;
}

Defines the and of three array of type double
public void Getsum (double d1,double d2,double D3) {
System.out.println (D1 + d2 + d3);
}
cannot be overloaded with several methods as above
public int getSum1 (int i,int j,int k) {
return i + j + k;
//}
public void getsum (int i,int j,int k) {
System.out.println (i + j + k);
//}


The following two methods form an overload.
public void method1 (int i,string str) {

}
public void Method1 (String str1,int j) {

}

* 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.
* 5. If there is a variable number of parameters in the method, be sure to declare at the end of the method parameter.
* 6. In one method, declare at most one variable number of parameters.


The following four methods form an overload
Once the method of the overloaded variable number of parameters is defined in the class, the following two methods can be omitted
public void SayHello () {
System.out.println ("Hello world!");
//}
public void SayHello (String str1) {
System.out.println ("Hello" + str1);
//}
Method of parameter with variable number
public void SayHello (String ... args) {
for (int i = 0;i < args.length;i++) {
System.out.println (Args[i] + "$");
}
System.out.println ("=====");
}

public void SayHello (int i,string ... args) {
public void SayHello (String ... args,int i) {
System.out.println (i);

for (int j = 0;j < args.length;j++) {
System.out.println (Args[j] + "$");
}
}

public void SayHello1 (string[] args) {
for (int i = 0;i < args.length;i++) {
System.out.println (Args[i]);
}
}

* One, third member of Class: Constructor (constructor constructor method)
* Constructor: Builder
* Constructor Function: ① Create object ② assign values to properties of the created object
*
* 1. When designing a class, the constructor of a null parameter is provided by default if the constructor of the class is not explicitly declared.
* 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

1.static: Static, can be used to modify properties, methods, blocks of code (or initialization blocks), * inner classes

2.
Static modifier property (class variable):
* 1. All objects created by the class share this property
* 2. When one of the objects modifies this property, it causes another object to make a call to this property. VS instance variable (non-static decorated property with each object owning a set of replicas)
* 3. Class variables are loaded with the load of the class, and the uniqueness
* 4. Static variables can be called directly from the "class. Class variable" form.
* 5. The class variable is loaded earlier than the object. So when you have an object, you can use the object. Class variable. However, "class. Instance variable" is not possible.
* 6. Class variables exist in the static domain.
*
* Static Modification method (class method):
* 1. Loaded with the load of the class, in memory is the only
* 2. Can be called directly through the "class. Class Methods" method
* 3. You can call a static property or a static method internally, and you cannot invoke a non-static property or method. Conversely, a non-static method can call a static property or a static method
* > Static method inside is not able to have this or super keyword!
* Note: Static structures (properties of static, methods, blocks of code, inner classes) have a lifetime earlier than non-static structures, and are recycled later than non-static structures

Java Object-Oriented analysis

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.