Objects and classes of Java learning notes (1/3)

Source: Internet
Author: User

Java Object-Oriented Programming (OOP) and how to write your own classes. We know that Java is completely object-oriented, and we must be familiar with OOP to write better Java programs. This is very different from the "structured" and "procedural" programming in the past 1970s S.

I. Category

A class is a blueprint or template for constructing objects. The construct object is the process of creating an instance of the class.

Encapsulation is an important concept related to classes. Formally, encapsulation combines data and behavior in a package, and hides the data implementation method from the object user. The data of an object is called an instance field, and the process of operating data is called a method ).

Inheritance allows one class in Java to be extended by another class. In fact, all classes in Java inherit the most primitive superclass-Object. The inherited new class has all the attributes and methods of the base class.

Ii. Objects

Three main features of an object:

1. behavior-What operations or methods are applied to objects?
2. state-how does the object instance domain change when a method is applied?
3. identity-how to identify different objects with the same behavior and status?
Iii. Relationships between classes

There are three common types:

1. Dependency (uses-a) -- a class method needs to manipulate the object of another class;
2. Aggregation (has-a) -- an object of a class contains an object of another class;
3. Inheritance (is-a) -- all attributes and methods of another class can be extended and additional functions can be added.
Iv. Custom classes

1. class definition form

The Code is as follows: Copy code
Class example
{
Constructor1
Constructor2
...
Method1
Method2
...
Field1
Field2
...
}

For example:

The Code is as follows: Copy code
Class Student
{
// Constructor
Public Student (String n, int num, int year, int month, int day)
{
Name = n;
Number = num;
GregorianCalendar calendar = new GregorianCalendar (year, month-1, day );
Birthday = calendar. getTime ();
}
// Method
Public String getName ()
{
Return name;
}
Public int getNumber ()
{
Return number;
}
Public Date getBirthday ()
{
Return birthday;
}
// Domain
Private String name;
Private int number;
Private Date birthday;
}

2. implicit and explicit parameters

The Code is as follows: Copy code
Public void setNumber (int n)
{
Number = n;
} Student student = new Student ("Jiankun Lei", 136524,1991, 1,1 );
Student. setNumber (1000 );

// Student is an implicit parameter, and 1000 is an explicit parameter, which is an implicit (implicit) parameter. Class Object before the method name; explicit) parameters are parameters in the brackets after the method name.

3. class-based Access Permissions

Remember: methods can access the private data of the called object. That is to say, a class method can access the private data of all objects in the class.

For example, you can define a method to determine the equals of two students:

The Code is as follows: Copy code
Boolean equals (Student other)
{
Return number. equals (other. number); // number is the private data of the Student class.
}

4. final Instance domain

Define the instance domain in the class as final. When constructing an object, you must initialize such a domain. After the object is constructed, it cannot be modified.

The Code is as follows: Copy code
Class Student
{
...
Private final String name;
}

5. Static domain and static method

Static domain: defines a domain as static. Each class can have only one such domain. It is a class and does not belong to any independent object.

The Code is as follows: Copy code
Class Employee
{
...
Private int id;
Private static int nextId;
...
} Public void setId ()
{
Id = nextId;
NextId ++;
}

1 2 3

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.