Java Core Technology Chapter fourth class and Object

Source: Internet
Author: User
Tags class definition

class: constructs the template and blueprint for the object. The process of constructing an object from a class is called creating an instance of the class.

Characteristics of the object:

    • Behavior of the object
    • The state of the object
    • The identity of the object

The relationships between classes:

    • Depend on
    • Polymerization
    • Inherited

object and Object variables:

New Date ()

New GregorianCalendar ()

New GregorianCalendar (1999, 11, 31)

New GregorianCalendar (1999, Calendar.december, 31)

New GregorianCalendar (1999, Calendar.december, 31, 23, 59, 59)

GregorianCalendar now = new Gregriancalendar ();

int month = Now.get (calendar.month);

int weekday = Now.get (Calendar.day_of_week);

GregorianCalendar deadline= new GregorianCalendar (...); Parameter can not be ...

Deadline.set (Calendar.year, 2001);

Deadline.set (Calendar.month, Calendar.april);

Deadline.set (Calendar.day_of_month, 15);

Deadline.add (Calendar.month, 3); If you pass a negative value, the date moves forward

GregorianCalendar calendar= New GregorianCalendar (year, month, day);

Date hireday = Calendar.gettime ();

GregorianCalendar calendar= new GregorianCalendar ();

Calendar.settime (Hireday);

int year = Calendar.get (calendar.year);

GregorianCalendar d= new GregorianCalendar ();

int today = D.get (Calendar.day_of_month);

int month = D.get (calendar.month);

D.set (Calendar.day_of_month, 1);

int weekday = D.get (Calendar.day_of_week);

int firstdayofweek = D.getfirstdayofweek ();

String weekdaynames[] = new DateFormatSymbols (). Getshortweekdays (); The first few letters of the day of the week

Constructor:

You cannot call a constructor on an already existing object to achieve the purpose of re-setting the instance domain.

    • The constructor has the same name as the class
    • Each class can have more than one constructor
    • Constructors can have 0, 1, or more parameters
    • The constructor has no return value
    • Constructors are always called with the new operation

Do not define a local variable with the same name as the instance field in the constructor.

Implicit and Explicit parameters:

The class object that appears before the method name is called an implicit parameter.

In each method, the keyword this represents an implicit parameter.

Private methods:

When implementing a class, all data fields should be set to private because the public data is very dangerous.

In Java, in order to implement a private method, you only need to change the keyword public to private.

For private methods, you do not have to preserve the original method if you use other methods to implement the appropriate operation.

Final instance domain:

You can define an instance field as final. You must initialize such a domain when you build the object. That is, you must ensure that the value of the field is set after each constructor is executed, and that it cannot be modified in subsequent operations.

Most of the final modifiers are applied to the base type field, or to the domain of the immutable class (the class is immutable, such as String) if each method in the class does not change its object.

Private final Date hiredate;

Simply means that the object reference stored in the HireDate variable cannot be changed after the object is constructed, and does not imply that the HireDate object is a constant. Any method can invoke the SetTime change on an object referenced by HireDate.

Static domain and Static methods:

Static domain:

If you define a domain as static, there is only one such domain in each class. Each object has a copy of its own for all instance domains.

Static constants:

The domain is not generally designed to be public, but static constants are not problematic.

static method:

A static method is a method that cannot be manipulated against an object.

int n = employee.getnextid ();

Use static methods in the following two scenarios:

    • A method does not require access to the object state, and its required parameters are provided through the display parameters.
    • A method only needs to access the static domain of the class.

Method Parameters:

Java programming languages are always called by value. That is, the method obtains a copy of all the parameter values, in particular, the method cannot modify the contents of any parameter variables passed to it.

Summarize the usage of the method parameters in the Java programming language:

    • A method cannot modify a parameter of a base data type (that is, numeric and Boolean)
    • A method can change the state of an object parameter
    • A method cannot have an object argument referencing a new object

Object constructs:

Method overloads:

The return type is not part of the method signature. That is, there cannot be two methods that have the same name, same parameter type, but return different types of values.

Default Domain initialization:

If the default value is not displayed in the constructor: The value is 0, the Boolean value is False, and the object reference is null. However, if the domain is not explicitly initialized, it can affect the readability of the code.

No parameter constructor:

If you do not write a constructor when you write a class, the system provides a parameterless constructor. This constructor sets all instance fields to the default values. If at least one constructor is provided in a class, but no parameterless constructor is provided, it is considered illegal to construct the object if it is not supplied.

Explicit domain initialization:

Because the constructor method of a class can be overloaded, you can set the initial state of the instance domain of the class in many forms.

Call another constructor:

The keyword this refers to the implicit argument of the method. However, this parameter has another meaning. If the first statement of the constructor is a shape such as this (...). ), this constructor will call another constructor of the same class. Cases:

Public Employee (double s) {

This ("employee#" + NextID, s);

nextid++;

}

Initialize BLOCK:

There are two ways to initialize a data field before:

    • Setting values in the constructor
    • Assigning values in declarations

In fact, Java has a third mechanism: initialization blocks. In a declaration of a class, you can include multiple blocks of code. As long as the objects of the class are constructed, the blocks are executed.

The initialization block is run first before the body part of the constructor is run.

To invoke the concrete processing steps of the constructor:

    1. All data fields are initialized to default values (0,false,null)
    2. All domain initialization statements and initialization blocks are executed sequentially, in the order in which they are declared in the class.
    3. If the second constructor is called by the first row of the constructor, the second constructor principal is executed.
    4. Executes the constructor body.

You can initialize a static domain by providing an initialization value or by using a static initialization block. Place the code in a block and tag the keyword static.

When the class is first loaded, the static domain is initialized. As with instance domains, the default initial value is 0,false,null unless you explicitly set them to another value. All static initialization statements, as well as static initialization blocks, are executed in the order of the class definition.

Package:

Static import:

The import statement not only imports the class, but also adds the ability to import static and static fields.

Import static java.lang.system.*;

You can use the static and static fields of the system class without having to prefix the class name.

To put a class in a package:

To put a class into a package, you must place the name of the package at the beginning of the source file. If you do not place the package statement in the source file, the class in the source file is placed in a default packet.

The compiler does not check the directory structure when compiling the source files. So as long as the source file is not dependent on other packages, the compilation error does not occur even if it is not in the declared package, but a run error occurs because the virtual machine cannot find the file.

Package Scope:

The parts marked as public can be used on any class, and portions marked as private are only used by classes that define them. If public or private is not specified, this section (class, method, or variable) can be accessed by all methods in the same package.

Class Design Tips:

    1. Be sure to keep your data private
    2. Be sure to privatize the data
    3. Do not use too many basic types in a class
    4. Not all domains require a separate domain accessor and domain change
    5. Decompose a class with too many responsibilities
    6. The class name and method name should be able to reflect their responsibilities.

Java Core Technology Chapter fourth class and Object

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.