python中的类和方法。面向对象技术简介

python中的类和方法。面向对象技术简介,python,class,oop,object,methods,Python,Class,Oop,Object,Methods,它应该产生 “嗨,我叫马克。”或者“嗨,我叫史蒂夫。” 但相反,它产生了 “您好,我的名字未定义。”您需要将introduction()->introduction(self)声明为实例方法(通过传入self)才能访问实例变量self.myname class Person: def __init__(self, name): """Make a new person with the given name.""" self.myname = name

它应该产生 “嗨,我叫马克。”或者“嗨,我叫史蒂夫。”

但相反,它产生了
“您好,我的名字未定义。”

您需要将
introduction()
->
introduction(self)
声明为实例方法(通过传入
self
)才能访问实例变量
self.myname

class Person:
    def __init__(self, name):
        """Make a new person with the given name."""
        self.myname = name

    def introduction(myname):
        """Returns an introduction for this person."""
        return "Hi, my name is {}.".format(myname)

# Use the class to introduce Mark and Steve
mark = Person("Mark")
steve = Person("Steve")

print(mark.introduction())
print(steve.introduction())
样本输出:

class Person:

    def __init__(self, name):
        """Make a new person with the given name."""
        self.myname = name

    def introduction(self):
        """Returns an introduction for this person."""
        return "Hi, my name is {}.".format(self.myname)
但是,请注意,类中的函数中的第一个参数是为类或对象保留的,以便将其自身传递到该类或对象(除非将标记应用于该方法,否则不会传递第一个隐式参数;其本质上表现为模块方法)

还要记住,
self
不是保留字,所以你可以给它起任何名字(即使
self
是PEP惯例)。下面的示例执行与上面示例相同的输出,并且语义相同

# Use the class to introduce Mark and Steve
mark = Person("Mark")
steve = Person("Steve")

print(mark.introduction())
print(steve.introduction())
>>> Hi, my name is Mark.
>>> Hi, my name 

您需要声明
introduction()
->
introduction(self)
作为实例方法(通过传入
self
)才能访问实例变量
self.myname

class Person:
    def __init__(self, name):
        """Make a new person with the given name."""
        self.myname = name

    def introduction(myname):
        """Returns an introduction for this person."""
        return "Hi, my name is {}.".format(myname)

# Use the class to introduce Mark and Steve
mark = Person("Mark")
steve = Person("Steve")

print(mark.introduction())
print(steve.introduction())
样本输出:

class Person:

    def __init__(self, name):
        """Make a new person with the given name."""
        self.myname = name

    def introduction(self):
        """Returns an introduction for this person."""
        return "Hi, my name is {}.".format(self.myname)
但是,请注意,类中的函数中的第一个参数是为类或对象保留的,以便将其自身传递到该类或对象(除非将标记应用于该方法,否则不会传递第一个隐式参数;其本质上表现为模块方法)

还要记住,
self
不是保留字,所以你可以给它起任何名字(即使
self
是PEP惯例)。下面的示例执行与上面示例相同的输出,并且语义相同

# Use the class to introduce Mark and Steve
mark = Person("Mark")
steve = Person("Steve")

print(mark.introduction())
print(steve.introduction())
>>> Hi, my name is Mark.
>>> Hi, my name 

它应该在内存中打印对象的表示(类似于
的内容嗨,我的名字是

原因是方法的第一个参数应该是调用该方法的对象

就像你有
def\uu init\uu(self,name):
你应该有
def简介(self,myname):

然后您将遇到另一个问题,因为
introduction
现在需要一个参数
myname
,而您没有提供它。您实际上并不需要它,因为您现在可以访问
self.myname

def introduction(myname):
    """Returns an introduction for this person."""
    return "Hi, my name is {}.".format(myname.myname)
将输出

class Person:
    def __init__(self, name):
        """Make a new person with the given name."""
        self.myname = name

    def introduction(self):
        """Returns an introduction for this person."""
        return "Hi, my name is {}.".format(self.myname)


# Use the class to introduce Mark and Steve
mark = Person("Mark")
steve = Person("Steve")

print(mark.introduction())
print(steve.introduction())

它应该在内存中打印对象的表示(类似于
Hi,我的名字是

原因是方法的第一个参数应该是调用该方法的对象

就像你有
def\uu init\uu(self,name):
你应该有
def简介(self,myname):

然后您将遇到另一个问题,因为
introduction
现在需要一个参数
myname
,而您没有提供它。您实际上并不需要它,因为您现在可以访问
self.myname

def introduction(myname):
    """Returns an introduction for this person."""
    return "Hi, my name is {}.".format(myname.myname)
将输出

class Person:
    def __init__(self, name):
        """Make a new person with the given name."""
        self.myname = name

    def introduction(self):
        """Returns an introduction for this person."""
        return "Hi, my name is {}.".format(self.myname)


# Use the class to introduce Mark and Steve
mark = Person("Mark")
steve = Person("Steve")

print(mark.introduction())
print(steve.introduction())

您的问题是,您为您的介绍方法提供了参数
myname
,但从未为其提供有效的参数。您可以简单地执行以下操作:

Hi, my name is Mark.
Hi, my name is Steve.
你给出了介绍方法,变量来自你的类
myname

def introduction(myname):
    """Returns an introduction for this person."""
    return "Hi, my name is {}.".format(myname.myname)
但上述情况甚至没有必要。由于您在类的
\uuuu init\uuuu
方法中初始化了name变量,因此它就像一个全局变量。所以你可以简单地说:

mark = Person(" Mark")
steve = Person(" Steve")

print(mark.introduction(mark.myname))
print(steve.introduction(steve.myname))

您的问题是,您为您的介绍方法提供了参数
myname
,但从未为其提供有效的参数。您可以简单地执行以下操作:

Hi, my name is Mark.
Hi, my name is Steve.
你给出了介绍方法,变量来自你的类
myname

def introduction(myname):
    """Returns an introduction for this person."""
    return "Hi, my name is {}.".format(myname.myname)
但上述情况甚至没有必要。由于您在类的
\uuuu init\uuuu
方法中初始化了name变量,因此它就像一个全局变量。所以你可以简单地说:

mark = Person(" Mark")
steve = Person(" Steve")

print(mark.introduction(mark.myname))
print(steve.introduction(steve.myname))

你为什么讨厌缩进?@Domoboto先生,那真的不需要,伙计。请那家伙把代码缩进。对不起。我不明白。缩进哪一个?@Mr.Python当你用一种依赖空白的语言问别人问题时,这是一种基本的礼貌。我完全同意。但没必要这么说为什么你一定讨厌缩进?@Domoboto先生,那真的不需要,伙计。请那家伙把代码缩进。对不起。我不明白。缩进哪一个?@Mr.Python当你用一种依赖空白的语言问别人问题时,这是一种基本的礼貌。我完全同意。但是没有必要这样说