Java Notes Collation (c), classes and methods in Java

Source: Internet
Author: User
Tags modifiers

Class

A class is an important concept in Java that can be interpreted as an abstraction of an object, an abstract object that contains variables (to describe the properties of the object) and methods (to describe what the object can do), and the individual members of the class can call each other (static A decorated member cannot access a member without a static adornment.

Each class must have one or more constructor methods , which are used to instantiate the abstract object.

Class is defined in the form
class class Name {    constructor;    member variables;    Method }

constructors, member variables, and methods in a class can be 0 or more

Class modifiers can use public, final, abstract, or no modifiers, abstract modified classes are abstracted as classes

Inner class:

Refers to a class defined in a class

 Public class a{    int  A;     int b;      Public void C () {}      Public class b{...    }

the inner class accesses the external class's private property or method using the external class. This property (method) to implement the

the inner class accesses the static property or method of the external class and can use the class name directly. Property (method)

Method

In object-oriented language, classes are the only class citizens, and the whole system consists of one class after another, and the method must belong to a class or object

Once a method is defined in the class of a class, if the method uses the static modifier, the method belongs to the class, otherwise the method belongs to an instance of the class.

Attention:

1. Methods cannot be defined independently, methods can only be defined within the class

2. In a logical sense, the method either belongs to the class itself (using the static adornment) or to an object of that class

3. You can never execute a method independently, and the execution method must use the class or object as the caller

  

Passing the parameters of a method

If you declare a method with a formal parameter declaration, you must specify the parameter values for the parameters when the method is called, and the parameter values that are actually passed to the formal parameter when the method is called are also called arguments

The method of defining multiple formal parameters is passed, but the variable length parameter can only be at the end of the formal parameter list, a method can only contain a variable length parameter, the variable length parameter essence is an array, so when a method containing the length deformable parameter is transferred, the variable length parameter may pass through multiple parameters. You can also pass in an array. For example:

  

 Public Static void Test (int  a,string...books) {}

Construction method

A constructor method is a special presence in a class that is a method with the same name as the class and no return value type. The creation of an object is accomplished by means of a construction method, whose function is to initialize the object. Constructor methods are called automatically when a class instantiates an object.

the construction method has the following characteristics

1. Construction method has the same name as class

2, each class can have more than one construction method

3, the construction method can have 0, one or more than 1 parameters

4. The construction method has no return value

5, the construction method is always accompanied by the execution of the new operator is called

Recursive method

A method body calls itself, called method recursion. The method recursion contains an implicit loop that repeats a piece of code, but does not require loop control

Overloading of methods

multiple methods, with the same name and different parameters, generate overloads (regardless of the return value).

The compiler picks out the corresponding method by matching the parameter type given by each method with the value used by the particular method call.

For example:

Newnew person ("Zhang San", 50)

The compiler determines which construction method to call by passing the parameters of the person

Create an object using the class name Method name = constructor of the new class;

For example

Newnew person ("Zhang San", 40)

In fact, the above object creation is made up of person P1 = null and P1 = new Person two parts

The person P1 = null part is called the "Declaration object", P1 = new person part is called "Instantiation of the object", in reality programming often merge two steps

Override of Method

Method overrides exist in Java inheritance , the subclass inherits the method of the parent class and often needs to modify the method of the parent class, and the method of modifying the parent class in the subclass is called the override of the method.

Attention:

1, the method of rewriting needs to satisfy: The method name is the same, the return value type is the same, the parameters are the same

2, the quilt class overrides the method, cannot have the more strict access permission than the parent class method

Differences between method overrides and method overloads

Overload

Rewrite

Words

Overloading

overriding

Defined

Method name is the same, parameter list is different

Method name, return value, parameter list are all the same

Permissions

No request

The overridden method cannot be more restrictive than the parent class's method permissions

Range

Same class

Inherited

Permission modifiers and scopes for methods and properties

Private < Default < protected < public

Private, proprietary, accessible only in the current class

Default, which can be accessed within the package

Protected, protection, can be used in this package, under this class and sub-class

Public, open, accessible throughout the project

Object

It says that the class itself is an abstract object, and we create an instance of the class by invoking the constructor in the class to instantiate the abstract object, using the New keyword to invoke the constructor of the class.

Once you have created an object, you can use this exclusive, method that can be used to invoke the object's instance variable.

In an object-oriented programming language, all transactions can be considered as an object.

Java Notes collation (c), classes and methods in Java

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.