Java Learning Notes <3> object-oriented correlation

Source: Internet
Author: User

The basic idea of object-oriented

This paper constructs the software system from the reality of the real world, and uses the natural way of thinking in the structure of the system, such as abstraction, classification

following and polymerization, polymorphism and so on.

Relationship of classes and objects

The data type specified by the class is stored in the object, and the object can invoke the method of the class.

Java Object-oriented

The <1> object is the core of the Java program, all objects.

The <2> object can be seen as a wrapper for static attributes (member variables) and dynamic properties (methods).

The <3> class is a template that innovates the same type of object, defining the member variables and methods that the type object should have.

Definition of Class

Member variables can be in any one of the Java language data types (including basic data types and reference data types)

Member variables are scoped to the entire class body.

You can initialize a member variable when it is defined, such as int age = 20 above; If not initialized, Java is initialized with the default values.

Default Initialization table:

Reference

String s; A reference to a string type is declared, but it does not point to an object.

s = new String ("Hello World"); Use the new statement to create an object of type string and point to it with S.

Creation and use of objects

<1> must create an object using the New keyword

<2> use objects (references). Member variables to refer to the member variables of an object

<3> Use Objects (References). Method + parameter list to invoke method of object

<4> different member variable storage space for each object of the same class

<5> share methods of this class with each object of the same category (in code Snippets)

Construction method

<1> Create a new object using the new+ construction method

<2> constructors are functions that are defined in a class to initialize an object

<3> constructor and class name are the same and no return value

<4> using constructors to initialize the object's member variables when creating an object

<5> when there is no constructor, the compiler automatically adds a shape such as the class name () {...} The constructor

Overload

Method names are the same and the parameters are different. When called, the corresponding method is selected according to the corresponding parameters.

this keyword

<1> using the This keyword in a method definition for a class represents a reference to an object that uses the method

<2> Use the this pointer when you must indicate who is using the method's object

<3> Sometimes this pointer can be used to handle the same name of the parameter in the method as the member variable

<4>this can be seen as a variable, and the value is a reference to the current object

Static keyword

<1> in a class, the member variable declared with static is a static member variable, which is the public variable of the class that is initialized the first time it is used.

<2> for all objects of the class, the static variable has only one copy

<3> the method declared with static is a static method, and when the method is called, the object's reference is not passed to it, so in the static method

You cannot access a member that is not static.

<4> can access static members through object references or class names.

Package and Import

To facilitate the management of a wide range of classes in large software systems, addressing naming conflicts, the Java Ingestion Package (pack) mechanism

The <1>package statement is the first statement in the Java source file that specifies the package that contains the class defined in the file

The <2>java compiler has a package that corresponds to the directory management of the file system. Indicates the level of the package. Such as:

Package COM.SXT//The file is located in the. \COM\SXT Directory

<3> If you are packaging a class, you must use the full name of the class when you use it. Example: Com.sxt.Myclass

You can also use import to introduce the classes you want to use at the beginning of the file, such as: Import Com.sxt.Myclass;

Summary:

The main package Description:

Inherited

Implementation of class inheritance mechanism using extends keyword in <1>java

<2> by inheritance, subclasses have all members of the base class

<3>java only supports single inheritance

Access control

Override of Method

<1> can override inherited methods in subclasses

The <2> override method and the overridden method have the same return type, method name, and parameter table

<3> overriding methods cannot use more restrictive access than overridden methods

Super keyword

Use the Super keyword in Java to refer to the components of the base class, such as:

How to construct in inheritance

In the construction of the <1> subclass, you must call the construction method of the base class

<2> subclasses can use Super (...) in their own constructors. Calls the construction method of the base class.

<3> can use this (...) Call this class for additional constructor methods.

<4> If you call super, you must write the first line of the subclass constructor

<5> if the subclass does not display the constructor method that calls the base class, the system automatically calls the parameterless constructor of the base class

<6> in 5, the Destructors class does not have a parameterless constructor and the compilation error

Object class

The <1>object class is the foundation of all Java classes

<2> if the class declaration does not use extends to indicate its base class, the default base class is the object class

ToString Method

<1> when a string is connected to other types of data, such as SYSTEM.OUT.PRINTLN (name), the ToString method is automatically called

<2> can override the ToString method in the user's custom type as needed

Equals method

<1> logical way to provide object equality

<2> can override the Equals method in a user's custom type as needed

Object transformation

<1> a reference to a base class can point to an object of its child class

<2> a reference to a base class cannot call a newly added member variable or method of a subclass

<3> can determine whether the object pointed to by the reference type variable is part of the class or subclass of the class by instanceof the class name.

Dynamic binding and polymorphism

Dynamic binding refers to the actual type that is referenced during run time, not compile time, and calls its corresponding method according to its actual type.

Conditions:

<1> to have inheritance

<2> to have a rewrite

<3> base class reference to child class object

Abstract class

<1> When you decorate a class with the abstract keyword, this class is called an abstract class. When a method is decorated, it is called an abstract method.

<2> classes that contain abstract methods must be declared as abstract classes.

<3> abstract classes must be inherited, and abstract methods must be overridden.

<4> abstract classes are not instantiated.

<5> abstract methods simply declare and do not need to be implemented.

Final keyword

The value of the <1>final variable cannot be changed

The <2>final method cannot be overridden

The <3>final class cannot be inherited

Interface

<1> interfaces: Collections of abstract methods and constant value definitions

<2> Essentially, an interface is a special kind of abstract class that includes only constants and methods, without the implementation of variables and methods

Example:

Interface Features

<1> multiple unrelated classes can implement the same interface

<2> A class can implement multiple unrelated interfaces

<3> similar to inheritance, there is polymorphism between interfaces and implementation classes

The member variables in the <4> interface are public static final type by default, and can only be this type

The <5> interface can only define abstract methods, which are public by default and can only be of the public type.

The <6> interface can inherit other interfaces and add new properties and methods

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.