Characteristics of three java: encapsulation, polymorphism and inheritance

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/baidu_33094261/article/details/102760289

Object-oriented - Encapsulated

1.1
Classes and Objects

1.1.1 Classes and objects understanding

Object: things in the world

Class: The class is an abstraction of reality a class of things with common attributes and behavior of

A collection of data objects is a type of object classes, attribute classes and having the same behavior

Simple to understand: The class is a description of the reality of things

The composition of the class

属性:事物的特征

行为:事物能执行的操作

The relationship between classes and objects: class is a thing of the description, the object was something concrete existence.

1.1.2 class definition:

Composition class is composed by the properties and behavior.

The class definition step:

① the definition of class

② writing a class member variables

③ writing a class member method

{public class name of the class

// Member variables

Variable 1 Variable Data type 1;

Variable data type of the variable 2 2;

// member method

method 1;

Method 2;

}

Sample Code

/*

Phone categories:

Class Name:

  手机(Phone)

Member variables:

Brand (brand)

Price (price)

Member method:

Call (call)

Send text messages (sendMessage)

*/

public class Phone {

//Member variables

String brand;

int price;

// member method

public void
call() {

 System.out.println("打电话");

}

public void
sendMessage() {

System.out.println ( "texting");

}

}

1.1.3 use objects

Create an object format:

Object class name name = new class name ();

Members call format:

Object name. Member variables

Object name. Member method ()

1.2
Object Memory FIG.

Those who are new things out of heap memory

Own member variables are as defined in the stack memory

