Java objects and Classes

Source: Internet
Author: User

Java objects and Classes

1. ObjectObject

An object is an entity that can be perceived. It has a unique name, a group of State attributes of the object, and inherent behavior capabilities of the object. For example, Michael has specific status attributes such as name, occupation, and eye, and can implement the methods of speaking, running, and eating. Object, which is a type of variable in java code.
ObjectStatement: Type object variable name
ObjectCreate: Object variable name = new type (); object declaration and creation can be completed once.
For example:

Class Testcalc {

Public static void main (string arg [])

{

Calculator c = new Calculator (); // create an object (declare an object, including the Object Name and object type); create an object (instantiate) using the keyword new ); when an object is created using new, the constructor is called to initialize the object (initialization ))

C. plus (10, 20)

}

}

Send a message to an object (accessing the instantiated object and method): the object name. attribute, or the object name. Method (actual parameter list) is to call an attribute or method of the object. For example, in the above example:

C. variablename; // attributes in the category class

C. plus (); // methods in the callback class

 

Note: You can read and modify attributes of an object, or call a method of an object. The attributes and returned methods of the called object must be further processed. The method that does not return the object must be called directly.

2. ClassClass

A class is a set of objects with the same attributes and methods. It is an abstraction or template of an object set. For example, people, dogs, bicycles, and transportation.

Definition class:
[Class modifier] class name {
Class Member
}

For example:

Class Human {
String className = "human ";
Int age, height;
String name;
Void speak (){
System. out. println ("Hello everyone, hahaha ");
}
Int salary (String workName ){
...
}
}

Class Dog {
...
}

Class {
...
}

A class can contain the following types of variables:
Local variable: Variables defined in methods, constructor methods, or statement blocks are called local variables. Both the variable declaration and initialization are in the method. After the method is completed, the variable will be automatically destroyed.
Member variables: Member variables are variables defined in the class and outside the method body. This type of variable is instantiated when an object is created. Member variables can be accessed by methods, constructor methods, and statement blocks of specific classes.
Class variable: Class variables are also declared in the class, except the method body, but must be declared as static type.

A class can have multiple methods. In the preceding example, speak () and salary () are all Human class methods.

 

3. Relationship between classes and objects

A class is a set of objects with the same attributes and methods. It is an abstract conceptual model, and an object is a class instance, which is specific.

 

4. AttributesField→ Domain

Syntax: [domain modifier] type attribute name [= attribute value] member variable → same as the declaration method of the Variable

 

5. MethodMethod

Syntax: [method modifier] method return type method name ([list of formal parameter types and names]) {

Method subject content;

}

Method return type: Any method has a return type. If a method does not return any results, the return type is void. If the return type is int, the return type is int, and so on.

The attribute and method cannot be duplicated. The method is followed by parentheses, and there is no parentheses after the attribute. system. out. println ()

Format parameters: formal parameters used during method running. The advantage of using formal parameters is that different results can be run according to the actual parameters passed in during method running.

Return Method: Use the following statement in the method: Return a value

Note: The first letter of the class name is in upper case, and the attributes and methods are not in upper case;

 

6.Constructor

The constructor is a member of a class. The constructor name is the same as the class name. It initializes the attribute value of the class object when creating the Class Object and returns the object of this type.
Features:
1. The name is the same as the class name;
2. The syntax is similar to a method, but there is no return type, and no void is returned;
3. It is called when an object is created, for example, A a = new A (); A () is the constructor;
4. If no declarative constructor exists in the class, the jvm will automatically add a constructor with null content without parameters during actual running;
5. the constructor can use access modifiers, but cannot use other modifiers of members. This is the second difference between the constructor and the method. In addition, the constructor cannot be inherited;
6. A class can have multiple Constructors (specify different parameter types or numbers-overload );
7. Assign real parameters in the constructor format when creating an object;

For example:

Class Box {
Private int width, length, deepth; // use the access modifier private;
Box (int w) {// only one parameter Type constructor with the same name as the class name;
Width = w;
Length = l;
Deepth = w;
}
......
}

Class MyBox {
Public static void main (String [] arg ){
Box box1 = new Box (10); // feature 7. When creating an object, assign real parameters according to the format of the constructor;
......
}
}

 

7. Reload

Repeat the loading method and constructor. Overload: multiple methods and constructors with the same name, but passed parameters and different types.

For example:

Class {
Void show (){
System. out. println ("");
}
Void show (String name ){
System. out. println (name );
}
Void show (int age ){
System. out. println ("I am" + age + "old .");
}
Void show (int ){
...
}
}

A a = new ();
A. show (); // The returned result is;
A. show ("jack"); // The returned result is name;
A. show (20); // The returned result is I am 20 old;

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.