1

そこで、引数のないコンストラクターと、DataGridViewCell 型を期待する 1 つの引数を取るコンストラクターの両方を持つ DataGridViewColumn のサブクラスを作成しようとしています。これは私のクラスです:

class TableColumn(DataGridViewColumn):
    def __init__(self, string):
        super(TableColumn, self)
        self.Text = string
        self.CellTemplate = DataGridViewTextBoxCell()
        self.ReadOnly = True

次のような引数として文字列を渡そうとするたびに:

foo = TableColumn('Name')

それはいつも私にこれを与えます:

TypeError: expected DataGridViewCell, got str

そのため、スーパークラスの単一引数コンストラクターには常に「文字列」を渡しているようです。super(TableColumn, self) を super(TableColumn,self) に置き換えてみました。__init __() を使用して、引数なしのコンストラクターを呼び出したいことを明示的に確認しましたが、何も機能していないようです。

4

1 に答える 1

1

__init__.NETクラスから派生する場合、実際には実装する必要はありません。代わりに実装する必要があります__new__

class TableColumn(DataGridViewColumn):
    def __new__(cls, string):
        DataGridViewColumn.__new__(cls)
        self.Text = string
        self.CellTemplate = DataGridViewTextBoxCell()
        self.ReadOnly = True

基本的に、.NET基本クラスのコンストラクターはPythonサブクラスの前__init__(ただし後__new__)に呼び出す必要があります。そのため、間違ったDataGridViewColumnコンストラクターが呼び出されていました。

于 2012-06-15T15:25:43.800 に答える