下面程序中Base是抽象类。请在下面程序的横线处填上适当内容,以使程序完整并使程序的输出为: Der1 called! Der2 called!
时间: 2023-12-03 16:46:56 浏览: 61
```python
from abc import ABC, abstractmethod
class Base(ABC):
@abstractmethod
def fun(self):
pass
class Der1(Base):
def fun(self):
print("Der1 called!")
class Der2(Base):
def fun(self):
print("Der2 called!")
# --------------------------------------
obj1 = Der1()
obj2 = Der2()
obj1.fun()
obj2.fun()
# --------------------------------------
```
输出:
```
Der1 called!
Der2 called!
```
相关问题
下面程序中Base是抽象类。请在下面程序的横线处填上适当内容,以使程序完整,并使程序 的输出为: #include <iostream> class Base {public: _______________; }; class Der1:public Base {public: void display(){cout<<"Der1 called!"<<endl;} }; class Der2:public Base {public: void display(){cout<<"Der2 called!"<<endl;} }; void fun(______________) { p->display();} void main() { Der1 b1; Der2 b2; Base * p=&b1; fun(p); p=&b2; fun(p); }
下面程序中Base是抽象类。请在下面程序的横线处填上适当内容,以使程序完整,并使程序 的输出为:
```
#include <iostream>
class Base {
public:
virtual void display() = 0;
};
class Der1:public Base {
public:
void display(){std::cout<<"Der1 called!"<<std::endl;}
};
class Der2:public Base {
public:
void display(){std::cout<<"Der2 called!"<<std::endl;}
};
void fun(Base *p) {
p->display();
}
void main() {
Der1 b1;
Der2 b2;
Base * p=&b1;
fun(p);
p=&b2;
fun(p);
}
```
程序输出:
```
Der1 called!
Der2 called!
```
在 `Base` 类中,我们需要定义一个纯虚函数 `display()`,使得 `Base` 成为一个抽象类。在 `fun()` 函数中,需要使用 `Base` 类型的指针作为参数,这样才能保证在调用 `p->display()` 时,能够调用到派生类中实现的 `display()` 函数。因此,在 `fun()` 函数的参数列表中,我们需要使用 `Base *` 类型的指针。
在 `main()` 函数中,我们定义了两个派生类对象 `b1` 和 `b2`,并分别将它们的地址赋值给 `Base *` 类型的指针 `p`。然后分别调用了 `fun(p)` 函数,程序输出了 `Der1 called!` 和 `Der2 called!`。
微信小程序中base64转换成图片并获取图片路径并显示在image标签
好的,我可以帮你解答这个问题。首先,你需要使用小程序内置的wx.getImageInfo接口获取图片信息,并且将base64编码转换成临时文件路径。具体的代码实现如下:
```javascript
// 将base64编码转换成临时文件路径
function base64ToPath(base64) {
const base64Img = base64.replace(/[\r\n]/g, '')
const buffer = wx.base64ToArrayBuffer(base64Img)
const filePath = `${wx.env.USER_DATA_PATH}/${Date.now()}.png`
wx.getFileSystemManager().writeFileSync(filePath, buffer, 'binary')
return filePath
}
// 获取图片信息并显示在image标签
wx.getImageInfo({
src: base64ToPath(base64Data),
success: function (res) {
console.log(res.width)
console.log(res.height)
console.log(res.path)
that.setData({
imagePath: res.path
})
}
})
```
上述代码中,我们定义了一个base64ToPath函数,将base64编码转换成临时文件路径,并且使用wx.getImageInfo获取图片信息,最后将图片路径设置到image标签中即可。注意,由于小程序的安全限制,我们只能将图片保存到小程序的临时文件夹中,不能直接将图片保存到相册中。