opp--object-oriented knowledge points

Source: Internet
Author: User
Tags modifiers

First day

1. What is a class? What is an object?

1) The real world is made up of a lot of objects.

Object-based extraction of classes

2) object: a single individual that really exists

Class: Represents a class of individuals (types, categories)

3) The class can contain:

3.1) Properties shared by all objects (static)----variables

3.2)----Method of behavior (motion) common to all objects

4) A class can create multiple objects

Multiple objects created by the same class with the same structure and different data

5) A class is a template for an object that is a concrete instance of a class

2. How do I create a class? How do I create an object?

3. Draw an equal sign to the reference type:

1) point to the same object

2) modifications to one of the references will affect the other

Eg: Door key

Basic type equals:

1) Assign Value

2) Modification of one of the values does not affect the other

Eg: copy of ID card

4.null: empty, i.e. not pointing to any object

If the reference value is NULL, the reference can no longer be manipulated.

Null pointer exception if operation (NullPointerException)

Add

1.1) Find the object: a bunch of small squares

2) Suction Type: Lattice class cell

3) member variables and methods in the design class:

4) Create the object:

2. Process-oriented is a process-centric programming idea, that is, to analyze the steps required to solve the problem, and then use the method to step-by-step implementation of these steps, when used, in the main method of a call in turn can be.

3. Describe the relationship between classes and objects

Reference answer

object is an objective entity, and it is the starting point and foundation of analyzing and solving problems in object-oriented programming process. The essence of an object is a piece of data storage area in memory, whose data structure is determined by the class that defines it.

A class is a template that is used to build objects, which are generated through the instantiation of a class, where a class can create multiple objects, each with its own data and behavior.

3 Please describe the difference between the reference type and the base type

Reference answer

In addition to the 8 basic types, variables declared with the class name (interface, array) are referred to as reference type variables, referred to as references. The function of the reference is to access the object.

The underlying type variable itself contains its instance data, and the reference type variable stores the address information of an object in memory. When a reference-type variable points to an object of that class, the object can be accessed through this variable.

Next day

1. Signature of Method: Method name + parameter list

2. Overloading of methods:

1) The same class, the method name is the same, the parameter list is different

2) The compiler automatically calls different methods at compile time based on the signature bindings of the method

3. Construction method: constructor, constructor, builder

1) Assigning initial values to member variables

2) with the same name as the class, no return value type

3) is automatically called when creating the (new) object

4) If you do not write the structure, the system provides a non-parametric construction method by default

If you write a construct yourself, it is no longer provided by default

5) Construction method can be overloaded

4.this: Refers to the current object, which object refers to which object

method, there is a default before accessing the member variable.

This usage:

1) this. Member Variable name----Access member variable

2) this. Method name ()------Call method

3) This ()-------------call construction method

5. Array of reference types

1)

1     New Cell[4]; 2     New Cell (2,5); 3     New Cell (3,6); 4     New Cell (4,7); 5     New Cell (5,8);

2)

1      New cell[]{2       New Cell (2,5),3       new cell (3,6),4       new cell (4,7 ),5       new Cell (5,8)6      }

3)

1     int New int [3] []; 2     New int [2]; 3     New int [3]; 4     New int [2]; 5     // the 1th element of the 2nd element in Arr is assigned a value of

4)

1 int New int // 3 Rows 4 columns 2      for (int i=0;i<arr.length;i++) {3        for (int j=0;j<arr[i].length;j++) {4         arr[i][j] = +; 5       }6     }

1) Find object: T object, j,o,i,s,z,l

2) Pumping class: T class, J class, L class, O,s,z,i class

3) member variables and methods in the design class:

4) Create an Object test:

Knowledge Point Supplement:

1. A file can contain multiple classes

Public-decorated classes can have only one

The public-decorated class must have the same name as the file name

Third Day

1. Memory Management: Understanding

1) Heap:

1.1) Store all new objects

1.2) The life cycle of the member variable:

The object is created in the heap, and the object disappears when it is recycled

1.3) No reference to the object is garbage,

Garbage collector (GC) is not scheduled to check, garbage is automatically recycled

1.4) Memory leaks: Useless objects are not recycled in a timely manner

Recommendation: If the object is no longer in use, set the reference to NULL in a timely manner

2) Stack:

2.1) store all local variables in the method being called

2.2) When invoking a method, allocate a corresponding stack frame to the method in the stack,

The local variables in the stored method in the stack frame,

Stack frames are eliminated at the end of method execution and local variables disappear

2.3) The life cycle of local variables:

method is invoked when the stack frame is removed when the method ends

