从.Net类继承的OOP [英] OOP Inheriting from a .Net class

查看:97
本文介绍了从.Net类继承的OOP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好,太晚了,我在画空白,准备问一个愚蠢的问题.
OOP基本概念.

我正在尝试继承一个名为System.Printing.PrintSystemJobInfo的.Net类. (来自.Net参考程序集System.Printing.dll版本3.0.0.0)

这是我无法正常工作的代码:

O.k, it''s late, I''m drawing a blank and am about to ask a stupid question.
OOP Basic Concepts.

I''m trying to inherit a .Net class called System.Printing.PrintSystemJobInfo
(From the .Net reference assemblies System.Printing.dll version 3.0.0.0)

Here is my non functioning code:

namespace MyNameSpace
{
    class PrintSystemJobInfoCustom : System.Printing.PrintSystemJobInfo
    {
        protected PrintSystemJobInfoCustom(){}

        public DateTime TimeJobSubmittedCustom { get; set; }
    }
}



我在构造函数中遇到的错误是:



The error I''m getting with the constructor is :

The type ''System.Printing.PrintSystemJobInfo'' has no constructors defined



但是正如您所看到的,我确实创建了一个构造器,但是显然是不正确的.当我删除此构造器时,它会给我同样的错误,但是是在类级别.
我使构造函数受到保护,因为它在公开时给了我这个错误:



But as you can see I did create a contructor, but obviously not correctly. When I remove this contructor, it gives me the same error, but at a class level.
I made the constructor protected because it gave me this error when it was public:

''System.Printing.PrintSystemJobInfo.PrintSystemJobInfo(System.Printing.PrintQueue)'' is inaccessible due to its protection level





Is something in that class preventing inheritance, or am I overlooking something simple?

推荐答案

您不能从没有公共或受保护的构造函数的类继承.
您会遇到第一个错误,因为编译器找不到PrintSystemJobInfo的公共默认构造函数. 而且会出现第二个错误,因为当您未定义构造函数时,将使用基的默认构造函数,在这种情况下,该构造函数是私有的.
我想问一下为什么要从PrintSystemJobInfo中招募吗?
You cannot inherit from a class with no public or protected constructors.
You get the first error because the compiler can''t find a public default constructor of PrintSystemJobInfo.
And you get the second error because when you define no constructor the default constructor of the base is used which in this case is private.

May I ask why you want to inhirit from PrintSystemJobInfo?


从MSDN中,我们看到System.Printing.PrintSystemJobInfo没有公共构造函数(很可能是私有或内部构造函数) ),因此您无法继承该类.您可以使用静态Get()方法获取该类的实例.

但是,如果必须创建实例,则可以尝试使用 Decorator [
From MSDN, we see that the System.Printing.PrintSystemJobInfo does not have a public constructor (most probably it''s private or internal), so you cannot inherit the class. You can get an instance of the class using the static Get() method.

However, if you must create an instance, you can try using the Decorator[^] pattern.


扩展方法如何?
How about an extension method?
public DateTime GetTimeJobSubmittedCustom(this PrintSystemJobInfo target){
 return target.TimeJobSubmitted + get the time zone offset from settings;
}



您可以从某处的语言环境设置中获取当前时区.



You can get the current time zone from the locale settings somewhere.


这篇关于从.Net类继承的OOP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