How to define the class structure

Source: Internet
Author: User
How to define the structure of the Class-Linux general technology-Linux programming and kernel information. The following is a detailed description. 1: Basic concepts of Classes
The basic unit of a Java program is a class. A class is an instance of an object, or an object is a variable of the data type defined by the class. After you create a class, you can use it to create many objects you need. Java converts every executable component into a class.
The class is defined as follows:
Class classname extends superclassname
{
.....

}
Here, classname and superclassname are valid identifiers. The keyword extends indicates that classname is a subclass derived from superclassname. There is a class called Object, which is the root of all Java classes. If you want to define the direct subclass of an Object, you can omit the extends clause and the compiler will automatically include it. The following is a simple class definition.
Curly braces must be used at the beginning and end of the class definition. If you want to create a rectangle class, you can use the following code:
Public class Rectangle
{
......

}

2: Basic composition of Classes
A class usually contains two types of elements: Data and functions. We generally call it an attribute and a member function. In many cases, we also call a member function a method ). Combining data with code through classes forms a very popular concept of encapsulation. Naturally, the definition of a class also includes the above two parts.

Class




3: Create a class instance
In the Rectangle of the Rectangle class, you may want to write information about the Rectangle into the class, such as width and height. Of course, you can write other information, but the length and width are enough for a simple rectangle. The class is defined as follows:
Public class Retangle
{
Int width, height;
}
After you create your own class, you usually need to use it to do some work. You can achieve this by defining the instance-object of the class.
The object is created through new. The implementation of the member function is as follows: Rectangle myrect = new Rectangle. Of course, the object myrect does not do anything at this time; it only saves the length and width of the rectangle. How can we use internal data of an object after an object is created? The following are examples:
Myrect. width = 10;
Myrect. height = 20;
Class member functions are also referenced using the "." operator.
Related Article

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.