Template...... Basic Knowledge Notes

Source: Internet
Author: User

function template technology defines parameterized non-member functions, at which time the program can invoke the same code with different parameter types

Class templates: Managing common data types for other data types


Containers: Examples of container difference usage

Generic Programming: Code is written independently of any particular type of implementation. Requires that the user provide the type or value that the specific program instance operates on.


    • What is the difference between inheritance and polymorphism?

1 . What are the characteristics of inheritance and inheritance?

Subclasses inherit the characteristics and behavior of the parent class, making the subclasses have various properties and methods of the parent class. Or subclass inherits a method from the parent class so that the child class has the same behavior as the parent class.

Features: In an inheritance relationship, the parent class is more generic and the subclass more specific. The parent class has more general characteristics and behaviors, and subclasses have some special characteristics and behaviors in addition to the characteristics and behaviors of the parent class.

In an inheritance relationship. The parent class and subclass need to satisfy the is-a relationship. The child class is the parent class.

Represents the terms of the parent and child classes: Parent and subclass, superclass and subclass, base class, and derived class, they represent the same meaning.

2 . Why do I need to inherit? When should I inherit?

Using inheritance can effectively implement code reuse and avoid duplicate code occurrences.

When two classes have the same characteristics (attributes) and behaviors (methods), the same parts can be extracted and placed into a class as the parent class, and the other two classes inherit the parent class.

Inheritance implements an object-oriented principle: Writeonce,only once(written once, and written once

3, How to achieve inheritance?

In the Java language, the extends(extended) keyword is used to represent a class that inherits another class.

Only some common properties and methods are defined in the parent class.

Subclasses automatically inherit the properties and methods of the parent class, and the subclasses can define specific properties and methods. or subclasses to redefine the properties of the parent class, overriding the parent class's methods can get different functionality from the parent class.

4 . What is method rewriting?

If a method is defined in a subclass whose name, return type, and argument list match exactly the name, return type, and argument list of a method in the parent class, the subclass's method overrides the parent class's method.

Methods override in different classes and are necessary for implementing polymorphism.

5. The use and location of the Super keyword, thesuper keyword calls the constructor of the parent class, and theSuper keyword calls the method of the parent class?

In the constructor method of the subclass, the constructor method of the parent class is called with the Super keyword.

If a method of the parent class is overridden in a subclass, the parent class's method can be called through the Super keyword.

Parent class:

private String name;

Private String sex;

Public xinxin1 (String name,string Sex)

{

This.name=name;

This.sex=sex;

}

public void Hello () {

System.out.println (" Hi! I am " +name+" I Am " +sex+ " Child ");

}

Sub-class:

Public xinxin2 (String name,string Sex)

{

// Call the construction method of the parent class

Super (Name,sex);

}

public void Hello () {

System.out.println (" I'm new here!") ");

// method of calling parent class

Super.hello ();

}

Location NOTE: The statement that calls the constructor method of the parent class (Super statement) must be the first statement in the constructor method.

Because when creating an object, you need to create the parent class object before creating the subclass object.

Note: When you create an object, the parent class object is created before the child class object is created. If the constructor method that calls the parent class is not displayed, the non-parametric construction method of the parent class is automatically called.

6, all kinds of Boss (ancestor)Object.

All classes inherit the Java.lang.Object class directly or indirectly, and theobject class defines the same behavior that all Java objects have, and is the ancestor of all classes.

If a class does not use the extends keyword, the class inherits directly from the Object class.

7, what is polymorphic?

Polymorphism is characterized by a variety of forms, with a variety of implementation methods. Or polymorphism is a feature that has the ability to express multiple forms. Or the same implementation interface, different instances are used to perform different operations.

8, Why need to use polymorphic? What are the benefits of polymorphism?

You can enhance the scalability and maintainability of your program to make your code more concise.

It can not only reduce the workload of coding, but also greatly improve the maintainability and extensibility of the program.

9, How to achieve polymorphism?

The general practice is to write a method that only receives the parent class as an argument, and the code written only deals with the parent class. When this method is called, a different subclass object (new object) is instantiated.

To say more specifically:

(1), subclasses override methods of the parent class. Enables subclasses to have different method implementations.

(2), the parent class type as the parameter type, the parent class and its child class object as parameters.

(3), runtime, according to the actual type of object created dynamically decide to use that method.

At run time, theJava Virtual opportunity determines which method to use based on the type of object that is actually created. This is generally referred to as dynamic binding.    

Ten , polymorphic Summary: polymorphism is closely related to inheritance and method rewriting, and we receive the parent class type as a parameter in the method, invoking the various methods of the parent class type in the method implementation. When the handle class is passed as a parameter to this method, theJava Virtual opportunity invokes the corresponding method in the subclass (when there is a method override) based on the object type actually created.










This article from "Jiang Technology" blog, declined reprint!

Template...... Basic Knowledge Notes

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.