0
点赞
收藏
分享

微信扫一扫

day08_面向对象基础_内存关系

西曲风 03-03 12:30 阅读 3

零、今日内容

一、作业

package com.qf.homework;

import java.util.Arrays;

/**
 * --- 天道酬勤 ---
 *
 * @author QiuShiju
 * @date 2024/2/28
 * @desc
 */
public class Homework {

    public static void main(String[] args) {
        test();
    }

    //写一个方法 用于合并两个int类型的数组  合并法则如下
    // {1,2,5,3,8,9}{1,3,0}---->
    // {0,1,2,3,5,8,9} (合并,去重,并排序)
    public static void test(){
        int[] arr1 = {1,2,4,5,3,8,9};
        int[] arr2 = {1,0,3,4,5,8,10};

        // 1先合并
        int[] arr3 = new int[arr1.length + arr2.length];
        //System.out.println("合并前arr3:" + Arrays.toString(arr3));
        System.arraycopy(arr1,0,arr3,0,arr1.length);
        System.arraycopy(arr2,0,arr3,arr1.length,arr2.length);
        //System.out.println("合并后arr3:" + Arrays.toString(arr3));

        // 2先排序
        Arrays.sort(arr3);
        //System.out.println("排序后arr3:" + Arrays.toString(arr3));

        // 3去重
        /**
         * 1)重新创建一个数组,存储去重后的元素
         * 2)先创建一个和数组一样长的
         * 3)从原数组中取值,与新数组中每个元素比较,如果不相等则存储
         * 4)最后如果装不满,再创建小点的数组,将元素拷贝过来
         */
        // 创新创建新数组,存储不重复元素
        int[] arr4 = new int[arr3.length];
        int index = 0;
        for (int i = 0; i < arr3.length; i++) {
            int n = arr3[i];
            boolean flag = false;// 标志,false意味着不重复
            for (int j = 0; j < index; j++) {
                if (n == arr4[j]) {
                    flag = true;// 说明有重复
                }
            }
            if (!flag) {
                arr4[index] = n;
                index++;
            }
        }
        //System.out.println("arr4中不重复的元素:"+Arrays.toString(arr4) );
        int[] arr5 = new int[index];
        System.arraycopy(arr4,0,arr5,0,index);
        System.out.println("arr5中不重复的元素:"+Arrays.toString(arr5) );
    }
}

二、面向对象编程

面向对象编程(Object Oriented Programing ) , OOP

2.1 面向对象编程介绍

2.2 面向对象编程中的概念

例如: 手机类,人类,神仙类

例如:

手机类属性: 颜色,价格,品牌

人类属性: 身高,体重,年龄,性别

例如:

手机行为: 打电话,发短信

人类行为: 吃饭,睡觉,敲代码

人类的对象: 具体的人,比如你的同桌(他身高180,体重180,年龄18,他会吃饭,睡觉)

2.3 第一个类

package com.qf.oop;

/**
 * --- 天道酬勤 ---
 *
 * @author QiuShiju
 * @date 2024/2/28
 * @desc 手机类
 */
public class Phone {

    // 类中有属性和方法
    // 属性就是变量
    /**
     * 属性需要写在类中方法外,写法
     * [访问修饰符] 数据类型 属性名;
     * 访问修饰符可写可不写,目前写的话就写public
     */
    public double price;
    String brand;// 品牌
    String color;

    /**
     * 定义方法,在类中定义的方法不要加static
     * 其他与之前学的方法一模一样
     */
    // 打电话
    public void call(String phoneNum){
        System.out.println("打电话给"+phoneNum );
    }

    // 打游戏
    public void play() {
        System.out.println("打游戏" );
    }
}
package com.qf.oop;

/**
 * --- 天道酬勤 ---
 *
 * @author QiuShiju
 * @date 2024/2/28
 * @desc 英雄类
 */
public class Hero {
    int hp;// 血量
    int lv; // 等级
    String type;// 类型

    public void attack(){
        System.out.println("攻击" );
    }
}

2.4 创建对象

package com.qf.oop;

/**
 * --- 天道酬勤 ---
 *
 * @author QiuShiju
 * @date 2024/2/28
 * @desc 测试创建对象
 */
public class TestOOP1 {

    public static void main(String[] args) {
        // 数据类型 变量 = 值;
        // 类名 对象名 = new 类名();
        Phone phone = new Phone();
        Hero hero = new Hero();
    }
}

2.5 操作属性和方法

package com.qf.oop;

/**
 * --- 天道酬勤 ---
 *
 * @author QiuShiju
 * @date 2024/2/28
 * @desc 测试创建对象
 */
public class TestOOP1 {

