上述代码中c1.display出现的内容为null或者0
时间: 2024-04-14 15:28:23 浏览: 24
感谢你提供的信息。根据你所说的情况,如果在执行 `c1.display()` 后显示的内容为 "null" 或者 "0",可能是因为在 `c1` 对象中没有正确保存 `s001` 的值或者 `display()` 方法没有正确实现。
请确保在创建 `c1` 对象时将 `s001` 的值正确保存到 `c1` 中。例如:
```python
class C1:
def __init__(self, value):
self.data = value
def display(self):
print(self.data)
s001 = "Hello, World!"
c1 = C1(s001)
c1.display()
```
这样,当调用 `c1.display()` 时,应该会正确地打印出 "Hello, World!"。
如果问题仍然存在,请提供更多的代码细节,以便我能够更好地理解问题并提供准确的解答。谢谢!
相关问题
请根据代码片段仿写实现div左下角拖拽移动用具体代码实现,import Vue from 'vue' Vue.directive('dialogZoomOut', { bind(el, binding, vnode, oldVnode) { let minWidth = 400;let minHeight = 300;let isFullScreen = false; let nowWidth = 0;let nowHight = 0;let nowMarginTop = 0;const dialogHeaderEl = el.querySelector('.el-dialog__header');const dragDom = el.querySelector('.el-dialog');dragDom.style.overflow = "auto";dialogHeaderEl.onselectstart = new Function("return false");dialogHeaderEl.style.cursor = 'move';const sty = dragDom.currentStyle || window.getComputedStyle(dragDom, null);let moveDown = (e) => {const disX = e.clientX - dialogHeaderEl.offsetLeft;const disY = e.clientY - dialogHeaderEl.offsetTop;let styL, styT;if (sty.left.includes('%')) {styL = +document.body.clientWidth * (+sty.left.replace(/%/g, '') / 100);styT = +document.body.clientHeight * (+sty.top.replace(/%/g, '') / 100);} else {styL = +sty.left.replace(/px/g, '');styT = +sty.top.replace(/px/g, '');};document.onmousemove = function (e) {const l = e.clientX - disX;const t = e.clientY - disY;dragDom.style.left = `${l + styL}px`;dragDom.style.top = `${t + styT}px`;};document.onmouseup = function (e) {document.onmousemove = null;document.onmouseup = null;};}dialogHeaderEl.onmousedown = moveDown;dialogHeaderEl.ondblclick = (e) => {if (isFullScreen == false) {nowHight = dragDom.clientHeight;nowWidth = dragDom.clientWidth;nowMarginTop = dragDom.style.marginTop;dragDom.style.left = 0;dragDom.style.top = 0;dragDom.style.height = "100VH";dragDom.style.width = "100VW";dragDom.style.marginTop = 0;isFullScreen = true;dialogHeaderEl.style.cursor = 'initial';dialogHeaderEl.onmousedown = null;} else {dragDom.style.height = "auto";dragDom.style.width = nowWidth + 'px';dragDom.style.marginTop = nowMarginTop;isFullScreen = false;dialogHeaderEl.style.cursor = 'move';dialogHeaderEl.onmousedown = moveDown;}}let resizeEl = document.createElement("div");dragDom.appendChild(resizeEl);resizeEl.style.cursor = 'se-resize';resizeEl.style.position = 'absolute';resizeEl.style.height = '10px';resizeEl.style.width = '10px';resizeEl.style.right = '0px';resizeEl.style.bottom = '0px';resizeEl.style.zIndex = '99';resizeEl.onmousedown = (e) => {let clientX = e.clientX;let disX = e.clientX - resizeEl.offsetLeft;let disY = e.clientY - resizeEl.offsetTop;document.onmousemove = function (e) {e.preventDefault(); let x = e.clientX - disX + (e.clientX - clientX);let y = e.clientY - disY;dragDom.style.width = x > minWidth ? `${x}px` : minWidth + 'px';dragDom.style.height = y > minHeight ? `${y}px` : minHeight + 'px';};document.onmouseup = function (e) {document.onmousemove = null;document.onmouseup = null;};}}})
以下是仿写的代码实现:
```html
<template>
<div class="dialog-wrapper" v-dialog-zoom-out>
<div class="el-dialog">
<div class="el-dialog__header">Dialog Title</div>
<div class="el-dialog__body">Dialog Content</div>
<div class="el-dialog__footer">
<button @click="$emit('close')">Close</button>
</div>
<div class="el-dialog__resize"></div>
</div>
</div>
</template>
<script>
export default {
name: "Dialog",
};
</script>
<style>
.dialog-wrapper {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.el-dialog {
position: absolute;
top: 0;
left: 0;
overflow: auto;
max-height: 80vh;
background-color: #fff;
border-radius: 4px;
box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
}
.el-dialog__header {
padding: 16px;
background-color: #f5f7fa;
border-top-left-radius: 4px;
border-top-right-radius: 4px;
cursor: move;
}
.el-dialog__body {
padding: 16px;
}
.el-dialog__footer {
display: flex;
justify-content: flex-end;
padding: 16px;
background-color: #f5f7fa;
border-bottom-left-radius: 4px;
border-bottom-right-radius: 4px;
}
.el-dialog__footer button {
padding: 8px 16px;
background-color: #fff;
border: 1px solid #c1c1c1;
border-radius: 4px;
cursor: pointer;
}
.el-dialog__resize {
position: absolute;
bottom: 0;
right: 0;
width: 10px;
height: 10px;
cursor: se-resize;
background-color: #c1c1c1;
}
</style>
```
```javascript
import Vue from "vue";
Vue.directive("dialogZoomOut", {
bind(el, binding, vnode, oldVnode) {
let minWidth = 400;
let minHeight = 300;
let isFullScreen = false;
let nowWidth = 0;
let nowHight = 0;
let nowMarginTop = 0;
const dialogHeaderEl = el.querySelector(".el-dialog__header");
const dragDom = el.querySelector(".el-dialog");
dragDom.style.overflow = "auto";
dialogHeaderEl.onselectstart = new Function("return false");
dialogHeaderEl.style.cursor = "move";
const sty = dragDom.currentStyle || window.getComputedStyle(dragDom, null);
let moveDown = (e) => {
const disX = e.clientX - dialogHeaderEl.offsetLeft;
const disY = e.clientY - dialogHeaderEl.offsetTop;
let styL, styT;
if (sty.left.includes("%")) {
styL = +document.body.clientWidth * (+sty.left.replace(/%/g, "") / 100);
styT = +document.body.clientHeight * (+sty.top.replace(/%/g, "") / 100);
} else {
styL = +sty.left.replace(/px/g, "");
styT = +sty.top.replace(/px/g, "");
}
document.onmousemove = function (e) {
const l = e.clientX - disX;
const t = e.clientY - disY;
dragDom.style.left = `${l + styL}px`;
dragDom.style.top = `${t + styT}px`;
};
document.onmouseup = function (e) {
document.onmousemove = null;
document.onmouseup = null;
};
};
dialogHeaderEl.onmousedown = moveDown;
dialogHeaderEl.ondblclick = (e) => {
if (isFullScreen == false) {
nowHight = dragDom.clientHeight;
nowWidth = dragDom.clientWidth;
nowMarginTop = dragDom.style.marginTop;
dragDom.style.left = 0;
dragDom.style.top = 0;
dragDom.style.height = "100VH";
dragDom.style.width = "100VW";
dragDom.style.marginTop = 0;
isFullScreen = true;
dialogHeaderEl.style.cursor = "initial";
dialogHeaderEl.onmousedown = null;
} else {
dragDom.style.height = "auto";
dragDom.style.width = nowWidth + "px";
dragDom.style.marginTop = nowMarginTop;
isFullScreen = false;
dialogHeaderEl.style.cursor = "move";
dialogHeaderEl.onmousedown = moveDown;
}
};
let resizeEl = document.createElement("div");
dragDom.appendChild(resizeEl);
resizeEl.style.cursor = "se-resize";
resizeEl.style.position = "absolute";
resizeEl.style.height = "10px";
resizeEl.style.width = "10px";
resizeEl.style.right = "0px";
resizeEl.style.bottom = "0px";
resizeEl.style.zIndex = "99";
resizeEl.onmousedown = (e) => {
let clientX = e.clientX;
let disX = e.clientX - resizeEl.offsetLeft;
let disY = e.clientY - resizeEl.offsetTop;
document.onmousemove = function (e) {
e.preventDefault();
let x = e.clientX - disX + (e.clientX - clientX);
let y = e.clientY - disY;
dragDom.style.width = x > minWidth ? `${x}px` : minWidth + "px";
dragDom.style.height = y > minHeight ? `${y}px` : minHeight + "px";
};
document.onmouseup = function (e) {
document.onmousemove = null;
document.onmouseup = null;
};
};
},
});
```
上述代码实现了一个类似于Element UI中的Dialog组件的拖拽和缩放功能。在使用时,只需要将 `v-dialog-zoom-out` 指令绑定到Dialog组件的容器元素上,即可实现拖拽和缩放功能。
目的:理解类的抽象与继承,体会方法重写和重写体现的多态性;综合运用面向对象的编程思想,对下面的问题进行分析,使用Java实现并进行测试。 内容:设计电话薄程序 (1)根据下面的描述设计类层次关系。 1)联系人信息有:姓名、电话、备注; 2)家人信息:还包括地址、固定电话; 3)朋友信息:还包括类别(同学、同事、其他朋友); 4)工作联系人信息:还包括公司、部门、职务。 (2)注意重写toString(),返回每类对象的全部信息 (3) 要求在父类中定义display()方法,此方法中将toString()结果输出值控台 (4)提供添加、删除、修改、查看指定成员、查看所有联系人信息等方法 (5)选做:图形用户界面、网络服务端 (6)使用Comparable或Comparator方法对联系人进行排序 能运行的java程序
以下是一个能够实现上述要求的Java程序,其中涉及到类的抽象与继承、方法重写和重载、多态性、排序等面向对象编程的概念:
```java
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;
abstract class Contact {
private String name;
private String phone;
private String remark;
public Contact(String name, String phone, String remark) {
this.name = name;
this.phone = phone;
this.remark = remark;
}
public abstract String getInfo();
public void display() {
System.out.println(getInfo());
}
public String toString() {
return getInfo();
}
public String getName() {
return name;
}
public String getPhone() {
return phone;
}
public String getRemark() {
return remark;
}
}
class FamilyContact extends Contact {
private String address;
private String homePhone;
public FamilyContact(String name, String phone, String remark, String address, String homePhone) {
super(name, phone, remark);
this.address = address;
this.homePhone = homePhone;
}
public String getInfo() {
return "家人 - " + getName() + " " + getPhone() + " " + getRemark() + " " + address + " " + homePhone;
}
}
class FriendContact extends Contact {
private String category;
public FriendContact(String name, String phone, String remark, String category) {
super(name, phone, remark);
this.category = category;
}
public String getInfo() {
return "朋友 - " + getName() + " " + getPhone() + " " + getRemark() + " " + category;
}
}
class WorkContact extends Contact {
private String company;
private String department;
private String position;
public WorkContact(String name, String phone, String remark, String company, String department, String position) {
super(name, phone, remark);
this.company = company;
this.department = department;
this.position = position;
}
public String getInfo() {
return "工作联系人 - " + getName() + " " + getPhone() + " " + getRemark() + " " + company + " " + department + " " + position;
}
}
class PhoneBook {
private ArrayList<Contact> contacts;
public PhoneBook() {
contacts = new ArrayList<Contact>();
}
public void addContact(Contact contact) {
contacts.add(contact);
}
public void removeContact(Contact contact) {
contacts.remove(contact);
}
public void modifyContact(Contact oldContact, Contact newContact) {
int index = contacts.indexOf(oldContact);
contacts.set(index, newContact);
}
public Contact searchContact(String name) {
for (Contact contact : contacts) {
if (contact.getName().equals(name)) {
return contact;
}
}
return null;
}
public void displayContacts() {
for (Contact contact : contacts) {
contact.display();
}
}
public void sortContacts() {
Collections.sort(contacts, new Comparator<Contact>() {
public int compare(Contact c1, Contact c2) {
return c1.getName().compareToIgnoreCase(c2.getName());
}
});
}
}
public class Main {
public static void main(String[] args) {
PhoneBook phoneBook = new PhoneBook();
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("请输入要进行的操作:1.添加联系人 2.删除联系人 3.修改联系人 4.查找联系人 5.显示所有联系人 6.排序联系人");
int option = scanner.nextInt();
switch (option) {
case 1:
System.out.println("请输入联系人类型:1.家人 2.朋友 3.工作联系人");
int type = scanner.nextInt();
System.out.println("请输入姓名:");
String name = scanner.next();
System.out.println("请输入电话:");
String phone = scanner.next();
System.out.println("请输入备注:");
String remark = scanner.next();
if (type == 1) {
System.out.println("请输入地址:");
String address = scanner.next();
System.out.println("请输入固定电话:");
String homePhone = scanner.next();
FamilyContact familyContact = new FamilyContact(name, phone, remark, address, homePhone);
phoneBook.addContact(familyContact);
} else if (type == 2) {
System.out.println("请输入类别:");
String category = scanner.next();
FriendContact friendContact = new FriendContact(name, phone, remark, category);
phoneBook.addContact(friendContact);
} else if (type == 3) {
System.out.println("请输入公司:");
String company = scanner.next();
System.out.println("请输入部门:");
String department = scanner.next();
System.out.println("请输入职务:");
String position = scanner.next();
WorkContact workContact = new WorkContact(name, phone, remark, company, department, position);
phoneBook.addContact(workContact);
}
break;
case 2:
System.out.println("请输入要删除的联系人姓名:");
String nameToRemove = scanner.next();
Contact contactToRemove = phoneBook.searchContact(nameToRemove);
if (contactToRemove != null) {
phoneBook.removeContact(contactToRemove);
System.out.println("删除成功!");
} else {
System.out.println("联系人不存在!");
}
break;
case 3:
System.out.println("请输入要修改的联系人姓名:");
String nameToModify = scanner.next();
Contact contactToModify = phoneBook.searchContact(nameToModify);
if (contactToModify != null) {
System.out.println("请输入新的联系人信息:");
int typeToModify = 0;
if (contactToModify instanceof FamilyContact) {
typeToModify = 1;
} else if (contactToModify instanceof FriendContact) {
typeToModify = 2;
} else if (contactToModify instanceof WorkContact) {
typeToModify = 3;
}
System.out.println("请输入姓名:");
String newName = scanner.next();
System.out.println("请输入电话:");
String newPhone = scanner.next();
System.out.println("请输入备注:");
String newRemark = scanner.next();
if (typeToModify == 1) {
System.out.println("请输入地址:");
String newAddress = scanner.next();
System.out.println("请输入固定电话:");
String newHomePhone = scanner.next();
FamilyContact newFamilyContact = new FamilyContact(newName, newPhone, newRemark, newAddress, newHomePhone);
phoneBook.modifyContact(contactToModify, newFamilyContact);
} else if (typeToModify == 2) {
System.out.println("请输入类别:");
String newCategory = scanner.next();
FriendContact newFriendContact = new FriendContact(newName, newPhone, newRemark, newCategory);
phoneBook.modifyContact(contactToModify, newFriendContact);
} else if (typeToModify == 3) {
System.out.println("请输入公司:");
String newCompany = scanner.next();
System.out.println("请输入部门:");
String newDepartment = scanner.next();
System.out.println("请输入职务:");
String newPosition = scanner.next();
WorkContact newWorkContact = new WorkContact(newName, newPhone, newRemark, newCompany, newDepartment, newPosition);
phoneBook.modifyContact(contactToModify, newWorkContact);
}
System.out.println("修改成功!");
} else {
System.out.println("联系人不存在!");
}
break;
case 4:
System.out.println("请输入要查找的联系人姓名:");
String nameToSearch = scanner.next();
Contact contactToSearch = phoneBook.searchContact(nameToSearch);
if (contactToSearch != null) {
contactToSearch.display();
} else {
System.out.println("联系人不存在!");
}
break;
case 5:
phoneBook.displayContacts();
break;
case 6:
phoneBook.sortContacts();
System.out.println("排序成功!");
break;
default:
System.out.println("输入错误,请重新输入!");
break;
}
}
}
}
```
这个程序实现了添加、删除、修改、查找和显示所有联系人信息的功能,并提供了一个排序联系人的选项。在程序运行时,用户可以根据提示输入相应的操作,然后程序会根据用户输入的内容执行相应的操作。
相关推荐
![cpp](https://img-home.csdnimg.cn/images/20210720083646.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)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)