Third, Java object-oriented programming _1

Source: Internet
Author: User
Tags modifiers

Directory

I. Concepts of objects and classes

Ii. objects and references

1. Objects

2. Member variables

3. References

III. Definitions of Java classes

Iv. Constructors (construction method)

Five, Memory analysis

I. Concepts of objects and classes 1. Objects

The object uses computer language to describe the things in the problem domain, and the objects have the static and dynamic properties corresponding to the things by "attribute (attribute)" and "method" respectively.

2. Class

A class is an abstract concept used to describe an object of the same type, which defines the static and dynamic properties that a class of objects should have

3. Relationship of Class objects

A class can be a template for a class of objects that can be called a concrete instance of

1) Association relationship (weakest relationship)

For example, teachers and students and schools are related to the relationship between teachers and students, students and schools have a relationship

2) Succession relationship (general and special Relationship)

XX is a kind of XX, for example: student is a person, student inherits from person

3) Aggregation relationship (overall and partial relationship)

For example, a man is part of a human being, an aggregation.

4) Implementation relationship (interface dependent) 5) polymorphic Two, object and reference 1. Object
    • object is the core of a Java program, in a Java program "Everything is Object"
    • objects can be called packages of static attributes (member variables) and dynamic properties (methods)
    • Class is a "template" for creating objects of a uniform type, defining the member variables and methods that the class object should have in a class

Why Use objects:

    • Object-Oriented Programming: A group of objects interact with each other to accomplish specific functions through communication
    • objects have interfaces for external services (can be reused through inheritance)
    • Object shadowing implementation of internal services (can be reused through aggregation)

object creation and use:

    • You must use the New keyword to create an object
    • Use objects (References). Member variables to refer to the member variables of an object
    • Use objects (References). Method (parameter list) to invoke the object's method
    • Each object of the same class has a different storage space for member variables
    • Methods for sharing this class with each object of the same category

2. Member variables

Member variables can make any data type in the Java language, including basic types and reference types

When defining a member variable, it can be initialized, the day is not initialized, Java defaults to the default value (0, NULL, etc. for different data types have different assignments, but are the most basic 0 or null) to initialize it (member variables can be uninitialized, local variables must be initialized)

Member variables are scoped to the entire class body

3. References

Variable types other than primitive types in the Java language are referred to as reference types

Objects in Java are manipulated by reference. For example:

// declares a reference variable of type string, but does not point him to an object String S; // use the new statement to create an object of type string and use S to point to it, which you can later do with S New String ("HelloWorld");

How to classify and object in memory Central:

Class is a static concept, stored in the code area

The object is new except that it is in heap memory, and each member variable of the class is represented by a different value in different objects (except for the static variable) and only one copy of the method is used, which takes up memory when executed.

III. Definitions of Java classes
//define a class with the class keyword, for example:classPerson {//member Variable definition     Public intId//Public member variables    Private intAge = 20;//Private member Variable//method Definition     Public intGetage () {returnAge ; }         Public voidSetage (inti) { age=i; }         Public intgetId () {returnID; }}

The definition of a class consists of two main components: member variables and methods

The format of the declaring class is:

[modifiers] Type <attr_name> [=defiultvalue];

For example: public int id; private int age = 20;

The format of the Declaration method is:

[<modifiers>] <modifuers> <return_type> <name> ([argu_list]) {<statements>}

For example: public int getage () {return age;}

Iv. Constructors (construction method)

Create a new object using the new+ construction method

A constructor is a function that is defined in a Java class to initialize an object

The constructor has the same name as the class and no return value

For example:

// define a class  Public class Person {    // define member variable    int  ID;     int Age ;     // Construction Method    Person (int n,int  i) {        = n;         = i;    }}

Constructor method name must be consistent with class name

The constructor method cannot have a return value and cannot have a void

Use constructors to initialize the object's member variables when creating an object

// define a test class  Public class test1 {    // Main method public    staticvoid  Main ( String args[]) {        //new A just person object and pass in the argument        new person (1,25);         New person (2,27);    }}

When no constructor is specified, the compiler automatically adds the class as follows:

The constructor for the class name () {}, for example:

// define a test class  Public class test1 {    publicint  x;     // Main Method     Public Static void Main (String arg[]) {        new  test1 ();}    }

As in the code above, the compiler will not error, it is equivalent to:

// define a test class  Public class test1 {    publicint  x;     // Main Method    Test1 () {}    // This is the Java auto-generated construction method public    staticvoid  Main (String arg[]) {        new  test1 ();}    }
Five, Memory analysis instance one:
//define the following classesclassBirthDate {Private intDayPrivate intMonthPrivate intYear ; //Construction Method     PublicBirthDate (intDintMinty) { day= D;        month = m; Year =y; }    //Method 1     Public voidSetday (intd) { Day=D; }    //Method 2     Public voidSetmonth (intm) {month=m; }    //Method 3     Public intGetDay () {returnDay ; }    //Method 4     Public intGetMonth () {returnmonth; }    //Method 5     Public voiddisplay () {System.out.println ( day+ "-" + month + "-" +Year ); }    }
//Test Class Public classTest {//Main Method     Public Static voidMain (String args[]) {test test=NewTest (); intDate = 9; BirthDate D1=NewBirthDate (7, 7, 1970); BirthDate D2=NewBirthDate (1, 1, 2000);        Test.change1 (date);        Test.change2 (D1);        Test.change3 (D2); System.out.println ("date =" +date);        D1.display ();    D2.display (); }    //Defining Methods     Public voidChange1 (inti) {i= 1234; }     Public voidChange2 (BirthDate b) {b=NewBirthDate (22, 2, 2004); }     Public voidChange3 (BirthDate b) {B.setday (22); }}

Debug output:

Date = 97-7-197022-1-2000

Test.change1 the value of date does not change after execution, the space allocated to I in the stack memory space disappears

The value of D1 is not changed after execution of the Test.change2, the memory space allocated to B in the stack memory space disappears, but the value in the heap memory space pointed to by B is temporarily not gone (it is automatically reclaimed by the Java garbage collection mechanism)

The value of D2 changed after Test.change3 execution, the memory space allocated to B in the stack memory space disappears, and the value of D2 is changed forever.

The memory layout should be like this when the program executes to the new D2 object (left stack right heap):

Third, Java object-oriented programming _1

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.