Java Objects and classes

Source: Internet
Author: User

1. Object

An object is an entity that can be perceived, has a unique name, has a set of state properties that represent an object, and the intrinsic behavior of the object. For example Zhang San this person, he has the name, occupation, eyes and other specific state attributes, can carry out said, run, eat and other methods.

Object, which is a variable of a certain type in Java code.
Object declaration : Type Object variable name
Object Creation : Object variable name =new type (); object declaration and creation can be done at once.
Send message to object: Object name. property, or object name. A method (the actual argument list) is a property or method that invokes the object.
For example:

Class testcalc{

public static void Main (string arg[])

{

Calculator c=new Calculator (); //Create Objects

C.plus (10,20)

}

}

Description: You can read and modify the properties of an object, or you can call a method of an object. The invocation of the object's properties and the return method must be done further, and the call to the object's non-return method is called directly.

2. Class

A class is a collection of objects that have the same properties and methods, an abstraction or a template for an object collection. For example, "people" category, "Dog" class, "Bicycle Class", "Vehicle" class.

To define a 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 Getmoneypermonth (String workname) {
...
}
}

Class dog{
...
}

Class a{
...
}

3. Relationship of classes and objects

A class is a collection of objects that have the same properties and methods, is an abstract conceptual model, and an object is an instance of a class and is specific.

4. Attribute field→ Field

Syntax: [domain modifier] Type property name [= attribute value] member variable → and variable declaration way

5. Method

Syntax: [method modifier] method returns the Type method name ([formal parameter type and Name list]) {

method body Content;

}

Method return type: Any method has a return type, a method if there is no result returned, the return type is void, the return value is of type int, the return type is int, and so on.

Properties and methods cannot have duplicate names, there are parentheses after the method, and no parentheses after the property System.out.println ()

System.out (). println () returns the type of the Out method

Formal parameters: The formal parameters used in the process of operation, the advantage of adopting formal parameters is that the method can run different results according to the actual parameters of the incoming amount.

method to return the result: Use the following statement within the method: return value

Note: The class name is capitalized, and the properties and methods are not very written;

Java Objects and classes

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.