Python - 带参数的方法

import math

class Point:
    def move(self, x, y):
        self.x = x
        self.y = y
    def reset(self):
        self.move(0, 0)
    def calculate_distance(self, other_point):
        return math.sqrt(
                (self.x - other_point.x)**2 +
                (self.y - other_point.y)**2)

# how to use it:
point1 = Point()
point2 = Point()

point1.reset()
point2.move(5,0)
print(point2.calculate_distance(point1))
assert (point2.calculate_distance(point1) == point1.calculate_distance(point2))
point1.move(3,4)
print(point1.calculate_distance(point2))
print(point1.calculate_distance(point1))

 

运行结果:

5.0
4.47213595499958
0.0

 

时间: 2024-07-28 22:10:35

Python - 带参数的方法的相关文章

C#线程调用带参数的方法

在 .NET Framework 2.0 版中,要实现线程调用带参数的方法有两种办法. 第一种:使用ParameterizedThreadStart. 调用 System.Threading.Thread.Start(System.Object) 重载方法时将包含数据的对象传递给线程. 使用 ParameterizedThreadStart 委托不是传递数据的类型安全的方法,因为 System.Threading.Thread.Start(System.Object) 方法重载接受任何对象. 这

C#创建线程带参数的方法_C#教程

1.无参数线程的创建 Thread thread = new Thread(new ThreadStart(getpic)); thread.Start(); private void showmessage() { Console.WriteLine("hello world"); } 2.带一个参数的线程 使用ParameterizedThreadStart,调用 System.Threading.Thread.Start(System.Object) 重载方法时将包含数据的对象传

ASP代码实现301重定向及带参数的方法

  由于改版需要,烈火网的一个栏目需要做301重定向,这是很久以前的烈火导航下的一个搜索,使用的是asp语言,但是蜘蛛喜欢,因此不能删除,只好写一个301重定向,但是原来的很多网址都是有参数的,例如TAG标签,形式如:liehuo_tag.asp?q=%C1%D2%BB%F0%CD%F8. 研究了一下,解决了301重定向带参数的问题,特来向大家分享,欢迎朋友多支持烈火网. 代码如下: <% if request.ServerVariables("HTTP_HOST")="

python 带参数的多重继承

1. 不带参数的多重继承 # 作者:hhh5460 # 时间:2017.07.18 class A(object): def show_x(self): print('A') class B(object): def show_y(self): print('B') class C(object): def show_z(self): print('C') class D(A, B, C): pass # 测试 if __name__ == '__main__': d = D() d.show_

在命令行下运行PHP脚本[带参数]的方法_php技巧

创建一个简单的文本文件,其中包含有以下PHP代码,并把它保存为hello.php: 复制代码 代码如下: <?php echo "Hello from the CLI"; ?> 现在,试着在命令行提示符下运行这个程序,方法是调用CLI可执行文件并提供脚本的文件名: #php phphello.php 输出Hello from the CLI 使用标准的输入和输出 你可以在自己的PHP脚本里使用这三个常量,以接受用户的输入,或者显示处理和计算的结果.要更好地理解这一点,可以看

ObjectDataSource控件的select方法如何应用类似select(sql)带参数的方法

问题描述 比如我有一个Result类,其中有一个Result.SearchResult(strSQL)的select方法,这个类还有更新,删除方法,我现在已经指定了更新,删除方法,而且Result.SearchResult(strSQL)也以指定,其中的strSql参数指定为None,strSQL是通过组合选择拼接而成的,我想让ObjectDataSource在程序中指定strSQL,完整的代码如何写?this.ResutlObjectDataSource.SelectParameters["s

C#带参数运行方法

比如aa.exe -autoaa.exe -main两组后缀,要求分别运行aa的某个线程,比如aa.exe -auto打开from1,aa.exe -main打开from2由于需要修改Program的Main方法,需要更加谨慎,因为一个结构清晰的Main对于后期维护是一个很好的帮助.以下的代码将解析参数,构造启动窗体,启动窗体三个逻辑分割为三个方法 Code 1 static class Program 2     { 3         /// <summary> 4         ///

C#,往线程里传带参数的方法

问题描述 List<SyncTableInfo>listTables=GetSyncTables();listTables=listTables.OrderBy(n=>n.Sort).ToList();string[]dataSource=new[]{"etl_scn","etl_aisien","erp-xy"};Thread[]thread=newThread[dataSource.Count()];for(inti=0;i

php include()带参数实现方法

具体方法详解 举例:假设在 index.php 中需要调用 inc.php?para=3 , inc.php  代码如下 复制代码 <?php     echo $_GET['para']; ?> 下面的写法是无法得到正确结果的: index.php  代码如下 复制代码 <?php     include dirname(__FILE__).'/inc.php?para=3'; ?> 稍微变通一下,把$_GET变量在include之前定义,则可以正常运行: index.php