首页 最新 热门 推荐

  • 首页
  • 最新
  • 热门
  • 推荐

55. Java 类和对象 - 了解什么是对象

  • 25-04-22 13:21
  • 3489
  • 11077
juejin.cn

55. Java 类和对象 - 了解什么是对象

在 Java 编程中,对象是程序运行的核心。典型的 Java 程序会创建许多对象,这些对象通过调用方法相互交互。通过这些交互,程序可以完成各种任务,比如实现图形用户界面 (GUI)、运行动画、或者通过网络发送和接收信息。


📦 创建对象示例

让我们来看一个名为 CreateObjectDemo 的小程序,它创建了三个对象:

  • 一个 Point 对象
  • 两个 Rectangle 对象

我们先看代码,然后逐步拆解:

java
代码解读
复制代码
public class CreateObjectDemo { public static void main(String[] args) { // 创建一个 Point 对象和两个 Rectangle 对象 Point originOne = new Point(23, 94); Rectangle rectOne = new Rectangle(originOne, 100, 200); Rectangle rectTwo = new Rectangle(50, 100); // 输出 rectOne 的宽度、高度和面积 System.out.println("Width of rectOne: " + rectOne.width); System.out.println("Height of rectOne: " + rectOne.height); System.out.println("Area of rectOne: " + rectOne.getArea()); // 设置 rectTwo 的起点 rectTwo.origin = originOne; // 输出 rectTwo 的起点坐标 System.out.println("X Position of rectTwo: " + rectTwo.origin.x); System.out.println("Y Position of rectTwo: " + rectTwo.origin.y); // 移动 rectTwo 并输出它的新坐标 rectTwo.move(40, 72); System.out.println("X Position of rectTwo: " + rectTwo.origin.x); System.out.println("Y Position of rectTwo: " + rectTwo.origin.y); } }

Point 类

java
代码解读
复制代码
public class Point { public int x = 0; public int y = 0; // 构造器 public Point(int a, int b) { x = a; y = b; } }

Rectangle 类

java
代码解读
复制代码
public class Rectangle { public int width = 0; public int height = 0; public Point origin; // 四个构造器 public Rectangle() { origin = new Point(0, 0); } public Rectangle(Point p) { origin = p; } public Rectangle(int w, int h) { origin = new Point(0, 0); width = w; height = h; } public Rectangle(Point p, int w, int h) { origin = p; width = w; height = h; } // 移动矩形 public void move(int x, int y) { origin.x = x; origin.y = y; } // 计算矩形面积 public int getArea() { return width * height; } }

🚀 程序执行过程解析

🌟 对象的创建

  1. 创建 Point 对象

    java
    代码解读
    复制代码
    Point originOne = new Point(23, 94);
    • 调用 Point 类的构造方法,创建一个坐标为 (23, 94) 的点对象。
  2. 创建 Rectangle 对象 rectOne

    java
    代码解读
    复制代码
    Rectangle rectOne = new Rectangle(originOne, 100, 200);
    • 使用 Rectangle 类的构造方法,创建一个以 originOne 为起点、宽 100 高 200 的矩形对象。
  3. 创建 Rectangle 对象 rectTwo

    java
    代码解读
    复制代码
    Rectangle rectTwo = new Rectangle(50, 100);
    • 这是使用宽高参数的构造方法,起点默认为 (0, 0)。

🔄 对象的交互

  • 更改 rectTwo 的起点为 originOne

    java
    代码解读
    复制代码
    rectTwo.origin = originOne;

    两个矩形现在共享同一个起点对象 (23, 94)。

  • 移动 rectTwo 到新位置 (40, 72)

    java
    代码解读
    复制代码
    rectTwo.move(40, 72);

    调用 move() 方法后,rectTwo 的起点被更新,但由于 originOne 是共享的,rectOne 的起点也随之变化!

🏁 输出结果

yaml
代码解读
复制代码
Width of rectOne: 100 Height of rectOne: 200 Area of rectOne: 20000 X Position of rectTwo: 23 Y Position of rectTwo: 94 X Position of rectTwo: 40 Y Position of rectTwo: 72

🧹 对象的生命周期

  1. 创建阶段
    • 通过 new 关键字调用构造方法,分配内存并初始化对象。
  2. 使用阶段
    • 通过方法调用、字段访问等与对象交互。
  3. 销毁阶段
    • 当对象不再被引用时,Java 的垃圾回收器 (GC) 会自动回收内存。
注:本文转载自juejin.cn的Cache技术分享的文章"https://juejin.cn/post/7495603578439974931"。版权归原作者所有,此博客不拥有其著作权,亦不承担相应法律责任。如有侵权,请联系我们删除。
复制链接
复制链接
相关推荐
发表评论
登录后才能发表评论和回复 注册

/ 登录

评论记录:

未查询到任何数据!
回复评论:

分类栏目

后端 (14832) 前端 (14280) 移动开发 (3760) 编程语言 (3851) Java (3904) Python (3298) 人工智能 (10119) AIGC (2810) 大数据 (3499) 数据库 (3945) 数据结构与算法 (3757) 音视频 (2669) 云原生 (3145) 云平台 (2965) 前沿技术 (2993) 开源 (2160) 小程序 (2860) 运维 (2533) 服务器 (2698) 操作系统 (2325) 硬件开发 (2491) 嵌入式 (2955) 微软技术 (2769) 软件工程 (2056) 测试 (2865) 网络空间安全 (2948) 网络与通信 (2797) 用户体验设计 (2592) 学习和成长 (2593) 搜索 (2744) 开发工具 (7108) 游戏 (2829) HarmonyOS (2935) 区块链 (2782) 数学 (3112) 3C硬件 (2759) 资讯 (2909) Android (4709) iOS (1850) 代码人生 (3043) 阅读 (2841)

热门文章

103
后端
关于我们 隐私政策 免责声明 联系我们
Copyright © 2020-2025 蚁人论坛 (iYenn.com) All Rights Reserved.
Scroll to Top