Java class, definition of java class

Source: Internet
Author: User

Java class, definition of java class

1. Four access controllers

Private, not specified, protected, public

 

2. No return value for the constructor

It is because the constructor returns the Instance Object of the class by default. Therefore, the return value does not need to be written.

 

3. static

Static indicates the ownership of the class. Instance:

Public class

{

Public void ()

{

System. out. println ("");

}

Public void B ()

{

This. ();
}

}

There is no problem in writing this way, but if we change it to this:

Public class

{

Public void ()

{

System. out. println ("");

}

 

Public static void B ()

{

This. ();

}

}

Because B () is owned by A class, you can access. B (), but there will be problems at this time, because this object is used in Method B, but no object has been created, so this object does not exist at all.

Conclusion: static members cannot directly access non-static members.

 

4. a variable number of parameters should be placed at the end of the method. Example: public void test (int a, String... s)

 

5. member variables and local variables

The system automatically allocates memory space for member variables. After the memory space is allocated, the system automatically specifies the initial value for the member variables.

After a local variable is defined, it must be explicitly initialized before it can be used. The system does not initialize the local variable.

 

6. package, import, and import static

Instance:

Package lee;

Public class Hello

{

Public static void main (String [] args)

{

System. out. println ("Hello World! ");

}

}

If you directly use the javac Hello. java command to compile this file, a Hello. class file will be generated in the current path, instead of the lee folder.

If you use the java-d Hello. java command for compiling, the lee folder will be generated.

We recommend that you always use the-d option during compilation.

 

You can use import to import a single class or all classes in the corresponding package.

You can use import static to import one or all static fields and methods in a specified class.

 

7. class inheritance

Keyword extends.

Override the parent class method, that is, the Child class overrides the parent class method.

The rule is: The method names are the same, and the parameter list is the same. The return type of the subclass method should be smaller or equal than the return type of the parent method, the exception class thrown by the subclass method declaration should be smaller or equal than the exception class thrown by the parent class method declaration; the access permission of the subclass method should be greater or equal than the access permission of the parent class method.

 

If you need to call the instance method whose parent class is overwritten in the subclass method, you can use the super limitation to call the instance method whose parent class is overwritten.

The Child class calls the parent class constructor and uses super to call the parent class constructor. It must appear in the first line.

 

8. Polymorphism

There are two types of variables referenced in java: compile-time type and runtime type.

Compile-time type: the type specified by declare variable

Runtime type: the type used to assign values to variables.

If the two types are different, they are called polymorphism. Instance:

Class BaseClass

{

Public int a = 6;

Public void base ()

{

System. out. println ("normal method of the parent class ");

}

Public void test ()

{

System. out. println ("override method of the parent class ");

}

}

 

Public class SubClass extends BaseClass

{

Public String a = "java ";

Public void test ()

{

System. out. println ("override parent class method of subclass ");

}

 

Public void sub ()

{

System. out. println ("normal method of subclass ");

}

 

Public static void main (String [] args)

{

BaseClass ploymophicBc = new SubClass ();

System. out. println (ploymophicBc. book );

PloymophicBc. base ();

PloymophicBc. test ();

// PloymophicBc. sub (); the following error occurs because the BaseClass class does not provide the sub method.

}

}

9. Forced type conversion

When writing a java program, the referenced variable can only call the method of the compile-time type, but cannot call the method of the runtime type, even if the actually referenced object actually contains the method. If you want this reference variable to call the method of its runtime type, you must forcibly convert its type to the runtime type. The type conversion operator is required to force the conversion.

10. instanceof

The first operand of the instanceof operator is usually a reference type variable, and the second operand is usually a class (or an interface). It is used to determine whether the previous object is a later class, or its subclass or implementation class instance. If yes, true is returned; otherwise, false is returned.

Generally, instanceof is used to determine whether an object can be forced type conversion before forced type conversion.

11. Inheritance and combination

Both inheritance and combination can meet the requirements of reusable classes. Is Inheritance or combination used?

Inheritance is a kind of "is-a" relationship, while a combination represents the "has-a" relationship.

12. initialize the block

[Modifier] {

...

}

The modifier can only be static. The modifier is executed only when a java object is created and before the constructor is executed.

The initialization block and the default value specified by the instance Field can be considered as the initialization code of the object. The execution sequence is the same as that in the source program.

Static initialization block. If you use static modification to initialize the block, the initialization block becomes a static initialization block, and the static initialization block belongs to the class.

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.