Animal { public virtual void Eat() { Console.WriteLine("Animal eat"); } } public class Cat : Animal { public override void Eat() { Console.WriteLine("Cat eat"); } } public class Dog : Animal { public override void Eat()
person has different understandings and expressions, but I tend to describe this: different objects implemented by inheritance call the same method and show different behaviors, called polymorphism.Copy codeThe Code is as follows:Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/--> public class Animal{Public virtual void Eat (){Console. WriteLine ("Animal eat
Today, we will introduce how to generate an instance that "inherits" multiple objects.
For example, there is a constructor of an "animal" object,
Copy codeThe Code is as follows: function Animal (){
This. species = "animal ";
}
There is also a constructor for the "cat" object,Copy codeThe Code is as follows: function Cat (name, color ){This. name = name;This. col
JavaScript Object-oriented Programming (II): Inheritance of constructors
The first part of the series focuses on how to "encapsulate" data and methods, and how to generate instances from prototype objects.
Today'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 = "
The last article mainly introducedJavascriptHow to "encapsulate" data and methods to generate instances from prototype objects. Today we will introduce how to create multiple prototype objects"Inheritance".
For example, there is an "animal" object,
Function Animal ()
{
This. species = "animal ";
}
There is also a "cat" object,
functionCat(nam
JavaScript is an object-oriented language. In JavaScript there is a classic word, everything is object. Since it is object-oriented, there are three main characteristics of object-oriented: encapsulation, inheritance, polymorphism. This is about the inheritance of JavaScript, the other two can be said later.
JavaScript inheritance is not the same as C + + inheritance, and C + + inheritance is class based, and JavaScript inheritance is based on prototypes.
Now the question is coming.
What's th
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 must understand is to use the parent class pointer to call the subclass at runtime:For example, a function is as follows: VoidAnimal: fun1 (
I. Rewrite the method.
1. Rewriting can only appear in the inheritance relationship. When a class inherits its parent class method, it has the opportunity to override the method of the parent class. A special case is that the method of the parent class is identified as final. The primary advantage of rewriting is that it can define a behavior unique to a child type.Class animal {Public void eat (){System. Out. println ("
Objective-C learning 04-polymorphism, objective-c04 Polymorphism
Polymorphism
Concept of Polymorphism
Here is an example. in the morning, I spoke to my colleagues about thirst. result: My colleague A took my cup and gave me A cup of water. colleague B picked up a disposable paper cup on the water dispenser and gave me a cup of water. Colleague C gave me a bottle of drinks he just bought in the morning. Colleagues get the same request, but all three of them have their own solutions. This is polym
Basic Learning day08 --- polymorphism, simple factory, Object class equals, toString, day08 --- equalsI. Polymorphism
1.1. Concept of Polymorphism
Definition: multiple forms of existence of a certain type of things.
For example, cats and dogs in animals.
The cat object corresponds to the cat type: Cat x = new CAT ();
At the same time, a cat is also a type of animal. It can also be called an animal:
A fascinating feature of Java is its support for Runtime polymorphism (polymophism), which saves a lot of trouble with maintenance types. You can use a reference of a parent class to point to a subclass and then call the subclass method at runtime. In this way, no matter how many sub-classes you have extended in the future or the sub-classes of sub-classes, you can keep the program running without changing any code. JVM will take care of everything at runtime. To illustrate this, see the followi
, respectively, responsible for P1, P2 two responsibilities. But when the program has been written, it's too time-consuming. So, simply modifying the class T and using it to take charge of two responsibilities is a good choice, although this is contrary to the principle of single responsibility.For example, use a class to describe an animal breathing the scene:class animal{ Public void Breathe (String
responsibilities. But when the program has been written, it's too time-consuming. So, simply modifying the class T and using it to take charge of two responsibilities is a good choice, although this is contrary to the principle of single responsibility. (The risk is the uncertainty of the spread of responsibility, because we would not think of this duty p, which could spread to P1,P2,P3,P4 in the future ...) Pn. So remember to refactor the code immediately before it spreads to the point where w
need to break the class T also into two classes T1 and T2, respectively, responsible for P1, P2 two responsibilities. But when the program has been written, it's too time-consuming. So, simply modifying the class T and using it to take charge of two responsibilities is a good choice, although this is contrary to the principle of single responsibility. (The risk is the uncertainty of the spread of responsibility, because we would not think of this duty p, which could spread to P1,P2,P3,P4 in the
responsibilities. But when the program has been written, it's too time-consuming. So, simply modifying the class T and using it to take charge of two responsibilities is a good choice, although this is contrary to the principle of single responsibility. (The risk is the uncertainty of the spread of responsibility, because we would not think of this duty p, which could spread to P1,P2,P3,P4 in the future ...) Pn. So remember to refactor the code immediately before it spreads to the point where w
classes T1 and T2, respectively, responsible for P1, P2 two responsibilities. But when the program has been written, it's too time-consuming. So, simply modifying the class T and using it to take charge of two responsibilities is a good choice, although this is contrary to the principle of single responsibility. (The risk is the uncertainty of the spread of responsibility, because we would not think of this duty p, which could spread to P1,P2,P3,P4 in the future ...) Pn. so remember to refactor
the subset of items.
Many interfaces in the system. Collections namespace provide basic set functions:
Ienumerable can iterate items in the set.
Icollection (inherited from ienumerable) can obtain the number of items in the set and copy the items to a simple array type.
Ilist (inherited from icollection) provides a list of items in the collection and can access these items and other functions related to the item list.
Idictionary (inherited from icollection) is similar to ilist, but provid
JavaScript is an object-oriented language. In JavaScript there is a classic word that all things are objects. Since it is object-oriented, there are three main features of object-oriented: encapsulation, inheritance, polymorphism. This is about the inheritance of JavaScript, the other two can be said later.The inheritance of JavaScript is not the same as C + +, the inheritance of C + + is class-based, and JavaScript inheritance is based on prototype.Now that's the problem.What is a prototype? Pr
. Proceed ();}Console.WriteLine ("After Animalinterceptor.intercept");}}
The first type: Classproxy
Copy Code code as follows:
{
Console.WriteLine ("\n*************classproxy*************\n");
var generator = new Proxygenerator ();
var animal = generator. CreateclassproxyAnimal. Play ();
Console.WriteLine (Animal. GetType ());Console.WriteLine (An
T2, respectively, responsible for P1, P2 two responsibilities. But when the program has been written, it's too time-consuming. So, simply modifying the class T and using it to take charge of two responsibilities is a good choice, although this is contrary to the principle of single responsibility.
For example, use a class to describe an animal breathing the scene:
1. Class animal{
Public void Breathe (Str
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.