jython是使用java实现的python,语法基本等同于python;
环境安装:https://download.csdn.net/download/shy_snow/21476227下载jython安装包然后执行java -jar jython-installer-2.7.2.jar 会弹出java的向导图形界面,一步步走下去就行了。
helloworld实现
>>> print('helloWorld!')
helloWorld!
>>>
1.变量,直接命名就行不需要定义类型,可以随意转型
x=6
2.if条件判断,if语句结尾是冒号:后面的print子语句前面要有四个空格来表示层级
>>> x = 3
>>> y = 2
>>> if x == y:
... print('x is equal to y')
... elif x > y:
... print('x is greater than y')
... else:
... print('x is less than y')
...
x is greater than y
3.循环
>>> for x in range(10):
... print x
...
0
1
2
3
4
5
6
7
8
9
4.异常处理try except:
>>> x = 8.97
>>> y = 0
>>> try:
... print('The rocket trajectory is: %f' % (x/y))
... except:
... print('Houston, we have a problem.')
...
Houston, we have a problem.
5.定义方法 def开头 :结尾,内容前面要有四个空格表示层级,方法结束空一行
def square_val(value):
return value * value
...
>>> print(square_val(3))
9
6.定义类,使用class关键字,构造器的名称都是__init__ 它第一个参数self就是新建的实例本身
>>> class my_object:
... def __init__(self, x, y):
... self.x = x
... self.y = y
...
... def mult(self):
... print(self.x * self.y)
...
... def add(self):
... print(self.x + self.y)
...
>>> obj1 = my_object(7, 8)
>>> obj1.mult()
56
>>> obj1.add()
15
7.保留关键字
and | assert | break | class | continue |
def | del | elif | else | except |
exec | finally | for | from | global |
or | pass | raise | return | |
try | while | with | yield |