[Java] Objects and Classes

LocalData class

java.time.LocalDate;

static LocalTime now();
构造一个表示当前日期的对象。

static LocalTime of(int year,int month,int day);
构造一个表示给定日期的对象。

int getYear();
int getMonthValue();
int getDayOfMonth();
得到当前日期的年、月和日。

DayofWeek getDayOfWeek
得到当前日期是星期几,作为DayOfWeek类的一个实例返回。
调用getValue来得到1~7之间的一个数,表示这是星期几,1表示星期一,7表示星期日。

LocalDate plusDays(int n)
LocalDate minusDays(int n)
生成当前日期之后或之前n天的日期。

Constructor

In the constructors C ++ and Java is the biggest difference, Java All new construction must use the heap structure.

It is noteworthy that, not to construct a local variable in the function declaration with the same name as the domain instance.

In C ++, the method defined outside of the general class, the method for establishing a defined inline inside the class. But not in Java this argument, whether it is an inline function, the virtual machine will have to decide.


Change and accessor

Change is actually a set method. Make changes to member variables by set method.

It is get accessor method. Gets the value of a member variable with the get method.

It should be noted that, for the get method not to return to variable objects . Variable object, which means that you can not change is the change in value, after all, want to change the values need to use set methods.


final

Added keyword final member variable, indicating when the constructed object must be initialized . And after the operation can not modify them .


static

Static member variables representing all the objects have both a variable.

Static member methods can only access static member variables. Static member invoked through the class name. (Although objects can be, but is not recommended)


Parameter passing (important)

In Java, all parameters are passed in accordance with the values ​​passed way.

Common variable transmission, is passed by value.

Mass class object, is passed by value. However, class objects on the heap using the new configuration, so that the class object is actually stored is an address, the address corresponding to the object on the heap. So if the parameter passed is the class object, that is, in fact, pass a copy of the value of that address.

Again:

  • For basic data type is "=" assignment change is a direct memory address (storage unit) of the value.
  • For reference type is "=" assignment change the memory address pointed to by the reference variable.

Object construction

Java support after writing a member variable in the class to an initial value directly. Even so:

class Employee
{    
    private static int nextId;
    private int id = assignl();
    ...
    private static int assignl()
    {
        int r = nextId; 
        nextId++; 
        return r;
    }
}

Note:

  1. In the Java constructor if the first one is the this (..); then the sentence can call another class constructor.
  2. There is also the concept of Java in the initialization block constructor. example:
class Employee
{    
    private static int nextId;
    private int id;
    ...
    //初始化块
    {
        id = 1;
    }
    //静态初始化块
    static
    {
        nextId = 1;
    }
}

The initialization block will be executed before the constructor body when calling the constructor.


package

Java uses package (package) would like to organize. The role of the package is to prevent conflict in the name of life.

Compiled from the point of view, no relationship between nested packages. For example: java.util packages and packages java.util.jar

Guide package: using the import statement. This statement is similar in C ++ namespace and using.

Package: Package Penalty for statement.

Path class must match the package name.


Documentation Comments

JDK includes a tool called javadoc, it can generate an HTML document from the source file. Add in the source code / ** comment started, code and comments can be stored in one place.

Class Notes

Method Notes

  1. @param variable description
  • The current methods will be labeled "param" (parameter) to add an entry portion. All @param mark a method must be put together.
  1. @return description
  • This will mark the current method to add "return" (return) section.
  1. @throws class description
  • This tag will add a comment to represent this method may throw.


Design skills classes

  1. To ensure that private data
  2. Ensure data initialization
  3. Do not use too much class in basic types
  4. The excess duty class decomposition
  5. Class name method name to be able to reflect their responsibilities
  6. Priority class immutable

Guess you like

Origin www.cnblogs.com/LampsAsarum/p/12222043.html