在 Python2.x 中,定义类时需要继承自 object,这是因为在 Python2.x 中,类可能会继承自经典类(classic class)或新式类(new-style class),而继承自 object 就是为了定义新式类。新式类在 Python2.x 中引入了许多新功能,比如属性访问控制、静态方法和类方法等 举例说明 加不加object的区别
时间: 2024-03-13 16:46:01 浏览: 77
python中定义类方法共3页.pdf.zip
下面举一个简单的例子来说明在 Python2.x 中,继承自 `object` 和不继承自 `object` 的区别:
```python
# 继承自 object 的新式类
class NewStyleClass(object):
def method(self, handles)
% hObject handle to b_edit (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function c_edit_Callback(hObject, eventdata, handles)
% hObject handle to c_edit (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of c_edit as text
% str2double(get(hObject,'String')) returns contents of c_edit as a double
% --- Executes during object creation, after setting all properties.
function c_edit_CreateFcn(hObject, eventdata, handles)
% hObject handle to c_edit (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function d_edit_Callback(hObject, eventdata, handles)
% hObject handle to d_edit (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of d_edit as text
% str2double(get(hObject,'String')) returns contents of d_edit as a double
% --- Executes during object creation, after setting all properties.
function d_edit_CreateFcn(hObject, eventdata, handles)
% hObject handle to d_edit (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on selection change in curve_type_popupmenu.
function curve_type_popupmenu_Callback(hObject, eventdata, handles)
% hObject handle to curve_type_popupmenu (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns curve_type_popupmenu contents as cell array
% contents{get(hObject,'Value')} returns selected item from curve_type_popupmenu
% --- Executes during object creation, after setting all properties.
function curve_type_popupmenu_CreateFcn(hObject, eventdata, handles)
% hObject handle to curve_type_popupmenu (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: popupmenu controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on selection change in surface_type_popupmenu.
function surface_type_popupmenu_Callback(hObject, eventdata, handles)
% hObject handle to surface_type_popupmenu (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns surface_type_popupmenu contents as cell array
% contents{get(hObject,'Value')} returns selected item from surface_type_popupmenu
% --- Executes during object creation, after setting all properties.
function surface_type_popupmenu_CreateFcn(hObject, eventdata, handles)
% hObject handle to surface_type_popupmenu (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: popupmenu controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
```
在这个示例代码中,我们使用了一个包含多个输入框和下拉框的GUI界面,并且添加了一个"Calculate"按钮来触发计算曲线积分和曲面积分的函数。你可以根据自己的需求来修改和添加GUI界面的
阅读全文