Java面向对象

面向对象和面向过程

面向过程:当需要实现一个功能得时候,每一个具体的步骤都要亲力亲为,详细处理每一个细节。
面向对象:当需要实现一个功能的时候,不关心具体的步骤,而是找一个已经具有该功能的人,来帮我做事。

import java.util.Arrays;

public class Demo01PrintArray {
    public static void main(String[] args) {
        int[] array = {10, 20, 30, 40, 50};

        //要求打印格式为:[10,20,30,40,50]
        //使用面向过程,每一个步骤细节要亲力亲为。
        //就是先输出一个[,在for循环中输出遍历,如果是最后一个输出就打印],如果不是就输出,。
        System.out.print("[");
        for (int i = 0; i < array.length; i++) {
            if (i == array.length - 1) {//如果是最后一个元素
                System.out.println(array[i] + "]");
            } else {//如果不是最后一个元素
                System.out.print(array[i] + ",");
            }
        }
        System.out.println("===============");
        //使用面向对象编程
        //找一个JDK给我们提供好的Arrays类,其中的toString方法,直接就能把数组变成想要的格式字符串
        System.out.println(Arrays.toString(array));

    }
}

类和对象

类:是一组相关属性和行为的集合,可以看成是一类事物的模板,使用事物的属性特征和行为特征来描述该类的事物
类和对象的关系
类是一类事物的描述,是抽象的
对象是一类事物的实例,是具体的
类是对象的模板,对象是类的实体

通常情况下,一个类并不能直接使用,需要根据类创建一个对象,才能使用。
1、导包:也就是指出需要使用的类,在什么位置。
import 包名称.类名称;
import cn.itcast.day06.demo01.Student;
对于和当前类属于同一个包的情况,可以省略导包语句不写
2、创建 格式
类名称 对象 = new 类名称();
Student stu = new Student();
3、使用,分为两种情况
使用成员变量:对象名.成员变量名
使用成员方法:对象名.成员方法名(参数)
(也就是,想用谁,就用对象名.谁)
注意事项:
如果成员变量没有进行赋值,那么将会有一个默认字,规则和数组一样

public class Demo01Student {

    public static void main(String[] args) {
        //1.导包
        //我需要使用的student类,和我自己demo01.Student位于同一个包下,所以省略导包语句

        //2.创建,格式
        //类名称 对象 = new 类名称();
        //根据Student类,创建了一个名为stu的对象
        Student stu = new Student();

        //3.使用其中的成员变量,格式:
        //对象名.成员变量名
        System.out.println(stu.name);//null
        System.out.println(stu.age);//0

        //改变对象当中的成员变量变量数值内容
        //将右侧的字符串,赋值交给stu对昂当中的name成员变量
        stu.name = "小甜甜";
        stu.age = 22;
        System.out.println(stu.age);//18
        System.out.println(stu.name);//小甜甜
        System.out.println("============");

        //4.使用对象的成员方法,格式
        //对象名.成员方法名()
        stu.study();
        stu.eat();
        stu.sleep();
    }
}
/*
* 定义一个类,用来模拟“学生”事物,其中就有两个组成部分
*
* 属性 (是什么)
*   年龄
*   姓名
* 行为-方法(能做什么)
*   吃饭
*   睡觉
*   学习
* 对应到java类中
* 成员变量(属性)
*   String name;//姓名
*   int age;//年龄
* 成员方法(行为)
*   public  void eat() {}// 吃饭
*   public void sleep() {}
*   public void study() {}
/*
* 注意事项:
* 1、成员变量是直接定义在类当中的,是在方法外边。
* 2、成员方法不要写static关键字*/
public class Student {
    //成员变量
    String name;//姓名
    int age;

    //成员方法
    public void eat() {
        System.out.println("吃饭饭");
    }
    public void sleep() {
        System.out.println("睡觉觉");
    }
    public  void study() {
        System.out.println("学习");
    }

}

手机类练习

定义一个手机类

手机类
/
* 定义一个类,用来模拟“手机”事物
*
* 属性:品牌、颜色、价格
* 行为:打电话、发短信、
*
* 对应到类当中:
* 成员变量(属性)
*   String barand;//品牌
*   double price;//价格
*   String color;//颜色
* 成员方法(行为)
*   public void call(){}//打电话
*   public void sendMessage(){}//群发消息
*
* */
public class Phone {

    //成员变量(属性)
    String barand;//品牌
    double price;//价格
    String color;//颜色

