day17-面向对象作业

  1. 定义一个狗类和一个人类:

    狗拥有属性:姓名、性别和品种 拥有方法:叫唤

    人类拥有属性:姓名、年龄、狗 拥有方法:遛狗

    class Person:
        
        def took(self):
            print("遛狗")
    
        def __init__(self, name='小明', age=18, dog='毛毛'):
            self.name = name
            self.age = age
            self.dog = dog
    
    
    class Dog:
    
        def yell(self):
            print("叫唤")
    
        def __init__(self, name='毛毛', sex='公', variety='泰迪'):
            self.name = name
            self.sex = sex
            self.variety = variety
    
  2. 定义一个矩形类,拥有属性:长、宽 拥有方法:求周长、求面积

    class Rect:
        def __init__(self, length=1, width=1):
            self.length = length
            self.width = width
    
        def area(self):
            return self.length * self.width
    
        def peri(self):
            return 2 * (self.width * self.length)
    
    
    rect = Rect(4, 5)
    a1 = rect.area()
    p1 = rect.peri()
    print(a1, p1)
    
  3. 定义一个二维点类,拥有属性:x坐标、y坐标 拥有方法:求当前点到另外一个点的距离

    import math
    
    
    class Dimension:
        def __init__(self, x=1, y=1):
            self.x = x
            self.y = y
    
        def distance(self, x, y):
            square = (x - self.x) ** 2 + (y - self.y) ** 2
            return math.sqrt(square)
    
    
    A1 = Dimension(2, 4)
    d1 = A1.distance(-1, 9)
    print(d1)
    
  4. 定义一个圆类,拥有属性:半径、圆心 拥有方法:求圆的周长和面积、判断当前圆和另一个圆是否外切

    from math import *
    
    
    class Circle:
        def __init__(self, x=0, y=0, r=1):
            self.x = x
            self.y = y
            self.r = r
    
        def area(self):
            area = pi * self.r ** 2
            return area
    
        def peri(self):
            peri = 2 * pi * self.r
            return peri
    
        def tangent(self, x, y, r):
            square = (x - self.x) ** 2 + (y - self.y) ** 2
            center_distance = sqrt(square)
            if center_distance == r + self.r:
                return "两圆外切!"
            elif center_distance == abs(r - self.r):
                return "两圆内切!"
            elif center_distance < abs(r - self.r) or (x == self.x and y == self.y):
                return "两圆内含!"
            elif abs(r - self.r) < center_distance < r + self.r:
                return "两圆相交!"
            return "两圆不想交!"
    
    
    A1 = Circle(1, 1, 1)
    d1 = A1.tangent(5, 1, 4)
    print(d1)
    
  5. 定义一个线段类,拥有属性:起点和终点, 拥有方法:获取线段的长度

    class Lines:
        def __init__(self, x0=0, x1=1):
            self.x0 = x0
            self.x1 = x1
    
        def get_line(self):
            length = abs(self.x0 - self.x1)
            return length
    
    
    l1 = Lines()
    len1 = l1.get_line()
    print(len1)
    
  6. 写一个斗地主游戏类(根据生活经验添加类的内容)

    from random import choice, shuffle
    
    
    class Landlords:
        def __init__(self):
            self.player2 = []
            self.player1 = []
            self.player3 = []
            self.table = {}
            color = ['♥', '♠', '♣', '♦']
            nums = [str(x) for x in range(2, 11)] + ['J', 'Q', 'K', 'A']
            pokers = []
            for i in nums:
                for c in color:
                    pokers.append(c + i)
            pokers.extend(('JOKER', 'joker'))
            self.poker = pokers
    
        def shuffles(self):
            landlord = choice(self.poker)
            self.poker.remove(landlord)
            landlord = '地' + landlord
            self.poker.append(landlord)
    
            return shuffle(self.poker)
    
        def deals(self):
            d_poker = iter(self.poker)
            for _ in range(17):
                self.player1.append(next(d_poker))
                self.player2.append(next(d_poker))
                self.player3.append(next(d_poker))
            return self.player3, self.player2, self.player3
    
        def settle(self):
            self.table = {str(x): x for x in range(3, 11)}
            self.table.update({'J': 11, 'Q': 12, 'K': 13, 'A': 14, '2': 15, 'oker': 16, 'OKER': 17})
            self.player1.sort(key=lambda item: self.table[item[2:]] if item.startswith('地') else self.table[item[1:]],
                              reverse=True)
            self.player2.sort(key=lambda item: self.table[item[2:]] if item.startswith('地') else self.table[item[1:]],
                              reverse=True)
            self.player3.sort(key=lambda item: self.table[item[2:]] if item.startswith('地') else self.table[item[1:]],
                              reverse=True)
            return self.player1, self.player2, self.player3
    
    
    poker = Landlords()
    poker.shuffles()
    poker.deals()
    poker.settle()
    print(poker.player1)
    print(poker.player2)
    print(poker.player3)
    

你可能感兴趣的:(python,机器学习,开发语言)