search
  • Sign In
  • Sign Up
Password reset successful

Follow the proiects vou are interested in andi aet the latestnews about them taster

1 answers
Q&A 如何使用 利用工厂模式创建出来的对象中的方法

0

用的C#;

比如有个abstract class叫item,其他两个类classA和classB都继承item;

使用工厂模式创建一个对象,新建的这个对象是item类,我能通过obj.getType().Name方法判断这个类实际上是classA还是classB;

在我知道这个类实际上是classA还是classB之后,如果想使用classA或者classB他们自己的方法,我应该怎么办?

刚开始接触设计模式这些知识,还希望各位给一些建议!

Your Answer
submit

1 answers
0

面向对象的特性是 抽象 封装 继承 多态

如果A和B有完全不同且和item无关的方法 应该使用接口实现

abstract class item { } interface IclassA { void a(); } interface IclassB { void b();} class classA : item, IclassA { public void a() { throw new NotImplementedException(); } } class classB : item, IclassB { public void b() { throw new NotImplementedException(); } }
item item = new classA(); if (item is IclassA) { var itemA = item as IclassA; itemA.a(); }


2016-11-07 15:12:41

submit

Popular tool

vc9-vc14 (32+64 bit) runtime library collection (link below)

vc9-vc14 (32+64 bit) runtime library collection (link below)

Download the collection of runtime libraries required for phpStudy installation

VC9 32-bit

VC9 32-bit

VC9 32-bit phpstudy integrated installation environment runtime library

PHP programmer toolbox full version

PHP programmer toolbox full version

Programmer Toolbox v1.0 PHP Integrated Environment

VC11 32-bit

VC11 32-bit

VC11 32-bit phpstudy integrated installation environment runtime library

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use