Like Java, Python is also an object-oriented programming language. For a Python developer, The Zen Of Python is simply not too cool. In terms of creating a class and then declaring the methods and variables in the class, the amount of code in Python is simpler than that in Java. Now we compare the implementation of the same dog class in the two languages.
Classes, methods and variables in Python:
class Dog(object): """狗类""" # Class attributes, this class and subclass descendants and subclasses that inherit this class externally can be accessed even when this class is imported, which is equivalent to public static int leg = 4 in Java; leg = 4 # 4 legs # Single underscore, private class attribute, this class and subclasses can be accessed, but other files except this py file cannot be accessed, and this attribute cannot be imported by importing, which is equivalent to protected in Java _dogType = "Husky" # dog breed # Private class attributes can only be accessed inside this class, and cannot be accessed anywhere else, equivalent to private in java __dogColor = "white" # the color of the dog's coat @staticmethod def introduce(): """Static method""" print("I am a puppy") @classmethod def eat(cls): """类方法""" print("I like to eat bones") def __init__(self, name, age): """One of the construction methods, automatically called after the object is instantiated, adding properties to the object""" # Instance properties, objects can directly access self.name = name self.age = age # Private properties, objects cannot be accessed directly, only through methods self.__weight = 20 def run(self): print("Erha ran over quickly") def get_weight(self): return self.__weight def set_weight(self, weight): self.__weight = weight print("{} ate bones and now weighs {}kg".format(self.name, self.__weight)) # Program entry, equivalent to the main function in java if __name__ == '__main__': peiqi = Dog("Peqi", 18) print(peiqi.name) # output "peiqi" print(peiqi.age) # output 18 print(peiqi._dogType) # output "Husky" print(peiqi.__dogColor) # Error, cannot access private class attributes with double underscores print(peiqi.__weight) # An error is reported, the private attributes of the object cannot be directly accessed, but can only be modified and accessed through methods print(peiqi.get_weight()) # output 20 peiqi.set_weight(30) # Output "Peqi ate bones and now weighs 30kg" print(peiqi.get_weight()) # output 30
Summary : For attribute (variable) declarations in Python, no underscores indicate public attributes, single underscores indicate private attributes, which are accessible in the package, but cannot be imported from the outside world, and double underscores indicate private attributes, which can be used in classes and cannot be used elsewhere. This rule also applies to methods. The method parameter modified by @classmethod in the Python class has cls in the class method. The class method can be accessed through Dog.eat(), or through the object.Method name. The static method modified by @staticmethod can be accessed by both classes and objects without passing parameters. The self parameter in the parentheses is an instance method, which cannot be accessed through the class name. The method name is the exclusive property of the object.
Note: Variables starting with a double underscore and ending with a double underscore in Python are special variables that come with Python and can be accessed globally, so it is strongly recommended not to define such variable names!
Classes, methods and variables in Java:
package practice; public class Dog { // the number of legs of the dog public static int leg = 4; // dog breed protected static String dogType = "Husky"; // the color of the dog's coat private static String dogColor = "白色"; // object properties String name; int age; private double weight; // Construction method public Dog(String name, int age, double weight) { this.name = name; this.age = age; this.weight = weight; } public void run() { System.out.println("Erha ran over quickly"); } // Static method, equivalent to the method of the @staticmethod modifier in Python public static void introduce() { System.out.println("I am a puppy"); } public void eat() { System.out.println("I like to eat bones"); } // main method public static void main(String[] args) { Dog dog = new Dog("佩奇", 18, 20); System.out.println(dog.name); System.out.println(dog.age); System.out.println(dog.weight); dog.eat(); dog.introduce(); dog.run(); } }