定义一个测试类,在测试类中定义个长度不小于200的字符串,要求改字符串中包含数字、英文字母、其他字符。利用字符统计类创建一个统计类对象,分别调用“数字字符频次统计”“字母字符频次统计”“非数字、非字母等其他字符频次统计”方法,对定义的字符串进行统计,统计后,再分别调用“获取数字频次信息”“获取字母频次信息”“获取非数字、非字母等其他字符频次信息”方法,将统计信息打印。
时间: 2023-02-12 08:48:18 浏览: 52
定义测试类:
```python
class Test:
def __init__(self):
self.string = "这是一个长度不小于200的字符串,包含数字1,英文字母a,其他字符@。"
test = Test()
```
创建统计类对象并进行统计:
```python
class CharCount:
def __init__(self):
self.digit_count = 0
self.letter_count = 0
self.other_count = 0
def digit_count(self, string):
for char in string:
if char.isdigit():
self.digit_count += 1
def letter_count(self, string):
for char in string:
if char.isalpha():
self.letter_count += 1
def other_count(self, string):
for char in string:
if not char.isdigit() and not char.isalpha():
self.other_count += 1
def get_digit_count(self):
return self.digit_count
def get_letter_count(self):
return self.letter_count
def get_other_count(self):
return self.other_count
count = CharCount()
count.digit_count(test.string)
count.letter_count(test.string)
count.other_count(test.string)
```
打印统计信息:
```python
print("数字字符数量:", count.get_digit_count())
print("字母字符数量:", count.get_letter_count())
print("其他字符数量:", count.get_other_count())
```