S1/using Java to understand program logic/11-classes and objects

Source: Internet
Author: User
Tags naming convention

Object: An entity used to describe an objective thing, consisting of a set of properties and methods.

The class defines the characteristics (attributes) and behavior (methods) that the object will have.

In Java object-oriented programming, you create an instance of the class with a class, that is, an object that creates a class.

A class is the type of an object. , in fact, defining a class is a data type that extracts the generality of a homogeneous entity. For example, "Customer" category, "person" category, "animal" category, etc.

The class template for Java is shown below.

Grammar:

public class < class name >{

Defining Attributes Section

Property 1 of type attribute 1;

Property 2 of type attribute 2;

......

Property 3 of type attribute 3;

Define Method Section

Method 1;

Method 2;

......

Method N;

}

The public in front of class is the common meaning, do not omit to write according to the code.

Naming rules for classes:

1. You cannot use keywords in java.

2. Cannot contain any embedded spaces or dots ".", and special characters other than the underscore "_" and "$" characters.

3. You cannot start with a number.

Note: The class name is usually made up of multiple words, with the first letter of each word capitalized. In addition, the class name should be concise and meaningful, use full words as much as possible, avoid abbreviations, unless the abbreviation is widely used, such as HTML, HTTP, IP, and so on.

How to define a class:

1. Define Class name

By defining the class name, you get the outermost frame of the program.

Grammar:

public class class Name {

}

2. Write the properties of the class

You describe the static characteristics (attributes) of a class by defining variables in the body of the class, which are called member variables of the class.

3, the method of writing classes

By defining methods in a class to describe the behavior that a class has, these methods are called member methods of the class.

In Java, the framework for a simple method is as follows.

Access modifier return value type method name () {

Method body

}

The access modifier restricts the scope of access to the method, such as public, as well as other access modifiers. The return value type is the type that returns the result after the method executes, either a base type, a reference type, or a return value, which must be described using void. The method name generally uses a meaningful name to describe the function of the method, and the naming convention should conform to the identifier.

Camel (Camel) nomenclature and Pascal (Pascal) nomenclature.

Camel nomenclature: The first letter of a method or variable name is lowercase, and the first letter of each word is capitalized. For example, Showcenter, username, and so on.

Pascal's nomenclature: The first letter of each word is capitalized. For example, class name school, and so on.

In Java, you define the properties and methods of a class using the Camel Nomenclature, which defines the class using the Pascal nomenclature.

The role of Tears is to create objects. The object is generated by the class, which is called the instantiation of the class. An instance is an object, and a class can generate multiple objects. The syntax for creating an object is as follows.

Grammar:

Class Name Object name =new class name ();

When you create an object for a class, you need to use the Java New keyword. For example, create an object of the school class.

School center=new School ();

The type of the center object is the school type. In Java, to reference the properties and methods of an object, you need to use the "." Operator. Where the object name is on the left side of the dot, the name of the property or method is on the right side of the dot.

Grammar:

The object name. Properties//Reference Object properties

Object name. Method Name ()//Method referencing object

For example, after you create an object center for the school class, you can assign values to or Invoke methods on the object's properties, as in the following code.

Center.name= "Beijing Center"; Assigning a value to the Name property

Center.showcenter (); Call the Showcenter () method

Default values for Java data types

Type

Default value

Int

0

Double

0.0

Char

' \u0000 '

Boolean

False

String

Null

S1/using Java to understand program logic/11-classes and objects

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.