目录
1. 语法基础差异
代码块界说
Java:- public class Example {
- public void method() {
- if (condition) {
- // code block
- }
- }
- }
复制代码 Python:- def method():
- if condition:
- # code block
复制代码 主要区别:
- Python 使用缩进来界说代码块,不必要花括号
- Python 不必要分号末端
- Python 不必要显式声明类(除非必要)
2. 变量声明和范例
Java:- String name = "John";
- int age = 25;
- List<String> list = new ArrayList<>();
复制代码 Python:- name = "John" # 动态类型,无需声明
- age = 25
- list = [] # 列表声明更简单
复制代码 主要区别:
- Python 是动态范例语言,不必要显式声明变量范例
- Python 的变量可以随时改变范例
- Python 的集合范例(列表、字典等)使用更简单
3. 面向对象编程
Java:- public class Person {
- private String name;
-
- public Person(String name) {
- this.name = name;
- }
-
- public void sayHello() {
- System.out.println("Hello, " + this.name);
- }
- }
复制代码 Python:- class Person:
- def __init__(self, name):
- self.name = name
-
- def say_hello(self):
- print(f"Hello, {self.name}")
复制代码 主要区别:
- Python 使用 self 代替 Java 的 this
- Python 不必要声明访问修饰符(public/private)
- Python 使用 __init__ 作为构造函数
- Python 的方法命名通常使用下划线命名法
4. 函数声明与调用
根本函数声明
Java:- public class Example {
- // 基本函数声明
- public int add(int a, int b) {
- return a + b;
- }
-
- // 静态方法
- public static void staticMethod() {
- System.out.println("Static method");
- }
-
- // 可变参数
- public void printAll(String... args) {
- for(String arg : args) {
- System.out.println(arg);
- }
- }
- }
复制代码 Python:- # 基本函数声明
- def add(a, b):
- return a + b
- # 静态方法
- @staticmethod
- def static_method():
- print("Static method")
- # 可变参数
- def print_all(*args):
- for arg in args:
- print(arg)
- # 带默认参数的函数
- def greet(name, greeting="Hello"):
- print(f"{greeting}, {name}")
- # 关键字参数
- def person_info(**kwargs):
- for key, value in kwargs.items():
- print(f"{key}: {value}")
复制代码 5. 继承与多态
Java:- public class Animal {
- protected String name;
-
- public Animal(String name) {
- this.name = name;
- }
-
- public void makeSound() {
- System.out.println("Some sound");
- }
- }
- public class Dog extends Animal {
- public Dog(String name) {
- super(name);
- }
-
- @Override
- public void makeSound() {
- System.out.println("Woof!");
- }
- }
复制代码 Python:- class Animal:
- def __init__(self, name):
- self.name = name
-
- def make_sound(self):
- print("Some sound")
- class Dog(Animal):
- def __init__(self, name):
- super().__init__(name)
-
- def make_sound(self):
- print("Woof!")
- # 多重继承示例
- class Pet:
- def play(self):
- print("Playing")
- class DomesticDog(Dog, Pet):
- pass
复制代码 6. 集合操作
列表/数组操作
Java:- List<String> list = Arrays.asList("a", "b", "c");
- Map<String, Integer> map = new HashMap<>();
- map.put("key", 1);
复制代码 Python:- list = ["a", "b", "c"]
- dict = {"key": 1}
- # 列表操作
- list.append("d")
- list[0] # 访问元素
- list[1:3] # 切片操作
- # 字典操作
- dict["new_key"] = 2
复制代码 7. 特殊方法与装饰器
特殊方法(把戏方法)
- class Person:
- def __init__(self, name):
- self.name = name
-
- def __str__(self):
- return f"Person: {self.name}"
-
- def __eq__(self, other):
- if isinstance(other, Person):
- return self.name == other.name
- return False
复制代码 属性装饰器
- class Person:
- def __init__(self):
- self._name = None
-
- @property
- def name(self):
- return self._name
-
- @name.setter
- def name(self, value):
- self._name = value
复制代码 8. 非常处置惩罚
Java:- try {
- throw new Exception("Error");
- } catch (Exception e) {
- System.out.println("Caught: " + e.getMessage());
- } finally {
- // 清理代码
- }
复制代码 Python:- try:
- raise Exception("Error")
- except Exception as e:
- print(f"Caught: {str(e)}")
- finally:
- # 清理代码
复制代码 9. Python特有特性
列表推导式
- # 生成 1-10 的平方数列表
- squares = [x**2 for x in range(1, 11)]
复制代码 切片操作
- list = [1, 2, 3, 4, 5]
- print(list[1:3]) # 输出 [2, 3]
复制代码 多重赋值
- a, b = 1, 2
- x, y = y, x # 交换变量值
复制代码 10. 快速入门发起
- 重点关注 Python 的缩进规则
- 风俗不使用范例声明
- 多使用 Python 的内置函数和特性
- 学习 Python 的列表推导式和切片操作
- 使用 f-string 举行字符串格式化
- 熟悉 Python 的命名规范(下划线命名法)
- 理解 Python 的继承机制,特别是 super() 的使用
- 把握 Python 的特殊方法(把戏方法)
- 学习使用装饰器
- 了解 Python 的非常处置惩罚机制
保举学习资源
记住,Python 推许简便和可读性,很多 Java 中的复杂结构在 Python 中都有更简单的实现方式。发起从简单的程序开始,逐步熟悉 Python 的特性和语法。
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |