类与对象

面向对象编程(OOP)是Java的核心,本课学习类和对象的基本概念。

什么是面向对象

面向对象是一种编程思想,把数据和操作数据的方法封装在一起。

面向对象 vs 面向过程

特性 面向过程 面向对象
思维方式 按步骤执行 按对象组织
代码结构 函数为主 类和对象为主
适用场景 简单任务 复杂系统

类的定义

类是对象的模板,定义了对象的属性和方法。

语法

JAVA
public class 类名 {
    // 属性(成员变量)
    数据类型 属性名;
    
    // 方法(成员方法)
    返回类型 方法名(参数列表) {
        // 方法体
    }
}

示例:定义Student类

JAVA
public class Student {
    // 属性
    String name;
    int age;
    double score;
    
    // 方法
    public void study() {
        System.out.println(name + "正在学习");
    }
    
    public void showInfo() {
        System.out.println("姓名:" + name + ",年龄:" + age + ",分数:" + score);
    }
}
▶ 试一试

创建对象

使用new关键字创建对象。

语法

JAVA
类名 对象名 = new 类名();

示例:创建和使用对象

JAVA
public class StudentTest {
    public static void main(String[] args) {
        // 创建对象
        Student stu1 = new Student();
        
        // 设置属性
        stu1.name = "Alice";
        stu1.age = 20;
        stu1.score = 95.5;
        
        // 调用方法
        stu1.study();      // Alice正在学习
        stu1.showInfo();   // 姓名:Alice,年龄:20,分数:95.5
        
        // 创建另一个对象
        Student stu2 = new Student();
        stu2.name = "Bob";
        stu2.age = 22;
        stu2.score = 88.0;
        
        stu2.study();      // Bob正在学习
        stu2.showInfo();   // 姓名:Bob,年龄:22,分数:88.0
    }
}
▶ 试一试

构造方法

构造方法用于创建对象时初始化属性。

特点

特点 说明
方法名与类名相同 必须一致
没有返回类型 连void都没有
new时自动调用 不需要手动调用
可以重载 多个构造方法

无参构造

JAVA
public class Student {
    String name;
    int age;
    
    // 无参构造
    public Student() {
        System.out.println("Student对象被创建了");
    }
}

有参构造

JAVA
public class Student {
    String name;
    int age;
    
    // 有参构造
    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }
}

示例:构造方法

JAVA
public class Student {
    String name;
    int age;
    double score;
    
    // 无参构造
    public Student() {
        this.name = "未知";
        this.age = 0;
        this.score = 0;
    }
    
    // 有参构造
    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }
    
    // 全参构造
    public Student(String name, int age, double score) {
        this.name = name;
        this.age = age;
        this.score = score;
    }
    
    public void showInfo() {
        System.out.println("姓名:" + name + ",年龄:" + age + ",分数:" + score);
    }
}

public class ConstructorDemo {
    public static void main(String[] args) {
        Student s1 = new Student();
        s1.showInfo();  // 姓名:未知,年龄:0,分数:0
        
        Student s2 = new Student("Alice", 20);
        s2.showInfo();  // 姓名:Alice,年龄:20,分数:0.0
        
        Student s3 = new Student("Bob", 22, 95.5);
        s3.showInfo();  // 姓名:Bob,年龄:22,分数:95.5
    }
}
▶ 试一试
⚠️ 注意: 如果没有定义任何构造方法,Java会自动提供一个无参构造。如果定义了有参构造,就不会自动提供无参构造了。

this关键字

this代表当前对象的引用。

用途1:区分成员变量和局部变量

JAVA
public class Student {
    String name;
    
    public void setName(String name) {
        this.name = name;  // this.name是成员变量,name是参数
    }
}

用途2:调用其他构造方法

JAVA
public class Student {
    String name;
    int age;
    
    public Student() {
        this("未知", 0);  // 调用有参构造
    }
    
    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }
}

示例:this的使用

JAVA
public class Point {
    int x, y;
    
    public Point() {
        this(0, 0);
    }
    
    public Point(int x, int y) {
        this.x = x;
        this.y = y;
    }
    
    public void move(int x, int y) {
        this.x += x;
        this.y += y;
    }
    
    public void show() {
        System.out.println("点坐标:(" + x + ", " + y + ")");
    }
    
    public static void main(String[] args) {
        Point p = new Point(3, 5);
        p.show();       // 点坐标:(3, 5)
        p.move(2, -1);
        p.show();       // 点坐标:(5, 4)
    }
}
▶ 试一试

对象内存模型

内存分配

JAVA
Student stu = new Student("Alice", 20);

示例:多个引用指向同一对象

JAVA
public class MemoryDemo {
    public static void main(String[] args) {
        Student s1 = new Student("Alice", 20);
        Student s2 = s1;  // s2和s1指向同一个对象
        
        System.out.println(s1.name);  // Alice
        System.out.println(s2.name);  // Alice
        
        s2.name = "Bob";
        
        System.out.println(s1.name);  // Bob(s1也被修改了)
        System.out.println(s2.name);  // Bob
    }
}
▶ 试一试

多个对象

JAVA
public class MultiObject {
    public static void main(String[] args) {
        // 创建多个对象
        Student[] students = new Student[3];
        
        students[0] = new Student("Alice", 20);
        students[1] = new Student("Bob", 22);
        students[2] = new Student("Charlie", 21);
        
        // 遍历
        for (Student s : students) {
            s.showInfo();
        }
    }
}

方法中的对象参数

JAVA
public class StudentUtil {
    // 方法参数是对象
    public static void printStudent(Student stu) {
        System.out.println(stu.name + " - " + stu.age);
    }
    
    // 返回对象
    public static Student createStudent(String name, int age) {
        return new Student(name, age);
    }
    
    public static void main(String[] args) {
        Student alice = new Student("Alice", 20);
        printStudent(alice);  // Alice - 20
        
        Student bob = createStudent("Bob", 22);
        bob.showInfo();
    }
}

❓ 常见问题

Q 类和对象有什么区别?
A 类是模板,对象是实例。类定义了属性和方法的结构,对象是具体的实例。
Q 构造方法可以有返回值吗?
A 不可以。构造方法没有返回类型,连void都没有。
Q this可以省略吗?
A 当成员变量和局部变量没有同名时可以省略。但加上this更清晰。

📖 小节

📝 作业

  1. Rectangle类: 定义矩形类,包含长、宽属性,计算面积和周长的方法
  2. BankAccount类: 定义银行账户类,包含存款、取款、查询余额方法
  3. 对象数组: 创建5个学生对象,找出成绩最高的学生

下一课

下一课我们将学习 封装,了解如何保护对象的数据。

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