1.3
member variables and local variables difference
(% 5B% E5% A4% 96% E9% 93% BE% E5% 9B% BE% E7% 89% 87% E8% BD% AC% E5% AD% 98% E5% A4 % B1% E8% B4% A5 ,% E6% BA% 90% E7% AB% 99% E5% 8F% AF% E8% 83% BD% E6% 9C% 89% E9% 98% B2% E7% 9B% 97% E9% 93% BE% E6% 9C% BA% E5% 88% B6,% E5% BB% BA% E8% AE% AE% E5% B0% 86% E5% 9B% BE% E7% 89% 87 % E4% BF% 9D% E5 % AD% 98% E4% B8% 8B% E6% 9D% A5% E7% 9B% B4% E6% 8E% A5% E4% B8% 8A% E4% BC% A0% 28img -6uNPmoXU-1572090409967% 29% 28https: //blog.csdn.net/HXNLYW/article/details/94550158%29%5D)

1.4
packaging and construction methods

1.4.1
private

private is a modifier, member (member variables, member method) can be used to modify

Modified members can only access this class. For class he used, the need for a corresponding operation.

1.4.2
this

this modified variable used to refer to members of the variable major role (duplicate names to distinguish local variables and member variables)

Shaped member variables involved in the same process, without this variable is modified parameter refers

this time: the hidden member variables to address local variables

this: object class representatives in your reference. (Which method is called an object, this object represents on which)

Different methods of participation shaped member variables, variables without this modification refers to the member variable

this represents the reference to the current method call, which object method call, which will this object represents

1.4.3
Packaging ideas

Package Overview

Is facing one of the three features of the object (encapsulation, inheritance, polymorphism) is an object-oriented programming language to simulate the objective world, the objective world, member variables are hidden inside the object, the outside world can not be directly manipulated

封装原则

Hiding certain information classes within a class, do not allow direct access to an external program, but is achieved by a method provided by the class of hidden information and the access operation member variables private, to provide the corresponding getXxx () / setXxx () method

Benefits Package

Member variable to the control operation by the method, improves the security code to code encapsulating methods to improve the reusability of code.

1.4.4 Constructor

Constructor is a special method

Role: to create objects Student stu = new Student ();

format:

{public class name of the class

Modifier class name (parameter) {

}

}

Function: The main target is to complete the initialization data

Create a constructor

If not defined constructor, the system will be given a default constructor parameters if no defined construction methods, the system will no longer provide a default constructor

Overloaded constructor

The self-defined parameterized constructor, also using no-argument constructor must write a no-argument constructor

Recommended use

In both cases, all written by hand no constructor parameters

Important features!

You can use parameterized constructor, as a member variable is initialized

Object-oriented - Inheritance

2.1 inheritance definition, pros and cons, super, visit Features

2.1.1 definitions

Inherited format

Format: public class subclass name parent name extend {}

Example: public class Extend Zi
Fu {}

Parent Class: it may also be referred to as a base class, the superclass.

Subclasses: the derived class

Inherit the characteristics of neutron class:

① subclass can have the contents of the parent class

② subclasses can also own unique content

2.1.2 inherited the pros and cons

Lee: improve code reusability and maintainability

Disadvantages: increase the coupling between classes, subclasses of the parent class changes also need to follow the changes, weakening the independence of the subclass

Using inheritance time:

Inheritance reflect the relationship: is a

Suppose method: two classes A and B, A is B is a B or a A, i.e., the presence of inheritance.

Example: apple and fruit, cats and animals, dogs and cats [XG1]

2.1.3 Inheritance Variable Access features:

Accessing a process variable in a subclass

Subclasses local area to find

Subclass members to find the range

Parent class members find range

If you are not on the error (without regard to his father's father [XG2] ...)

2.1.4 Super

Super similar keyword usage and this keyword

2.1.5 constructor inherited access features

The default constructor with no arguments have access to the parent class

2.1.6 Access member methods inherited characteristics

Access by a child class object Method

①子类成员范围找

② parent class members find range

③ If you are not on the error (without regard to his father's father ...)

2.2 The method of rewriting and overwriting method wherein Note

2.2.1 method overrides

Overview: subclass method declaration and the emergence of the parent class exactly the same.

Application: When required function subclass the parent class, subclass and main body have their own unique content, you can override the parent class method, so that followed the functionality of the parent class, but also defines the type of unique content.

@Override

Annotations can help rewrite the method of checking the correctness of the method statement

2.2.2 Notes

Private methods can not be overridden (subclass the parent class private members are not inherited)

Subclass method can not be less access to (public> Default> Private)

Notes 2.3 java inherited, package, import

2.3.1
in java inherited Notes

Only supports single inheritance, do not support multiple inheritance

Java class supports multiple inheritance

2.3.2
package

Modifiers

Package: in fact, folders,

Role: manage your classes

Definition Format:

Format: package package name; (coated with multi-stage; separately)

Example: package com.itheima; (Notepad)

2.3.3
import

Package guide: using different packet classes, the full path of the package used to write, to write too much trouble, in order to simplify the operation with a package, java package provides a guide function.

Format: import package name;

Examples: import cn.com.itcast.Teacher

2.4 permission modifier (private, protected, public), the state of the modifier (final, static)

2.4.1 Permissions modifier (private, protected, public)

2.4.2 state modifier (final, static)

final: members can be modified methods, member variables, classes

final modified features:

Modification method: the ultimate method show that this method can not be overridden

Modifying variables: indicates that the variable is constant and can not be assigned again

Modified class: class indicates that the class is final and can not be inherited

final modification of local variables:

Basic type: modified data value change can not occur

Reference types: modified address value changes can not occur, the address where the content can be changed

static: members can be modified methods, member variables

static modification features:

 被类的所有对象共享(这也是我们判断是否使用静态关键字的条件)

 可以通过类名调用,也可以通过对象名调用,推荐使用类名调用

static access features; static member methods can only access static member methods.

Object-oriented - Polymorphism

More than 3.1 state

3.1.1 Polymorphism

The same object, manifested at different times different tenses

Example: Cat

It can be said is a cat a cat: Cat cat = new Cat ();

It can also be said that the cat is an animal: Animal animal = new Cat ();

Cat show up at different times in different forms, this is polymorphism.

Polymorphic premise and reflect

Inheritance / implementation relationship

There are ways to rewrite

There are references to parent child class object

3.1.2 polymorphic members access features:

Member variable: Compile look left, look left execution

Member method: Compile look left, look to the right of execution

Access is not the same reason: the members have to rewrite the method, but no member variables

3.1.3 Benefits and drawbacks

Benefits: improve program scalability

 具体体现:定义方法的时候,使用父类型作为参数,将来在使用的时候,使用具体的子类型参与操作。

Drawbacks: You can not use function-specific subclasses

3.1.4 Transition

① upward transition

  从子到父

  父类引用指向子类对象

② downcast

   从父到子

   父类引用转为子类对象

3.1.5 abstract class

Java, a method is no method body should be defined as abstract methods, whereas if there is an abstract method, the class must be defined as an abstract class.

Abstract class features:

Abstract classes and abstract methods must be modified to use the abstract keyword

public abstract class类名{}

public abstract void eat();

Abstract classes are not necessarily abstract methods, a method of abstract class is an abstract class must

An abstract class can not be instantiated

How abstract class to instantiate it? Referring polymorphic manner, by subclass object instantiated abstract class called polymorphism

Subclass of abstract class

Either override all abstract methods abstract class

Or is an abstract class

Features abstract class members

Member variables (variables can also be a constant)

Construction method

The method has a structure, but can not be instantiated

So, what is the role of the constructor is it? Subclasses for initializing the access to the parent class data

Member method

Can abstract methods: defining a subclass must complete certain actions

There may also be a non-abstract methods: improve code reusability

3.2 Interface

3.2.1 interface and its features, features members

Interface Overview:

Interface is a kind of common standards, they meet the standards, you can use

Java interface in more reflected in the behavior of the abstract

Features Interface

Interface with the keyword interface modification

public interface 接口名{}

Represented by class implements the interface implements

public class类名implements接口名{}

Interfaces can not be instantiated

Interface how to instantiate it? Referring polymorphic manner, by implementing the class of the object instance, the interface is called polymorphism.

Polymorphic forms: polymorphic concrete class, an abstract class of multi-state, multi-state interfaces

Polymorphic premise: inheritance relationship or implement; the method of rewriting; the parent (Class / Interface) reference point (promoter / implement) class object

Interface implementation class

All abstract interface methods either rewrite

Or is an abstract class

Members of the interface features

Member variables

Only constant

The default modifier: public static final

Construction method

Interface no constructor, because the interface is mainly to conduct the abstract, there is no specific

If a class has no parent, the default inherited from class Object

Member method

Only abstract methods

The default modifier: public abstract

3.2.2 The relationship between classes and interfaces

The relationship between class and class

Inheritance, only single inheritance, single inheritance may multilayer

Relationship classes and interfaces

Relationship to achieve, can be a single implementation can also be implemented multiple, multiple interfaces may also be implemented at the same time a class inheriting

Interface and its relationship

继承关系,可单继承,也可多继承

The difference between abstract classes and interfaces 3.2.3

An abstract class is an abstraction of things, and the interface is an abstract behavior

3.2.4 class name as parameter and return value

Parameter is the name of the class method, in fact, need is the object of the class

The return value of the method is the class name, in fact, the return of objects of the class

3.2.5 Abstract class name as a parameter and return value

Shape parameter method is an abstract class name actually needed is an object of the subclass of abstract class

The return value of the method is an abstract class name, in fact, returns the class object is a child character abstract class

3.2.6 Interface name as parameter and return value

The method parameter is the name of the interface, in fact, it needed to achieve the object of the class interface

The return value is the interface method name, in fact, is to achieve the return of the interface class object

3.3 Internal class

3.3.1 Internal class

Internal class: defines a class in a class

Internal class definition format

format

{public class name of the class

   修饰符class类名{

}

}

example:

{public class name of the class

  public

class Inner{

}

}

Features like internal access

You can directly access the internal class members outside of class, including private

To access the external members of the class within the class, you must create an object

3.3.2 members of the inner class

Just a note, the members of the class to create objects using internal format

Format: external name class class internal
object name = internal external object class object.

3.3.3 partial inner class

Local inner class is defined in the method in the class, so the outside world can not be used directly, we need to create objects in internal methods and use

Such members can directly access the external class, the method can access local variables

3.3.4 anonymous inner classes

Provided: the presence of a class or interface, where the class may be a particular class may be an abstract class

format:

new class or interface name () {

     重写方法;

}

example:

new Inter(){

   public void show(){

}

}

Nature: an inherited class or subclass that implements this interface anonymous object

[XG1] such a relationship should not use inheritance

[XG2] happen will complain

Guess you like

Origin blog.csdn.net/baidu_33094261/article/details/102760289