Java Learning II

Source: Internet
Author: User

First, class

1. A class is a mold that determines the characteristics (attributes) and behavior (methods) that an object will have

2, the characteristics of the class:

(1), class is the type of object

(2) A collection of groups of objects with the same properties and methods

3, the class is abstract concept, is just a template, such as: "Mobile phone", the object is a you can see, touch the specific entity

Second, define the class

1. The importance of classes: All Java programs use class classes as organizational units

2. Define the steps of the class:
(1) Define class name (2) write the properties of the Class (3) methods for writing classes

public class class Name {

Defining attribute Parts (member variables)

Property 1 of type attribute 1;

Property 2 of type attribute 2;

....

Method 1;

Method 2;

....

}

Third, the object

1. Steps to use objects

(1) Create an object

Class Name Object name = new class name ();

Telphone phone = new Telphone ();

(2) Working with objects

Reference object's properties: Object name. Properties

Phone.screen = 5;

Methods for referencing objects: Object names. Method Name ()

Phone.sendmessage ();

IV. member variables and local variables

1. Member variables

defined in the class to describe what the object will be

2. Local Variables

Defined in a method of a class to temporarily save data in a method

Difference:
(1) The scope of a local variable is limited to the method that defines it

(2) The scope of the member variable is visible within the entire class

(3) different initial values

Java will give the member variable an initial value

Java does not assign an initial value to a local variable

(4) In the same method, local variables with the same name are not allowed, and in different methods, local variables with the same name can be

(5) When two types of variables have the same name, local variables have higher precedence

Five, the construction method

1. Create a new object using the new+ construction method

2, the constructor method is defined in the Java class to initialize the object method, the construction method with the same name and no return value

3, no parameters of the construction method

4, the construction method with parameters

Java Learning II

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.