3) Method Area:

3.1) store. Class bytecode files and all methods

3.2) Only one copy of the method, through this to distinguish the specific reference

2. Inheritance:

1) facilitates code reuse (avoids code duplication)

2) implement inheritance through extends

3) Parent class/base class: Characteristics and behaviors common to all subclasses

Subclass/Derived class/: characteristics and behavior specific to subclasses

4) After inheriting the parent class, the subclass has:

Parent class Common + subclass Special

5) A parent class can have more than one child class,

A subclass can have only one parent class---single inheritance

6) Inheritance has transitivity

7) Java stipulation: The parent class must be constructed before the subclass is constructed

If the parent class construct is not called in the subclass construct, a super () is added by default to invoke the parameterless construct of the parent class.

If the parent class construct is tuned in the subclass construct, it is no longer provided by default

Super () must be in the first sentence of the subclass construct

3.super: Refers to the parent class object of the current object

Usage:

1) Super. Member Variable name---Access member variables of the parent class

2) Super. Method Name ()-----method of calling the parent class

3) Super ()------------Call the construction method of the parent class

4. Upward styling

1) The reference of the parent type to the object of the child class

2) What you can point out, see the type of reference

Knowledge Point Supplement:

1. Member Variables:

1) in class, outside the method

2) When new is present in the heap, the object is erased when it is recycled

3) with default values

Local variables:

1) in the method

2) When the method is called to exist in the stack, the call end stack frame is cleared and disappears

3) No default value

Member variables: There are a few objects there are several (heap)

Member method: Only one copy (method area)

2. In Java, there are three levels of Java programs, virtual machines, and operating systems in which Java programs interact with virtual machines, while virtual machines interact with the operating system. The compiled Java bytecode file runs in the JVM.

Regardless of whether the code or data in the program needs to be stored in memory, and the Java program requires memory to be managed and allocated by the JVM, developers need only care about how the JVM manages memory, without having to focus on how the operating system manages the memory, which guarantees the platform independence of the Java program.

The JVM divides the requested memory logically into three regions: heap, stack, method area. These three regions are used to store different data.

3. GC threads start tracking from reference variables in the stack to determine which memory is in use, and if the GC cannot track down a block of memory, then the GC will assume that the memory is no longer in use, which is recyclable. However, Java programmers do not have to worry about memory management because the garbage collector is automatically managed.

4. Constructs the class T and J classes of the parent class, Tetromino the public (T class and J class public) information in the parent class, the T class and the J class inherit the Tetromino parent class. At this point, the child class can share the data of the parent class. This process is the generalization process.

5. Briefly describe the JVM garbage collection mechanism

The garbage collection mechanism is the mechanism provided by Java to automatically free up memory space.

The garbage collector (garbage COLLECTION,GC) is a thread that comes with the JVM to reclaim objects that are not referenced.

6 Is there a memory leak in the Java program?

There is a memory leak. There are generally two kinds of memory leaks.

The first is the memory allocated in the heap, and when it is not released, all the ways to access the memory are deleted, and the other is the memory and the way it is accessed (referenced) when the memory object is clearly not needed. The first case, in Java has been due to the introduction of garbage collection mechanism, has been well resolved. So, the memory leak in Java, mainly refers to the second case.

7.Foo foo = new Foo ();

FOO.F ();

The memory implementation principle of the above code is:

The 1.Foo class is first loaded into the JVM's method area, which includes information about the class, including methods and constructs, and so on.

2. Allocate the reference variable foo in the stack memory.

3. Allocate the instance variable memory space in heap memory according to the Foo type information, and then point the reference Foo in the stack to the first address of the Foo object heap memory.

4. Invoke the method using the reference Foo and invoke the F method based on the type Foo referenced by foo.

8. When a subclass constructor is called to initialize a subclass object, the parent class constructor is always executed before the subclass constructor. The subclass constructor calls the parent class constructor in the following cases.

A, the first row of the subclass constructor executor uses super to explicitly call the parent class constructor, and the corresponding constructor of the parent class is called according to the list of arguments passed in the super call.

B, the first line of code executed by the subclass constructor uses this to explicitly invoke the overloaded constructor in this class, and the system invokes another constructor in this class based on the list of arguments passed in this call. The parent class constructor is called when you execute another constructor in this class.

C, there is neither a super call nor this call in the subclass constructor execution body, the system will implicitly call the parent class constructor without arguments before executing the subclass constructor.

Fourth day

1. Override of the method (override):

1) occurs in a parent-child class with the same method name, with the same argument list and different method body

2) When the override method is called, look at the object

