# 定义名为student的类 class student: name = "momo" # 创建student类的对象s1 s1 = student() print(s1.name)
class student: def __init__(self, fullname): # 构造函数 self.name = fullname s1 = student("momo") print(s1.name)
// 定义名为student的类 class student { String name = "momo"; } // 主类,用于测试student类 public class main { public static void main(String[] args) { // 创建student类的对象s1 student s1 = new student(); System.out.println(s1.name); } }
// Student类 class Student { String name; // 实例变量 // 构造函数 public Student(String fullname) { this.name = fullname; } } // 主类,用于测试Student类 public class Main1 { public static void main(String[] args) { // 创建Student类的对象s1 Student s1 = new Student("Momo"); // 打印name System.out.println(s1.name); } }
以上就是“面向对象编程中的关键语法差异:Python 与 Java”的详细内容,更多请关注php中文网其它相关文章!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号