English 中文(简体)
2. 多种形态/图伊普种姓
原标题:Polymorphism/Type Casting Usage

我总是对多变主义感到不快,但我认为我不会最充分地理解这一概念。

我理解多变性是一种目标,可以采取多种形式。 因此,物体可以是另一个形式?

Mammal (base class)
cat (subclass of mammal) 
supercat (subclass of cat)
Cat newCat = new Cat();

现在,我想要把目录当作一个超级编目,这就是“吗?”

SuperCat supCat = (SuperCat)newCat;

难道这不像什么? 你们想要什么时候? 上述法典是否有效? 因此, C. 转换成“超级目录” • 增加记忆分配? 之后,这本书又写上了up子。

注: 多吗?

最佳回答

仅就你的说明发表评论。

两种多形态。

时间多变:

This is where your polymorphic behavior is defined prior to compilation. This happens with method overloading e.g.

public bool DoSomeThing(int myVal)
{
  // some code here
} 

public bool DoSomeThing(double myVal)
{
  // some code here
} 

移动式多变:

This is where the polymorphic behavior happens at runtime. An example of this would be a method that accepts a type that implements a specific interface (or common base class). This interface could be of a different type depending on the code that is executing.

e.g.

public interface IWeapon
{
   void Strike();
}

public class Knife : IWeapon
{
   public void Strike()
   {
     Console.WriteLine("Stab");
   }
}

public class Gun : IWeapon
{
   public void Strike()
   {
     Console.WriteLine("Shoot");
   }
}

public class Warrior
{
  public void Attack(IWeapon weapon)
  {
    weapon.Strike();
  }
}

如今,如果向Warrior提供枪枝,攻击的产物将是舒特,而Knife的产物是Stab。

我没有测试过这一法典,因此可能出现同学错误,但你应当能够了解这一想法。

问题回答

多种形态是你通过基类接口与亚类物体相互作用的。 例如:

List<Cat> cats = new List<Cat>();
cats.Add(new Cat());
cats.Add(new SuperCat());

foreach (Cat cat in cats) 
{
    cat.Meow();  // Works whether cat refers to a Cat or SuperCat.
                 // If SuperCat has its own Meow implementation, that is called.
}

多种形态允许一种类型表示某种合同,而对于其他类型,则以不同方式(无论是通过集体继承还是非)执行合同,每种方式都是根据其自己的目的。 适用该合同的法典不应(*) 只须注意涉及哪些执行,但合同必须服从。

从这个员额中提取。

多种形态 - 在两项判决中界定:

No this is not "Polymorphism" A SuperCat is a Cat, but not all Cats are SuperCat´s.

多种形态意味着,你的超级骑士团是一家超级骑士团,一个加塔,另一个是Mammal。 这可视为其中任何一种。

聚合->

道德-研究; 制图或类型





相关问题
Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

JSON with classes?

Is there a standardized way to store classes in JSON, and then converting them back into classes again from a string? For example, I might have an array of objects of type Questions. I d like to ...

Object-Oriented Perl constructor syntax and named parameters

I m a little confused about what is going on in Perl constructors. I found these two examples perldoc perlbot. package Foo; #In Perl, the constructor is just a subroutine called new. sub new { #I ...

Passing another class amongst instances

I was wondering what is the best practice re. passing (another class) amongst two instances of the same class (lets call this Primary ). So, essentially in the constructor for the first, i can ...

Where can I find object-oriented Perl tutorials? [closed]

A Google search yields a number of results - but which ones are the best? The Perl site appears to contain two - perlboot and perltoot. I m reading these now, but what else is out there? Note: I ve ...

热门标签