Hanshunping the Java section 05.06.07 class. Object

Source: Internet
Author: User

    • The development of computer language to approach people's way of thinking

assembly language [for machine]

C language [process oriented]

Java language [object-oriented]

    • From class to object, there are several arguments: 1. Create an object; 2. Instantiate an object; 3. Instantiate the class.
    • The first letter of the class name is capitalized.
5.1 Categories and objects of distinction and contact

1, the class is abstract, conceptual, representing a class of things, such as human, cat;

2, the object is concrete, actual, represents a specific thing;

3, the class is the template of the object, the object is an individual, an instance of the class.

5.2 How to define a class

A comprehensive class definition is more complex:

Package Name:

Class name extends parent class implements interface name

{

member variables;

Construction method;

Member methods;

}

Member variables of the 5.2.1 class
    • Member variables are part of a class, typically a basic data type, or a reference type.
    • Object-How to create an object:

There are two methods of creating an object:

1, first declare and then create

Object declaration: Class Name Object name

Object creation: Object name =new class name ();

2. One Step method

Class Name Object name =new class name ();

    • Object-How to access (use) the member variables of the object:

Object name. variable name;

    • Note Point to the problem:

Member methods for the 5.2.2 class
    • Member methods are also called member functions.
    • Member method of the class-definition

Public return Data type method name (parameter list)

{

Statement Method (function) body

}

1, parameter list: Indicates the member function input;

2. Return data type: Indicates the data type of the output of the member function (no return value when the data type is void);

3. Function Body Statement: Represents a block of code to implement a function.

    • Memory will open up a new stack for member functions, as shown in:

    • Member method of the class-declaration

For example, public int test (int a); /* Method declaration */

The function of this statement is to declare the method, declared in the form:

Access modifier data type function name (parameter list);

    • Member methods of the class-special instructions

1. The parameter list of a method can be multiple, and the data type can be any type: int float double char, etc.;

2, in the call member method, the number of parameters given, the type to be consistent with the definition (note: For example, 2.4 default is double,2.4f is float);

3, when invoking the member method, the type of the returned result should also be consistent with the definition;

4, the method can have no return value, the return data type is void, indicates that the method does not return a value.

The construction method of 5.2.3 class

What kind of construction method is it? Before answering this question, let's look at a requirement:

Before we created the human object, we first created an object and then assigned a value to his age and name attribute, and if I now ask that you specify the age and name of the object directly when creating a human object, what should I do?

You can define a construction method when you define a class.

    • The construction method is a special method of the class, its main function is to complete the initialization of the new object. It has several features:

1, the method name and the class name are the same;

2, no return value;

3, when creating a new object of a class, the system automatically calls the constructor method of the class to complete the initialization of the new object;

4, a class can have a variety of construction methods, for example: we can give the person class to define a construction method, the method to create a new object, only specify the name of the person, do not need to specify the age;

5, each class has a default construction method;

The default construction method:

Class Person

{

}

For example

Person P1=new person ("Mike");

Person P2=new person ("Jack");

Person P3=new person ();

Class Person

{

int age;

String name;

public person (int age,string name)

{

Age=age;

Name=name;

}

Public person (String name)

{

Name=name;

}

Public person ()

{

}

}

Hanshunping the Java section 05.06.07 class. Object

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.