2. Overrides and overloads differ:----Common face questions

1) Rewrite:

1.1) Parent-child class, method names are the same, parameter lists are the same

1.2) Follow the "Run time" binding to invoke the method based on the type of the object

2) Overload (overload):

2.1) The same class, the method name is the same, the parameter list is different

2.2) Follow the "compile period" binding, depending on the type of reference binding method

3.package:

1) Avoid naming conflicts for classes

2) The fully qualified name of the class: package name. class Name

3) package name recommended all letters lowercase

Import

1) classes in the same package can be accessed directly,

There are two ways of accessing classes in different packages:

1.1) declaring classes via import/Introducing classes

1.2) Fully qualified name----cumbersome

2) Syntax: Import package name. class name;

4. Access Control Modifiers:

1) Public: Open, any class

2) Private: proprietary, this class

3) Protected: Protected, this class, sub-class, same package class

4) Default: Do not write anything, this class, the same package class

Description

1) class access control can only be public and default

2) Members of the class, such as 4 of the above can be

5.static: Static

1) Static variables:

1.1) Modified by static

1.2) belongs to the class, there is a method area, only one copy

1.3) often accessed through the class name.

1.4) When to use: all objects have the same data when used

2) static method:

2.1) Modified by static

2.2) belongs to the class, there is a method area, only one copy

2.3) often accessed through the class name.

2.4) The static method does not have an implicit this pass,

Therefore, you cannot access instance members directly in a static method

2.5) When to use: the operation of the method is only relevant to the parameter and is not related to the object

3) static BLOCK:

3.1) Modified by static

3.2) The class is automatically executed when loaded, and the class is loaded only once,

Static blocks are executed only once

3.3) When to use: often used to load static resources (Pictures, audio, video, etc.)

6.final: The final

1) modifier variable: variable cannot be changed

2) Modification Method: Method cannot be overridden

3) Modifier class: class cannot be inherited

7.static final-----------Self-study

Complementary points of knowledge:

1. Is there more instance variables or static variables?

----------instance variable multiple

----------static methods are not much

2. The static method does not have an implicit this pass,

No this means there are no objects,

Instance variables belong to objects and must be accessed through object points

Therefore, you cannot access instance members directly in a static method

3. Member variables:

1) instance variable: belongs to the object, exists in the heap,

There are several objects that are accessed through object points

2) Static variable: belongs to the class, exists in the method area,

Only one copy is accessed through the class name.

4. Rewrite adherence to the "Two and two small one big" principle:----is equal to the absolute right

* 1) Two:

* 1.1) Method name is the same

* 1.2) Same parameter list

* 2) Two small:

* 2.1) The return value type of the subclass is less than or equal to the parent class's

* 2.1.1) void and basic type must be the same

* 2.1.2) When a reference type is less than or equal to the parent class

* 2.2) when the subclass throws an exception that is less than or equal to the-----exception of the parent class

* 3) a large:

* 3.1) The subclass has access rights greater than or equal to the-----modifier of the parent class

5. The requirement for method overloading is the principle of "two different",

* 1) Two:

* 1.1) The method name is the same,

The same class means that two methods can be declared in the same class or inherited, or one is declared, and the other is inherited.

* 1.2) Same class

* 2) A different: parameter list is different

6. For class adornments, you can use public and default methods. Where public-decorated classes can be used by any one class, the default access-controlled classes can only be used by classes in the same package.

The protected and private access modifiers are not decorated with classes, but they can be decorated with internal classes (later in this course).

7. With regard to the package and import statements, the following statement is wrong:

A. Package provides a naming mechanism for managing class name spaces

B. When defining a class, you must specify a package name in addition to the name of the class.

C. Import statement for importing the required classes

D. Using classes with the same class name in different packages at the same time, the package name cannot be omitted

8. The final keyword modifies the member variable, meaning that it is immutable after initialization (once the object is created and immutable), the member variable can be assigned an initial value at the time of declaration, or it can be initialized in a construction method or in a static code block.

Fifth day

1.static Final constants:

1) constants must be declared and initialized at the same time

2) can not be changed, through the class name. To access

3) constant name suggests all uppercase letters, multiple words separated by _

4) is directly replaced with a specific value at compile time---high efficiency

2. Abstract Method:

1) modified by the abstract

2) Only the definition of the method, no concrete implementation of the method (not even {})

3. Abstract class:

1) modified by the abstract

2) classes that contain abstract methods must be abstract classes

If there is no abstract method in a class, you can declare it as an abstract class---I am willing to

3) Abstract classes cannot be instantiated (new)

4) An abstract class is a subclass that needs to be inherited:

