Java_面向对象编程-对象在计算机中的执行原理

以如下代码为例

package com.badidu.object;

public class Test {
    public static void main(String[] args) {
        Student s1 = new Student();
//        1.创建面向对象

        s1.name = "boniu";
        s1.chinese = 100;
        s1.math = 100;
//        2.封装数据

        s1.printTotalScore();
        s1.printAvageScore();
//        3.调用方法

        Student s2 = new Student();
//        1.创建面向对象

        s2.name = "bonzai";
        s2.chinese = 59;
        s2.math = 100;
//        2.封装数据

        s2.printTotalScore();
        s2.printAvageScore();
//        3.调用方法
//        4.写完使用Ctrl+Alt+L排版
    }
}


对象在计算机中的执行原理?

  • Student s1 = new Student();
  • 每次 new Student();就是在堆内存中开辟一块内存区域代表一个学生对象
  • s1 变量里面记住的是学生对象的地址

在代码各部分对应的区域

  • 方法区:Test.class、Student.class
public class Student {
public class Test {
  • 栈内存:main函数
public static void main(String[] args) {
  • 堆内存:创建的对象、面向对象封装的数据
Student s1 = new Student();
//        1.创建面向对象

        s1.name = "boniu";
        s1.chinese = 100;
        s1.math = 100;
//        2.封装数据

        s1.printTotalScore();
        s1.printAvageScore();
//        3.调用方法

        Student s2 = new Student();
//        1.创建面向对象

        s2.name = "bonzai";
        s2.chinese = 59;
        s2.math = 100;
//        2.封装数据

        s2.printTotalScore();
        s2.printAvageScore();
//        3.调用方法
//        4.写完使用Ctrl+Alt+L排版

总的来说,就是栈内存的main方法,在堆内存中创建对象;堆内存的对象,根据方法区的面向对象模板 构造对象的数据结构,也根据面向对象模板 对对象进行数据的封装

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值