写一个父函数和子函数的demo
时间: 2024-02-09 18:08:37 浏览: 29
好的,以下是一个使用Python语言编写的父函数和子函数的示例:
```python
# 父函数
def calculate_total_price(price, quantity):
tax = 0.1 # 税率为10%
subtotal = price * quantity
total_price = add_sales_tax(subtotal, tax)
return total_price
# 子函数
def add_sales_tax(subtotal, tax_rate):
sales_tax = subtotal * tax_rate
total_price = subtotal + sales_tax
return total_price
# 调用父函数
price = 10
quantity = 2
total_price = calculate_total_price(price, quantity)
print("总价为:", total_price)
```
在上面的代码中,父函数`calculate_total_price()`计算商品总价,其中调用了子函数`add_sales_tax()`来计算销售税和总价。在调用父函数时需要传入参数`price`和`quantity`,而子函数需要传入参数`subtotal`和`tax_rate`。最终输出商品的总价。
通过使用父函数和子函数,代码更加清晰简洁,易于维护和扩展。
相关问题
用C语言写一个父函数和子函数的demo
好的,以下是一个使用C语言编写的父函数和子函数的示例:
```c
#include <stdio.h>
// 子函数
double add(double a, double b) {
return a + b;
}
// 父函数
double calculate_total_price(double price, int quantity) {
double tax = 0.1; // 税率为10%
double subtotal = price * quantity;
double sales_tax = add(subtotal, tax);
double total_price = add(subtotal, sales_tax);
return total_price;
}
int main() {
double price = 10;
int quantity = 2;
double total_price = calculate_total_price(price, quantity);
printf("总价为:%f", total_price);
return 0;
}
```
在上面的代码中,子函数`add()`用于计算两个数的和,父函数`calculate_total_price()`计算商品的总价,其中使用了子函数`add()`来计算销售税和总价。在调用父函数时需要传入参数`price`和`quantity`,而子函数需要传入参数`a`和`b`。最终输出商品的总价。
通过使用父函数和子函数,代码更加清晰简洁,易于维护和扩展。
帮我写一个QT子窗口修改父窗口控件属性demo
以下是一个简单的QT子窗口修改父窗口控件属性的demo,其中包含一个按钮和一个文本框,点击按钮后会改变父窗口的标题:
```
#include <QtWidgets>
class ChildWindow : public QWidget
{
Q_OBJECT
public:
ChildWindow(QWidget *parent = nullptr) : QWidget(parent)
{
QVBoxLayout *layout = new QVBoxLayout(this);
QPushButton *button = new QPushButton("Click me", this);
QLineEdit *lineEdit = new QLineEdit(this);
layout->addWidget(button);
layout->addWidget(lineEdit);
connect(button, &QPushButton::clicked, this, [=]()
{
parentWidget()->setWindowTitle(lineEdit->text());
});
}
};
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr) : QMainWindow(parent)
{
QWidget *centralWidget = new QWidget(this);
setCentralWidget(centralWidget);
QVBoxLayout *layout = new QVBoxLayout(centralWidget);
QPushButton *button = new QPushButton("Open child window", centralWidget);
layout->addWidget(button);
connect(button, &QPushButton::clicked, this, [=]()
{
ChildWindow *childWindow = new ChildWindow(this);
childWindow->show();
});
}
};
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MainWindow mainWindow;
mainWindow.show();
return app.exec();
}
#include "main.moc"
```
在主窗口中,我们创建了一个按钮,点击该按钮会打开一个子窗口。子窗口中包含一个文本框和一个按钮,点击该按钮会修改主窗口的标题。在子窗口中,我们通过调用`parentWidget()`函数获取到父窗口,然后调用`setWindowTitle()`函数修改父窗口的标题。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)