字符串

String

Python
'el' in 'hello'  # return True
'hello'[1: 3]  # return 'el'
' hello '.strip()  # return 'hello'
str(10)  # return '10'
Java
"hello".contains("el"); //return true
"hello".substring(1, 3); //return "el"
" hello ".trim(); //return "hello"
String.valueOf(10); //return "10"

StringBuilder

Python
s = 'h' + 'e' + 'l' + 'l' + 'o'  # 当然append()也是可以的
Java
//方法一
String s = "";
for (int i = 0; i < 1000, i++) {
    s = s + "," + i;
}

//方法二(推荐)
StringBuilder sb = new StringBuilder(1024); //StringBuilder有预分配缓冲区的功能,每次向其中新增字符时,不会创建新的临时对象
for (int i = 0; i < 1000, i++) {
    sb.append(",").append(i); //支持链式操作
}
String ss = sb.toString();

JavaBean类

符合命名规范的类。Python没有像JavaBeans那样严格的规范,但是有一些通用的最佳实践,例如使用__init__方法初始化属性,属性私有化,以及提供公共的getter和setter方法来访问或修改数据。

Python
class Car:
    def __init__(self, make, model, year):
        self._make = make  # 使用单下划线表示“受保护的”属性
        self._model = model
    
    @property
    def make(self):
        return self._make
    
    @make.setter
    def make(self, value):
        self._make = value
    
    @property
    def model(self):
        return self._model
    
    @model.setter
    def model(self, value):
        self._model = value
    
    def drive(self):
        print(f"The {self.year} {self.make} {self.model} is driving.")
Java
public class Car {
    private String make;
    private String model;

    public Car() {
    }

    public Car(String make, String model) {
        this.make = make;
        this.model = model;
    }

    public String getMake() {
        return make;
    }

    public void setMake(String make) {
        this.make = make;
    }

    public String getModel() {
        return model;
    }

    public void setModel(String model) {
        this.model = model;
    }

    public String toString() {
        return "Car{make = " + make + ", model = " + model + "}";
    }
    
    public void drive() {
        System.out.printf("The %s %s is driving.%n", this.make, this.model);
    }    
}

枚举类

Python和Java的枚举类在概念上相似,但实现和使用方式有所差异:

  • 语法差异:Python使用enum模块来定义枚举,而Java则是直接支持枚举类型作为语言的一部分。
  • 方法和功能:Java的枚举更像是一个类,可以拥有字段、方法及构造函数。而Python的枚举更加简洁,虽然也可以有方法,但通常用得较少。
  • 使用方式:在Java中枚举经常用于创建有限的常量集合,如状态机(State Machine)、单例模式(Singleton Pattern)等。Python中虽然也可以这样使用,但Python开发者可能会更偏向于使用其他方式,如简单的常量集合,而不是枚举。
Python
from enum import Enum

class Color(Enum):
    RED = 1
    GREEN = 2
    BLUE = 3

favorite_color = Color.RED
print(favorite_color.name, favorite_color.value)
Java
public class Main {
    public static void main(String[] args) {
        Weekday day = Weekday.SUN;
        if (day.dayValue == 6 || day.dayValue == 0) {
            System.out.println("Today is " + day + ". Work at home!");
        } else {
            System.out.println("Today is " + day + ". Work at office!");
        }
    }
}

enum Weekday {
    MON(1, "星期一"), TUE(2, "星期二"), WED(3, "星期三"), THU(4, "星期四"), FRI(5, "星期五"), SAT(6, "星期六"), SUN(0, "星期日");

    public final int dayValue;
    private final String chinese;

    private Weekday(int dayValue, String chinese) {
        this.dayValue = dayValue;
        this.chinese = chinese;
    }

    @Override
    public String toString() {
        return this.chinese;
    }
}