4.1) overriding all the abstract methods of the parent class-common

4.2) also declared as abstract class----infrequently used

5) Meaning of abstract class:

5.1) Encapsulating the data and behavior common to subclasses-subclass reuse

5.2) provides a common type for all subclasses--upward styling

5.3) contains an abstract method that provides a unified portal for all subclasses

4. Interface:

1) is a standard, normative

As long as you meet this standard, you can do something---API

2) defined by interface

3) can only contain constants and abstract methods

4) interface cannot be instantiated

5) interface is required to be implemented/inherited, implementing class/Subclass:

All abstract methods in the interface must be overridden

6) A class can implement multiple interfaces,

If you inherit and implement it, you must first inherit it and then implement it.

7) interface can inherit interface

Complementary knowledge points

1. Abstract classes can contain abstract and non-abstract methods, and all methods in an interface are abstract.

2. The interface contains no constructors, and abstract classes can contain constructors, and constructors in abstract classes are not used to create objects, but instead have their subclasses call these constructors to complete initialization operations that belong to abstract classes

3.//Demo Interface basic syntax

1 Interfaceinter1{2      Public Static Final intNUM = 250;3      Public Abstract voidShow ();4     DoublePI = 3.14159;//default public static final5     voidSay ();//Default public abstract6}

Sixth day

Schoolboys

1. Polymorphism: Multiple forms

1) The meaning of polymorphism:

1.1) different implementations of the same type of reference when pointing to different objects

Behavior polymorphism: Cut (), run ()

1.2) different functions when the same object is styled as a different type

Polymorphism of objects: me, water

2) Upward Styling:

2.1) The reference of the parent type to the object of the child class

2.2) types that can be shaped: the parent class, the implemented interface

2.3) What you can point out, see the type of reference

3) Forced type conversion, only two successful conditions:

3.1) The object to which the reference is directed is the type

3.2) referencing the object to which it is directed, implements the interface

4) As long as the two conditions are not met,

The ClassCastException type conversion exception occurs

It is generally judged by instanceof before conversion:

Whether the object pointed to by the reference is of that type

2. member Inner class: The odds of being applied alone are not too great

1) class in a set of classes, inside called inner class inner, outside called outside class outer

2) Internal classes typically serve only external classes and do not have visibility outside of them

3) objects in an inner class are typically created in an external class

4) Members of the external class (including private) can be accessed directly from the inner class

An implicit reference in the inner class points to the outer class object that created it

External class name. this.

3. Anonymous inner class:

1) You want to create an object of a class (subclass), and the object is created only once,

At this point the class does not have to be named, called Anonymous inner class

2) The Inner class (member, anonymous) has a separate. class

4. Object-oriented three major features:

1) Package:

1.1) Class: Encapsulates the characteristics and behavior of an object

1.2) Method: Encapsulation of the specific business logic/implementation

1.3) Access control modifier: Encapsulated is access permission

2) Inheritance:

2.1) Easy to reuse code

2.2) extends, father: a common child: special

3) Polymorphism:

3.1) Meaning:

3.1.1) When a reference of the same type points to a different object, there are different implementations

Polymorphism of behavior

3.1.2) The same objects are shaped into different types, with different functions

Polymorphism of objects

3.2) upward styling, forced conversion, instanceof

3.3) Two forms of expression:

3.3.1) rewrite

3.3.2) Reload

Knowledge Point Supplement:

1. The parent class is large, the sub-class is small

Type conversions:

1) automatic type conversion: small to large

2) Forced type conversions: from big to small

2. Design principles:

1) Pumping common features and behaviors into the parent class

2) If all sub-classes behave the same, they are designed as normal methods

If all subclasses behave differently, they are designed as abstract methods

3) The interface is used when it conforms to a relationship that is both

3. Inheritance to conform to is a relationship

4.1) created a subclass of Inter2 with no Name

2) An object was created for the subclass named O3

3). The class body of the subclass in curly braces

Inter2 O3 = new Inter2 () {};

Knowledge Point Consolidation:

11. Public classAnswer {2   Public Static voidMain (string[] args) {3    intScore = 20;4Answer ans=NewAnswer ();5 Ans.add (score);6System.out.println ("Main:score =" +score);7  }8  voidAddintscore) {9System.out.println ("add:score=" + score++);Ten  } One}

Parsing: Output add:score=20

Main:score =20

The reason is that score is defined only in the main function, the scope is also the main function, when the program calls the Ans.add function, the function is passed to the parameter score=20, but this function does not return a value. The output or the variable score=20 in the main function

opp--object-oriented knowledge points

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.