在Python开发过程中,你是否遇到过代码重复冗余、难以维护的问题?是否想过如何优雅地复用代码,让程序结构更加清晰?继承作为面向对象编程的三大特性之一,正是解决这些问题的利器。
本文将带你从零基础开始,深入掌握Python继承的方方面面。无论你是刚接触Python的新手,还是想深入理解继承机制的进阶开发者,这篇文章都将为你提供完整的学习路径。我们将通过实际的代码示例,让你彻底理解继承的本质,掌握在实际项目中的应用技巧。
在日常开发中,我们经常会遇到这样的场景:
Python# 没有使用继承的代码 - 存在大量重复
class Dog:
def __init__(self, name, age):
self.name = name
self.age = age
def eat(self):
print(f"{self.name}正在吃饭")
def sleep(self):
print(f"{self.name}正在睡觉")
def bark(self):
print(f"{self.name}正在汪汪叫")
class Cat:
def __init__(self, name, age):
self.name = name
self.age = age
def eat(self):
print(f"{self.name}正在吃饭")
def sleep(self):
print(f"{self.name}正在睡觉")
def meow(self):
print(f"{self.name}正在喵喵叫")
可以看到,Dog和Cat类中存在大量重复的代码,这违反了DRY原则(Don't Repeat Yourself)。
在实际的Python开发中,我们经常需要编写稳定易维护的类:打开文件、连接数据库、启动串口,与硬件交互的上位机开发尤甚。很多问题并不是出在业务逻辑,而是出在对象生命周期管理:何时初始化资源?何时释放?本文聚焦面向对象中的两个关键点——构造函数与析构函数。我们将通过问题分析、可落地的解决方案与可直接复制的代码示例,帮你搭建“创建即可用、销毁不泄露”的类设计,提升你的编程技巧与项目稳定性。
__init__(self, ...),在对象创建后被调用,用于初始化对象状态。注意它不是“真正分配内存”的地方(那是 __new__)。__del__(self),在对象被垃圾回收时“可能”被调用,不保证时机与顺序,尤其在解释器退出阶段。过度依赖会引发不可预期问题。__enter__/__exit__)和 contextlib 才是强烈建议的方式。connect()、open() 等方法,或使用上下文管理器保障释放。__enter__/__exit__,用 with 确保异常也会正确释放。contextlib.ExitStack 简化清理。__del____del__ 里引用全局模块或其他可能已被回收的对象。close()/dispose() 方法__exit__ 中复用该方法,形成单一释放通道。_closed 标记,确保重复释放不会出错(幂等)。延伸学习建议:
contextlib:建议阅读并实战 contextlib.contextmanager、ExitStack在做 Python开发 的上位机开发或工具类项目时,很多人一开始就被“对象、属性、方法”绕晕:属性到底放哪?方法该不该是静态的?为什么一个类写着写着就难以维护?本文聚焦“Python 面向对象—属性与方法”的核心实践,用通俗语言和可复制的代码示例,带你搭建既清晰又好扩展的类设计。你将学会:如何区分类属性/实例属性、实例方法/类方法/静态方法、何时使用属性描述符与@property,以及在 Windows 下做设备管理、配置管理等上位机开发的落地写法。
Pythonclass SerialDevice:
port = "COM1" # 错误:应为每个设备实例独有
baudrate = 115200 # 错误:不同设备可能不同
connected = False # 错误:状态不应共享
def __init__(self):
pass
def connect(self):
# 假装连接
self.connected = True
def read_value(self):
# 读取数据(伪代码)
return 42
问题:connected 是类属性,多个实例互相影响;port、baudrate 也不应共享。这样的“共享状态”在上位机开发中非常危险。
SkiaSharp 是一个强大的跨平台 2D 图形库,提供了灵活且高效的图像处理能力。本文将深入探讨 SkiaSharp 中图像裁剪的各种技术和方法。
C#SkiaSharp SkiaSharp.Views.WindowsForms
SkiaSharp 提供了强大的图像处理能力,其中图像旋转是最常用的操作之一。本文将深入探讨SkiaSharp中图像旋转的多种方法和技巧。
C#SkiaSharp SkiaSharp.Views.WindowsForms
C#using System.Windows.Forms;
using SkiaSharp;
namespace AppRotation
{
public partial class Form1 : Form
{
private SKBitmap originalBitmap;
private string currentImagePath;
public Form1()
{
InitializeComponent();
}
private void btnLoad_Click(object sender, EventArgs e)
{
using (OpenFileDialog openFileDialog = new OpenFileDialog())
{
openFileDialog.Filter = "Image Files|*.png;*.jpg;*.jpeg;*.bmp;*.gif";
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
currentImagePath = openFileDialog.FileName;
using (var fileStream = new FileStream(currentImagePath, FileMode.Open))
{
originalBitmap = SKBitmap.Decode(fileStream);
UpdateRotatedImage(pic, tbAngle.Value);
btnSave.Enabled = true;
}
}
}
}
private void tbAngle_Scroll(object sender, EventArgs e)
{
lblAngle.Text = $"Rotation Angle: {tbAngle.Value}°";
if (originalBitmap != null)
{
UpdateRotatedImage(pic, tbAngle.Value);
}
}
private void btnSave_Click(object sender, EventArgs e)
{
if (originalBitmap == null) return;
using (SaveFileDialog saveFileDialog = new SaveFileDialog())
{
saveFileDialog.Filter = "PNG Image|*.png|JPEG Image|*.jpg|BMP Image|*.bmp";
saveFileDialog.DefaultExt = "png";
saveFileDialog.AddExtension = true;
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
using (SKBitmap rotatedBitmap = RotateImage(originalBitmap, tbAngle.Value))
{
using (SKImage image = SKImage.FromBitmap(rotatedBitmap))
using (SKData data = image.Encode(SKEncodedImageFormat.Png, 100))
using (FileStream stream = new FileStream(saveFileDialog.FileName, FileMode.Create))
{
data.SaveTo(stream);
}
}
MessageBox.Show("Image saved successfully!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
private void UpdateRotatedImage(PictureBox pic, int angle)
{
if (originalBitmap == null) return;
using (SKBitmap rotatedBitmap = RotateImage(originalBitmap, angle))
{
using (SKImage image = SKImage.FromBitmap(rotatedBitmap))
using (SKData data = image.Encode())
using (MemoryStream memStream = new MemoryStream())
{
data.SaveTo(memStream);
memStream.Position = 0;
if (pic.Image != null)
{
pic.Image.Dispose();
}
pic.Image = new Bitmap(memStream);
}
}
}
public SKBitmap RotateImage(SKBitmap originalBitmap, float angle)
{
// 创建一个新的位图,大小适应旋转后的图像
SKBitmap rotatedBitmap = new SKBitmap(
(int)(Math.Abs(originalBitmap.Width * Math.Cos(angle * Math.PI / 180)) +
Math.Abs(originalBitmap.Height * Math.Sin(angle * Math.PI / 180))),
(int)(Math.Abs(originalBitmap.Width * Math.Sin(angle * Math.PI / 180)) +
Math.Abs(originalBitmap.Height * Math.Cos(angle * Math.PI / 180)))
);
using (SKCanvas canvas = new SKCanvas(rotatedBitmap))
{
// Clear canvas with transparent background
canvas.Clear(SKColors.Transparent);
// 将画布中心移动到图像中心
canvas.Translate(rotatedBitmap.Width / 2f, rotatedBitmap.Height / 2f);
// 旋转画布
canvas.RotateDegrees(angle);
// 绘制原始图像,使其居中
canvas.DrawBitmap(originalBitmap,
new SKPoint(-originalBitmap.Width / 2f, -originalBitmap.Height / 2f));
}
return rotatedBitmap;
}
}
}