    public static void main(String[] args) {
        // 数据类型 变量 = 值;
        // 类名 对象名 = new 类名();
        Phone phone = new Phone();
        Hero hero = new Hero();

        /**
         * 通过对象调用属性和方法
         */
        // 获得属性值
        // 对象名.属性
        // 刚创建的对象,属性都是默认值
        double price = phone.price;
        String color = phone.color;
        String brand = phone.brand;
        System.out.println(price );// 0.0
        System.out.println(color );// null
        System.out.println(brand );

        // 属性赋值
        // 对象.属性 = 值;
        phone.price = 3000.0;
        phone.color = "黑色";
        phone.brand = "HUAWEI";

        System.out.println(phone.brand );
        System.out.println(phone.price );
        System.out.println(phone.color );

        // 对象调用方法
        // 对象名.方法();
        // 有参数要传实参,有返回值可以接收
        phone.call("110");
        phone.play();
    }
}

2.6 类可以创建多个对象

public class TestOOP2 {

    public static void main(String[] args) {
        /***
         * - 一个类可以创建多个对象
         * - 对象和对象之间没有关系
         * - 每个对象都拥有属于自己的属性和方法
         */
        //
        // 创建第1个对象
        Phone p1 = new Phone();
        p1.brand = "OPPO";
        p1.price = 2999.0;
        System.out.println(p1.price );// 2999.0
        System.out.println(p1.brand );// OPPO

        System.out.println("------------" );

        // 创建第2个对象
        Phone p2 = new Phone( );
        System.out.println(p2.brand );// null
        System.out.println(p2.price );// 0.0

        // p1和p2对象没有任何关系
        // p1和p2对象都有属于自己的属性和方法
    }
}

2.7 多个引用指向一个对象

public static void main(String[] args) {

        Phone p1 = new Phone( );
        Phone p2 = p1;
        /**
         * 此时,p1和p2都是引用
         * 且都指向一个对象
         */
        p1.price = 3999.0;
        System.out.println(p2.price );// 3999
    }

image-20240228154022039

    public static void main(String[] args) {
        // 方法的参数列表是类时候,传入对象
        Phone p3 = new Phone( );
        test(p3);// p3赋值给方法的参数phone
        System.out.println(p3.brand );// 锤子
        System.out.println(p3.price );// 1999.0

    }
    // 当方法的参数列表是类名时,如何调用?
    public static void test(Phone phone) {// phone和上面的p3指向同一个对象
        phone.brand = "锤子";
        phone.price = 1999.0;
    }
}

三、对象内存

3.1 一个对象内存

image-20240228150018270

3.2 多个对象内存图

image-20240228151057465

四、成员变量和局部变量

成员变量(属性)局部变量
位置类中方法外方法内
作用域类中的所有方法只能在当前方法内使用
内存位置在堆中的对象中在栈中的方法里面
初始化时机new时赋默认值方法调用时赋值

image-20240228161346272

image-20240228161219732

五、练习

T1: 请写出编译运行后的结果。

class MyClass{
	int value;
}
public class TestRef{
    public static void main(String args[]){
        int a = 10;
        int b = a;
        b++ ;
        System.out.println(a);//10
        MyClass mc1 = new MyClass();
        mc1.value = 10;
        MyClass mc2 = mc1;
        mc2.value ++;
        System.out.println(mc1.value);//11
    }
}
思考:

T2: 编译运行TestReturnRef 程序,结果为:

class ClassA{
	int value = 10;
}
public class TestReturnRef{
    public static void main(String args[]){
        ClassA ca = new ClassA();
        ca = getObject();
        ca = getObject();
        ca = getObject();
        System.out.println(ca.value);
    }
    public static ClassA getObject(){
        ClassA newObject = new ClassA();
        newObject.value += 10;
        return newObject;
    }
}
思路:

T3:

class ClassA{
	int value;
}
public class TestClassA{
    public static void main(String args[]){
        int value = 10;
        changeInt(value);
        System.out.println(value);// 10
        ClassA ca = new ClassA();
        ca.value = 10;
        changeObject(ca);
        System.out.println(ca.value);// 11
    }
    public static void changeInt(int value){
    	value++;
    }
    public static void changeObject(ClassA ca){
    	ca.value++;
    }
}
思路

T4:

class ClassA{
	int value;
}
public class ChangeRef{
    public static void main(String args[]){
        ClassA ca = new ClassA();
        changeValue(ca);
        System.out.println(ca.value);//100
        changeRef(ca);
        System.out.println(ca.value);// 100
    }
    public static void changeValue(ClassA ca){
    	ca.value = 100;
    }
    public static void changeRef(ClassA ca){
        ca = new ClassA();
        ca.value = 200;
    }
}

// 调用changeValue方法时内存

image-20240228165534817

// 调用changeRef方法时内存

image-20240228165521070

// 方法执行完后

image-20240228165621997

补充

一个类文件中只能有一个public修饰的类

六、总结

举报

相关推荐

0 条评论