上一篇
类(Class) 是对象的模板或蓝图,用于描述具有相同属性和行为的对象集合。
🌰 例子:Person
类可以定义所有人的共同特征(姓名、年龄)和行为(问候、学习)。
public class Person { // 属性(成员变量) String name; int age; // 构造方法(初始化对象) public Person() { this.name = "未知"; this.age = 0; } // 方法(行为) public void sayHello() { System.out.println("大家好,我是" + name + ",今年" + age + "岁!"); } }
对象(Object) 是类的具体实例,通过new
关键字创建。
🌰 例子:Person p1 = new Person();
创建了一个Person
对象。
Person p1 = new Person(); p1.name = "张三"; p1.age = 20; p1.sayHello(); // 输出:大家好,我是张三,今年20岁!
方法(Method) 是实现特定功能的代码块,由以下部分组成:
public
、private
等void
或具体数据类型// 无参无返回值方法 public void greet() { System.out.println("你好!"); } // 有参有返回值方法 public int add(int a, int b) { return a + b; }
构造方法(Constructor) 用于初始化对象,方法名与类名相同,无返回类型。
public class Person { String name; int age; // 无参构造方法 public Person() {} // 有参构造方法 public Person(String name, int age) { this.name = name; this.age = age; } }
同一类中允许多个同名方法,但参数列表必须不同。
public class MathUtils { // 方法1:两个整数相加 public int add(int a, int b) { return a + b; } // 方法2:三个整数相加 public int add(int a, int b, int c) { return a + b + c; } }
this.name
表示成员变量,name
表示局部变量。this(参数)
必须放在构造方法首行。public class Person { String name; public Person() { this("未知"); // 调用有参构造方法 } public Person(String name) { this.name = name; } }
通过new
关键字实例化对象:
// 步骤1:声明对象引用 Person p1; // 步骤2:创建对象实例 p1 = new Person(); // 合并为一步 Person p2 = new Person("李四", 25);
通过对象引用访问成员:
p1.name = "王五"; p1.age = 30; p1.sayHello(); // 调用方法 int sum = mathUtils.add(5, 10); // 调用有返回值方法
修饰符 | 类内 | 包内 | 子类 | 任何位置 |
---|---|---|---|---|
private |
||||
default |
||||
protected |
||||
public |
通过private
隐藏属性,提供public
的getter/setter方法:
public class Person { private String name; public String getName() { return name; } public void setName(String name) { if (name != null && !name.isEmpty()) { this.name = name; } else { throw new IllegalArgumentException("姓名不能为空!"); } } }
通过extends
关键字实现继承:
public class Student extends Person { String school; public Student(String name, int age, String school) { super(name, age); // 调用父类构造方法 this.school = school; } @Override public void sayHello() { super.sayHello(); // 调用父类方法 System.out.println("我在" + school + "上学!"); } }
Person p = new Student("张三", 20, "清华大学"); p.sayHello(); // 输出:大家好,我是张三,今年20岁!我在清华大学上学!
// 向上转型(自动) Person person = new Student(); // 向下转型(需强制转换) if (person instanceof Student) { Student student = (Student) person; }
public class MathUtils { public static final double PI = 3.14159; public static int add(int a, int b) { return a + b; } } // 使用静态成员 MathUtils.add(5, 10); System.out.println(MathUtils.PI);
// 虚拟线程示例 ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor(); executor.submit(() -> { System.out.println("运行在虚拟线程上"); }); // 字符串模板 String name = "Java开发者"; String message = STR."欢迎, \{name}!今天是\{java.time.LocalDate.now()}";
<!-- pom.xml示例 --> <project> <properties> <maven.compiler.source>21</maven.compiler.source> <maven.compiler.target>21</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>3.2.5</version> </dependency> </dependencies> </project>
通过本文,你掌握了Java面向对象编程的核心概念:
this
关键字。💡 小贴士:多实践是掌握OOP的关键,赶紧写一个自己的类试试吧!
本文由 业务大全 于2025-08-22发表在【云服务器提供商】,文中图片由(业务大全)上传,本平台仅提供信息存储服务;作者观点、意见不代表本站立场,如有侵权,请联系我们删除;若有图片侵权,请您准备原始证明材料和公证书后联系我方删除!
本文链接:https://xdh.7tqx.com/wenda/691921.html
发表评论