JAVA class and object (2) ---- class definition basics, java ----

Source: Internet
Author: User

JAVA class and object (2) ---- class definition basics, java ----

Classes are the basic elements of java programs and an important composite data type in java. It encapsulates the states and methods of a class of objects, and is the prototype of this class of objects. The implementation of a class includes two parts: class declaration and class body. The basic format is as follows:

Class <class name>

{

Attribute

Method

}

Class is a keyword used to define a class. "Class <class name>" is the declaration part of the class. The class name must be a valid java identifier and the content between braces is the class body.

 

 

 

Class declaration Syntax:

[Public] [abstract | final] class ClassName

[Extends superClassName] [implements interfaceNameList]

{

...

}

The modifiers public, abstract, and final indicate the class attributes. ClassName indicates the class name, superClassName indicates the name of the parent class, And interfaceNameList indicates the list of interfaces implemented by the class. Traditionally, the first letter of the class name is in uppercase, but this is not necessary. The naming rules of class names must comply with the naming rules of java variables. In addition, when a class name is composed of multiple words, the first letter of each word is capitalized for differentiation. For example, BeijingTime.

 

 

 

 

Class Definition Syntax:

The purpose of writing a class is to describe the attributes and functions of a class of things. The description process is implemented by the class body. The content of the class body consists of two parts: (1) definition of variables, used to characterize attributes (2) Definition of methods, used to portray functions.

Class className

{

[Public | protected | private | static | final] [transient] [volatile] type variableName; // definition of member variables

  

[Public | protected | private] [static] [final | abstract] [native] [synchronized]

ReturnType methodName ([paramlist]) [throws javasttionlist] // syntax of the member Method

}

The following is an Employee class. Two variables are defined in the class body. One is the name variable of the String type and the other is the salary variable of the double type. The method definition section describes the getName () method of the name variable.

Public class Employee {

Private String name; // variable definition

Private double salary;

Public Employee (String n, double s) {// Constructor

Name = n;

Salary = s;

}

 

Public String getName () {// getName () method

Return name;

}

}

 

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.