用的C#;
比如有个abstract class叫item,其他两个类classA和classB都继承item;
使用工厂模式创建一个对象,新建的这个对象是item类,我能通过obj.getType().Name方法判断这个类实际上是classA还是classB;
在我知道这个类实际上是classA还是classB之后,如果想使用classA或者classB他们自己的方法,我应该怎么办?
刚开始接触设计模式这些知识,还希望各位给一些建议!
Your Answer
1 answers
面向对象的特性是 抽象 封装 继承 多态
如果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(); }
Hot tools Tags
Hot Questions
Golang fatal error: concurrent map read and map write
2026-01-02 12:22:25
Difference between __getattr__ and __getattribute__
2026-01-02 12:01:09
How can I save the state of my program and then load it?
2026-01-02 11:43:04
Read a character from standard input in Go (without pressing Enter)
2026-01-02 11:22:13
Selecting data from two different servers in SQL Server
2026-01-02 11:01:47
Popular tool
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 phpstudy integrated installation environment runtime library
PHP programmer toolbox full version
Programmer Toolbox v1.0 PHP Integrated Environment
VC11 32-bit
VC11 32-bit phpstudy integrated installation environment runtime library
SublimeText3 Chinese version
Chinese version, very easy to use
Hot Topics
Douyin level price list 1-75
20416
7
20416
7
wifi shows no ip assigned
13574
4
13574
4
Hot Article
What is the 'basis' of a currency contract? What impact does it have on perpetual contract prices?
2025-12-26
By DDD
How to set up price alerts so you don't miss key entry points?
2026-01-01
By DDD
Tutorial on existence check of nested array values in PHP multidimensional array
2025-12-28
By DDD
How to draw dotted lines in PS How to draw various dotted lines in PS
2025-12-28
By DDD






