Sometimes the parent class object needs to be pointed to the subclass object, and sometimes the parent class object needs to be converted to the subclass object. For example, we can say that a dog is an animal and a dog, but the premise is that we must know that a dog must be an animal, the animal must be a dog. The following describes how to direct the parent cl
, if you write a tree traversal algorithm, you can use it to process any directed acyclic graph.
Future Tense considerations increase the reusability, maintainability, and robustness of your code, and are easy to modify when the environment changes. It must be used with the constraint condition for temporal. Too many programmers only focus on their current needs, but they sacrifice the long-term viability of software.Be a different person, be a defectTo develop programs in the future tense.
It
Question settingsThe farmer wants to ship cats, dogs, and fish on the left bank to the right bank by boat. During the transportation process, only one animal can be shipped at a time, and the farmer can cross the river by air. When a person is not on the bank, the dog will bite the cat and the cat will eat fish. When a person is on this bank, there will be no conflict. Use an object-oriented design to solve such problems.
Analysis: According to the Qu
) {
THIS.name = name;
This.color = color;
This.type = "Cat animal";
This.eat = function () {alert ("Eat Mouse");
}
The same approach is used to generate an instance:
var cat1 = new Cat ("Da Mao", "Yellow");
var cat2 = new Cat ("Er Mao", "Black");
alert (Cat1.type); Cat Animals
Cat1.eat (); Eat mice
There seems to be no problem on the surface, but there is a big drawback in actually doing so. That is,
program follow the principle of a single responsibility, you need to decompose class C into two classes C1 and C2, respectively, responsible for R1, R2 two responsibilities. But when the program has been written, it's too time-consuming. So it is a good choice to simply modify Class C and use it to take charge of two duties, although it is contrary to the principle of single responsibility.To illustrate:Class Animal{public void Breathe (String
", "Yellow");
var cat2 = new Cat ("Er Mao", "Black");
alert (cat1.name); Da Mao
alert (Cat1.color); Yellow
At this point, CAT1 and Cat2 automatically contain a constructor attribute that points to their constructors.
Alert (Cat1.constructor = = Cat); True
Alert (Cat2.constructor = = Cat); True
JavaScript also provides a instanceof operator that validates the relationship between a prototype object and an instance object.
Alert (cat1 instanceof Cat); Tr
the relationship between a prototype object and an instance object.
Alert (cat1 instanceof Cat); True
Alert (cat2 instanceof Cat); True
Four, the problem of the structure function patternThe constructor method works well, but there is a problem of wasting memory.See, let's add a constant Cat property type (kind) to the object, and add a method eat (eat). Then the prototype object Cat becomes the following:
function Cat (name,color) {
THIS.name = name;
This.col
Like other high-level languages, JavaScript also has the new operator, and we know that the new operator is used to instantiate a class, allocating an instance object in memory. But in JavaScript, all things are objects, why do they have to be created by new? This article will bring you together to explore the mysteries of new in JavaScript ...
First, recognize the new operator
function Animal (name) {
this.name = name;
}
Animal.color
virtual void fun ();
virtual void fun1 ();
};
Implementation of CA,CB Class
...
void Main ()
{
CA A; Not allowed because there are pure virtual functions in the class CA
CB b; OK, because there is no pure virtual function in class CB
...
}
---------------------------------------------------------------
The use of virtual functions in the middle of polymorphism:
Polymorphism is generally done by pointing to a pointer to a base class.
Dog Mydogwangwang;
Mydogwangw
the leaf node is connected in an orderly manner through a two-way linked list. At the same time, the data table is different from the index leaf node, the data in the table is unordered. They are not all stored in the same table block and there is no connection between the blocks.
In general, the index stores the physical address values of specific data.
Index type
We know that there are two types of indexes: clustered index and non-clustered index.
Clustered index: physical storage is sor
contains four fields: Animal,name,sex and age, and uses animal as the indexed column, with the following SQL code:
Copy Code code as follows:
-----------------------------------------------------------
----Create t_pet table in tempdb.
-----------------------------------------------------------
Use tempdb
CREATE TABLE T_pet
(
Animal V
virtual functionsCb B; // yes, because CB class does not have pure virtual functions...}
---------------------------------------------------------------Use of virtual functions in the middle of polymorphism:Polymorphism is generally implemented by pointing to the base class pointer.
Dog mydogwangwang;Mydogwangwang. Born ();It must be "dog"
SoHorse myhorsepipi;Myhorsepipi. Born ();It must be "horse"
Is it also polymorphism?//////////////////////////////////////// /////////One thing you mu
. They are not all stored in the same table block and there is no connection between the blocks.
In general, the index stores the physical address values of specific data.
Index type
We know that there are two types of indexes: clustered index and non-clustered index.Clustered index: physical storage is sorted by index.Non-clustered index: physical storage is not sorted by index.
Clustered Index
Data Pages of clustered indexes are stored physically and orderly, and data pages are leaf nodes of
JAVA 15th (polymorphism and basic application)Polymorphism:The third feature of object-oriented definition: a function with the same name has multiple forms. For example, a function with a polymorphism has different parameter lists, and its existence is different, there is also a function that is placed in the parent class and in the subclass, and its existence is also different.The object also has polymorphism.Example: animals include pigs, cats, and dogs.The cat object corresponds to the cat c
Questions: Now there is a constructor for an "animal" object. function Animal () { this" animal "; There is also a constructor for the "Cat" object. function Cat (name,color) { this. Name = name; this. color = color; } how to make "cat" Inherit "animal"? First, constructor bindingTake
Java override (Override) vs. overload (overload)
Override (Override)Overrides are subclasses that rewrite the implementation of the method that allows access to the parent class, and the return value and formal parameters cannot be changed. that is, the shell does not change, the core rewrite! The benefit of overriding is that subclasses can define their own behavior as needed. That is, subclasses can implement methods of the parent class as needed.The overriding method cannot throw a new
In most programming languages, there are classes and objects, and a class can inherit other classes.In JavaScript, inheritance is prototype based (prototype-based), which means that there is no class in JavaScript and that an object inherits another object instead. :)
1. Inheritance, the Protoin JavaScript, when an object rabbit inherits another object animal, this means that there will be a special attribute in the Rabbit object: rabbit.__proto__ =
differs from the index leaf node, the data in the table is stored out of order, they are not all stored in the same block, and there is no connection between the blocks.In general, the index holds the physical address value of the specific data.type of indexWe know that there are two types of indexes: Clustered indexes and nonclustered indexes.Clustered index: Physical storage is sorted by index.Nonclustered indexes: Physical storage is not sorted by index.Clustered IndexThe data pages of a clu
Original address: http://www.ruanyifeng.com/blog/2010/05/object-oriented_javascript_inheritance.htmlToday's introduction is about five ways to "inherit" between objects.For example, there is now a constructor for an "animal" object.
function Animal () {
This.species = "Animal";
}
There is also a constructor for the "Cat" object.
fun
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.