面向对象程序设计对我来说还是很新的。我只是想学习一些有关类和方法的知识。我正在尝试编写一个名为``Object''的简单类,然后定义一些几何对象,例如圆,矩形等。然后,我想拥有诸如“区域”和“周长”之类的属性,在这些属性中,我想通过以下方式访问圆的区域:
class object:
import numpy as np
pi = np.pi
def circle(self):
def area(self, radius):
self.area=self.radius**2 * self.pi
s = object()
print(s.__dict__)
print(s.circle.area(5))
运行时,我得到:
{}
Traceback (most recent call last):
File "/Users/aya/Desktop/test.py", line 12, in <module>
print(s.circle.area(5))
AttributeError: 'function' object has no attribute 'area'
我如何使s.circle.area(5)
工作?
答案 0 :(得分:0)
1)名称object
已经作为基本的Python对象,您应该使用其他名称(Polygon
?)
2)我认为您的意思是让circle
是一个新类,而不是一个函数。您可以通过将一个类包含在类声明中来使其类似于或“继承”另一个类:
class Circle (Polygon): #NOT object, see number one
def __init__ (self, radius): #called to make a new Circle
self.radius = radius #give this circle its radius
def area (self):
# self.pi is already declared because Polygon states it, and Circles "inherit" that trait
return self.pi * (self.radius ** 2)
现在,要绕圈:
circle = Circle (5) # or Circle (radius = 5)
pi = circle.pi
# or Circle.pi, since it belongs to the whole class (upper-case) and not just the object (lower-case)
area = circle.area() #needs to be on a specific circle, because radii can vary
答案 1 :(得分:0)
下面是可以帮助您的示例
with
inter as
(select rn, amount,
isnull(lag(amount) over (order by rn), amount) l_amount
from test
),
inter2 as
(select rn, amount,
sum(case when amount <> l_amount then 1 else 0 end) over (order by rn) grp
from inter
)
select min(rn) rn, amount, count(*) cnt
from inter2
group by amount, grp
order by 1;
当我运行以上文件时。
class Dog:
# Class Attribute
species = 'mammal'
# Initializer / Instance Attributes
def __init__(self, name, age):
self.name = name
self.age = age
# Instantiate the Dog object
philo = Dog("Philo", 5)
mikey = Dog("Mikey", 6)
# Access the instance attributes
print("{} is {} and {} is {}.".format(
philo.name, philo.age, mikey.name, mikey.age))
# Is Philo a mammal?
if philo.species == "mammal":
print("{0} is a {1}!".format(philo.name, philo.species))
内部类示例
Philo is 5 and Mikey is 6.
Philo is a mammal!
答案 2 :(得分:0)
一切对我来说都不新鲜,但是您在这里s.circle.area(5)
试图访问Object的属性,该对象也应该是对象,因为您尝试调用此特定属性的方法。
明确地说是“。”。仅允许您访问属性或函数。例如
class Square:
def __init__(self, size):
self.size = size
def resize(self, new_size):
self.size = new_size
#Usage
>>> form = Square(5)
>>> print(form.size)
>>> 5 #output
>>> form.resize(120)
>>> print(form.size)
>>> 120 #output
在您的代码中,这确实很奇怪,没有任何意义,很多混乱。您正在函数圆内定义函数区域,函数圆尝试使用对象不存在的self.radius和现有属性pi创建区域属性。
想象一下类似的方法可能会起作用,您正在尝试通过调用circle方法(没有意义,很难解释)来访问该函数的功能。
您仍然可以在我们的广场上访问属性:
>>> form.size
现在您尝试从中调用函数:
>>> form.size.action()
在这种情况下,您使用的是整数方法(int也是包含方法的类)
通过调用对象方法:
>>> form.reshape(8) #changing the current size
>>> form.reshape(8).action()
以这种方式调用重整函数返回的方法。 似乎您对函数定义不是很自信(两个def关键字紧随其后,有可能,但不确定这是您的目标),导入我不确定是否真的选择了明智的方法(numpy将在以下位置导入)每个对象的创建,对您有用吗?)。小心基础,更多的基础知识将变得更强壮,更多的您将了解自己在做什么,而在编程中,您必须知道自己在做什么。
我如何使s.circle.area(5)工作?
至少可以做到
import numpy
class Circle:
def __init__(self, area = 0):
self.area = area
def area(self, new_area):
self.area = new_area
return self.area
class Object:
pi = numpy.pi
circle = Circle()
>>> obj = Object()
>>> obj.circle.area(5)
我终于可以执行此操作!许多事情不是很好的实践,或者如果没有正确使用,就不会有理想的行为,您必须了解正在发生的事情。我知道那是您正在学习的东西,它的确不错,但是请不要跑步(不要太快)而又不会走路。 (我可能会后悔一点,这是给你的,要小心)
P.S。 :我在这里谈论的一切都有大量的文档。在Python中,类定义确实是一种标准。面向对象的程序设计而不创建对象,这可能确实很矛盾。 这里是一些文档:https://docs.python.org/3.7/tutorial/classes.html#a-first-look-at-classes