    //成员方法(行为)
    public void call(String who) {
        System.out.println("给" + who + "打电话");
    }//打电话

    public void sendMessage() {
        System.out.println("群发短信");
    }//群发消息
}
一个对象的内存图

完整的创建对象,访问成员对象,调用成员方法,内存当中栈、堆、和方法区所发生的事情
在这里插入图片描述

public class Demo02phoneOne {

    public static void main(String[] args) {
        //根据phone类,创建一个名为one的对象
        //格式: 类名 对象名 = new 类名称();
        Phone one  = new Phone();
        System.out.println(one.barand);//null
        System.out.println(one.price);//0.0
        System.out.println(one.color);//null
        System.out.println("================");

        one.barand = "苹果";
        one.price = 8388.0;
        one.color = "黑色";
        System.out.println(one.barand);
        System.out.println(one.price);
        System.out.println(one.color);
        System.out.println("================");
        one.call("乔布斯");
        one.sendMessage();

    }
}
两个对象使用同一个方法的内存图

在这里插入图片描述

package cn.itcast.day06.demo02;

public class Demo02phonetwo {

    public static void main(String[] args) {

        Phone one  = new Phone();
        System.out.println(one.barand);//null
        System.out.println(one.price);//0.0
        System.out.println(one.color);//null
        System.out.println("================");

        one.barand = "苹果";
        one.price = 8388.0;
        one.color = "黑色";
        System.out.println(one.barand);
        System.out.println(one.price);
        System.out.println(one.color);
        System.out.println("================");
        one.call("乔布斯");
        one.sendMessage();

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

        Phone two  = new Phone();
        System.out.println(two.barand);//null
        System.out.println(two.price);//0.0
        System.out.println(two.color);//null
        System.out.println("================");

        two.barand = "三星";
        two.price = 5999.0;
        two.color = "蓝色";
        System.out.println(two.barand);
        System.out.println(two.price);
        System.out.println(two.color);
        System.out.println("================");
        two.call("欧巴");
        two.sendMessage();

    }
}
两个对象的引用指向同一个对象的内存空间

由于上面两个对象使用同一个方法,内存占用并没有产生联系,因为two也是用new的,但是如果不new,将one赋值给two时内存占用回如何?
在这里插入图片描述

public class Demo02phoneSame {

    public static void main(String[] args) {

        Phone one  = new Phone();
        System.out.println(one.barand);//null
        System.out.println(one.price);//0.0
        System.out.println(one.color);//null
        System.out.println("================");

        one.barand = "苹果";
        one.price = 8388.0;
        one.color = "黑色";
        System.out.println(one.barand);
        System.out.println(one.price);
        System.out.println(one.color);
        System.out.println("================");
        one.call("乔布斯");
        one.sendMessage();

        System.out.println("================");
        //将one当中保存的地址对象赋值给two

        Phone two  = one;
        System.out.println(two.barand);//苹果
        System.out.println(two.price);//8388.0
        System.out.println(two.color);//黑色
        System.out.println("================");

        two.barand = "三星";
        two.price = 5999.0;
        two.color = "蓝色";
        System.out.println(two.barand);
        System.out.println(two.price);
        System.out.println(two.color);
        System.out.println("================");
        two.call("欧巴");
        two.sendMessage();

    }
}

使用对象类型作为方法的参数

当一个对象作为参数传递到方法当中时,实际上传递进去的是对象的地址值。
在这里插入图片描述

public class Demo01PhonParm {
    public static void main(String[] args) {
        Phone one = new Phone();
        one.barand = "苹果";
        one.color = "土豪金" ;
        one.price = 8388.0;

        method(one);//传递进去的参数其实就是地址值

    }
    public static void method(Phone param){
        System.out.println(param.color);//土豪金
        System.out.println(param.price);//8388.0
        System.out.println(param.barand);//苹果


    }
}

使用对象类型作为方法的返回值

当对象类型作为方法的返回值时,返回值其实就是对象的地址。
在这里插入图片描述

public class Demo01PhoneReturn {
    public static void main(String[] args) {
        Phone two = getPhone();
        System.out.println(two.color);//黑色
        System.out.println(two.price);//8388.0
        System.out.println(two.barand);//苹果

    }
    public static Phone getPhone(){
        Phone one  = new Phone();
        one.barand = "苹果";
        one.price = 8388.0;
        one.color = "黑色";
        return  one;
    }
}

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zgDaren

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值