Java Object-Oriented overview

Source: Internet
Author: User

I. Object-oriented and process-oriented

process oriented: Procedure oriented is a top-down programming

oo: Object oriented is the abstraction of things, the need to create an abstract model (with attributes and behaviors), and then use the model directly.

Example: People put elephants in refrigerators
Process oriented:

1) Open the refrigerator

2) put the elephant in.

3) Close the refrigerator door
Object-oriented:
People
Open (refrigerator) {

Refrigerator. Open ()

}
Operation (Elephant) {

Elephants. Enter (refrigerator)

}
Close (refrigerator) {

Refrigerator. Close Up ()

}
}

Elephant
Enter (refrigerator) {}

}
Refrigerator
Open () {}

Close () {}

}

three main features of object-oriented: encapsulation, inheritance and polymorphism

Ii. related knowledge of class

A class is an abstraction of something of a common character in real life, such as all birds flying, consisting of attributes (member variables or field) & Behavior ( member methods or method ) . An object is a concrete instantiation of a class, which is the actual existence of each individual of that class of things.

member variables and local variables  

outside the method body, variables declared within a class are called member variables, are divided into instance variables (not static adornments) and class variables (with static adornments), the instance variables exist in the heap memory where the object resides, and can have modifiers , if not explicitly assigned at the time of declaration ,

there are different default initialization values for different data types, and variables declared inside the method body are called local variables and are divided into formal parameters (variables defined in the method signature), method Local variables (defined within a method), code block local variables (defined within a code block),

local variables exist in the stack memory, no modifiers, same as the method modifier, must be explicitly assigned (local variables do not have default initialization values)  

 PackageCom.yyx.pratice; Public classPerson {//member Variables    PrivateString name; Private intAge ; //member Methods     Public voidEatfood () {//Local VariablesString foodname = "Burger"; System.out.println ("I'm eating" +foodname); }     Public Static voidMain (string[] args) { person person=NewPerson ();    Person.eatfood (); }} 

Three, class of memory analysis

All object names are saved in the stack memory, and the specific contents of the objects are stored in the corresponding heap memory, and the new keyword must be used to open up the heap memory space

A class is a reference data type, and a reference data type refers to a heap of memory space that can be pointed to by multiple stack memory at the same time

 PackageCom.yyx.pratice; Public classTestzoom { Public Static voidMain (string[] args) {Animal A1=NewAnimal (); A1.name= "Floral";//object. Property directly assigned valueA1.setage (3);//Packaging IdeasSystem.out.println (A1.name + "" +a1.getage ()); Animal A2=NewAnimal (); System.out.println (A2.name+ " " +a2.getage ()); Animal A3=NewAnimal (); A3=A1; System.out.println (A3.name+ " " +a3.getage ()); }}classAnimal {String name; Private intAge ;  PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }     Public intGetage () {returnAge ; }     Public voidSetage (intAge ) {         This. Age =Age ; }} Run Result: Floral3NULL0Floral3

The construction method of the four classes

    • the method name of the construction method must be the same as the class name
    • The constructor method has no return type, cannot be defined as Void, does not declare a method type before the method name, cannot be modified by static, final, synchronized, abstract, native
    • A class can define multiple construction methods, and if a constructor is not defined when the class is defined, the compiled system automatically inserts a default constructor with no parameters, which does not execute any code
    • Construction methods cannot be called by programmers, but are called by the system
    • The main function of the construction method is to complete the initialization of the object, which can pass the parameters that define the object to the domain of the object.

The precedence of the property assignment of a class object:

    1. Default initialization of a property
    2. Explicit initialization of a property
    3. Class with the constructor for the property.
    4. Assign a value to a property by using the "object. Method" Way
 PackageCom.yyx.pratice; Public classPerson {PrivateString name; Private intAge ; /** No parameter construction method, no constructor is defined when defining the class, * the compilation system automatically inserts a default constructor with no parameters*/     PublicPerson () {Super(); }    /** Parametric construction method*/     PublicPerson (String name,intAge ) {        Super();  This. Name =name;  This. Age =Age ; }     PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }     Public intGetage () {returnAge ; }     Public voidSetage (intAge ) {         This. Age =Age ; }     Public Static voidMain (string[] args) {person Person1=NewPerson (); Person1.setname ("Zhang San"); Person1.setage (23); System.out.println (Person1.getname ()+ " " +person1.getage ()); Person Person2=NewPerson ("John Doe", 21); System.out.println (Person2.getname ()+ " " +person2.getage ()); }}

Java Object-Oriented overview

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.