Java Basics-Classes and objects

Source: Internet
Author: User

Classes and objects

Object: An object is an instance of a class (objects are not looking for a girlfriend), stateful and behavioral. For example, a dog is an object whose state is: color, name, variety, behavior: wagging the tail, barking, eating, etc.
Class: A class is a template that describes the behavior and state of a class of objects. Determines the set of attributes (attributes) and behaviors (methods) that an object will have to determine object information, a set of objects that have the same properties and methods
Software objects also have states and behaviors. The state of the software object is the attribute, and the behavior is manifested by the method.
Hash encoding : Each object has its own hash code, which, when called by the program, finds the object's location in memory by looking for the hash encoding of the object.
In software development, the internal state of the method operation object is changed, and the mutual invocation of the object is done by means of the method.
Properties: Characteristics of an object
Method: An action performed by an object

Defining classes

Class with the first letter capitalized to create the class
Defining objects
Class Name Object name = new class name ();
Using the properties of an object
Phone.screen
Ways to use objects
Phone.sendmessage ();

Construction method

Each class has a constructor method. If you do not explicitly define a construction method for a class, the Java compiler will provide a default constructor for the class.

public class Puppy{    public Puppy(){    }     public Puppy(String name){        // 这个构造器仅有一个参数:name    }}

Create a new object when you create an object, you execute a construction method.
Defines a method for initializing an object in a Java class
The constructor method has the same name as the class and no return value
The construction method can also assign a reasonable value to the object

Creating objects

Declaration: Declares an object, including the object name and object type.
Instantiation: Use the keyword new to create an object.
Initialize: When you create an object with new, the constructor method is called to initialize the object.

Puppy mypuppy = new Puppy ("Tommy");

Accessing instance variables and methods

Access member variables and member methods through the objects you have created, as follows:
```
/* Instantiate object /
Objectreference = new Constructor ();
/
access a variable in a class /
Objectreference.variablename;
/
access the methods in the class */
Objectreference.methodname ();

Java Basics-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.