java--Object-oriented (i)

Source: Internet
Author: User

Class:

Overview: Abstract description of a class of things (through which things can abstract classes, such as humans, vehicles, etc.)

Defines the syntax for a class: [Modifier] class Name {class member;}

Description

Modifier: decorates or restricts the current class (Public,private,protect, etc.)

Class: The keyword for the category definition.

Class Name: Custom, the first letter of each word should be capitalized (can be Chinese text, but not recommended to be named)

Class Member: All content defined within the class. Include: Property (String name; etc.), Method (constructor)

The implementation of the class can be divided into the following steps:

1. Find an object (that is, find the object to describe)

2. Analyzing Objects (analyzing objects, finding properties and behaviors)

3. Description Class (definition Class)

4. Using classes (creating objects)

Object:

Overview: A real entity in a class of things (objects belong to classes).

Instantiated object: Class Name Object name = new constructor ();

Description

Object Name: Custom, the naming convention is the same as the variable.

NEW: operator to invoke the constructor for object initialization (i.e. memory request);

Constructor: A member variable that is used to request memory and initialize a class.

constructor function:

Overview: A special method. Can be: constructor, construction method, Constructor. The constructor allows you to initialize an object.

Syntax: [modifier] Function name (consistent with class name) (argument list) {[function body;]}

Characteristics:

1. Constructor does not return a value type

2. Function names must have the same name as the class name in which they reside

3. Constructors cannot be called arbitrarily and can only be invoked with the new operator when instantiating an object

4. If the definition constructor is not displayed in the code, the compiler will provide a parameterless constructor, and if a custom constructor is displayed, the compiler will not provide any constructors.

Package:

Overview: Logical grouping of source code files. The management of the source code through the package can be implemented to define a file with the same name (the file cannot be in the same package);

Naming conventions:

1. All the lowercase letters

2. Cannot use "." Start or end

3. Define Eg:com.xxxx.cc.dao in order from large to small

Scope of the variable:

Overview: The range of variables that can be used.

Scope:

Global variables: can be used in any method of the entire class.

Local variables: can only be used in the current code snippet; (a pair of {} is called a code snippet)

About this:

Overview: The This keyword represents the current object.

Role: The authority and global variable names (naming conflicts), the This keyword can be used to distinguish between local variables and member variables (using this is called the member variable (global variables, also the properties of the Class));

Process-oriented and object-oriented:

Process-oriented: the process-oriented approach is also called function-oriented programming by means of describing each process. The disadvantage of process-oriented programming is that the reusability of the Code (reusability) is low and is not suitable for writing large projects.

Object-oriented: Encapsulates the method of each procedure of a thing into a class file, allowing time to invoke each function through an object's operation. Object-oriented advantages: class files can be common, and the most current pair improves the reusability of the entire class file. So object-oriented programming is used by current projects.

Three main features of object-oriented:

Encapsulation, inheritance, polymorphism

Packaging:

Overview: The process of wrapping data, methods into a class is called a class encapsulation. The wrapper consumer does not care about the implementation within the class, as long as the members of the class are called.

Packaging:

Encapsulation of classes: Encapsulation into class files

Encapsulation of features: Define some code as a method

Encapsulation of data: privatization of the fields of the class and access to the data through specific means (the encapsulation of the data is actually the protection of the data)

Encapsulation of data:

Implementation method: The member field of the class is decorated by the access modifier private (the private-decorated member field is self-contained in the current class), and then the interface for data access is provided through getter and setter accessors.

Implementation steps:

1. Privatization of variables

2, get and set accessors (can exist at the same time, can exist only one)

Benefits of using GET and set:

The value of the data can be set efficiently through the set accessor.

You can get the value of a private field through an individual accessor.

Inherited:

Overview: One of the three characteristics of object-oriented, through inheritance can solve the problem of code redundancy, improve the reusability of code.

implementation: [Modifier] class class name 1 extends class name 2{}

Description

Class Name 1: Called a subclass, also known as a derived class.

Class Name 2: Called the parent class, in some languages also becomes the base class, the superclass (object is the parent class of all classes, called base class, superclass)

Benefits of inheritance: Slow code redundancy, improved code reusability

What can a subclass inherit from a parent class?

1. Subclasses can inherit all non-private members of the parent class

2. Parent class in a different package, a member subclass defined by the default modifier in the parent class cannot inherit

3. The constructor subclass of the parent class cannot be inherited.

Modifier

java--Object-oriented (i)

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.