2023.3.28——java面向对象的学习day8

  1. 类的介绍
    Java 中想要创建对象,必须先要有类的存在
    类指的是一组相关属性和行为的集合,我们将其理解为是一张对象的设计图

    1). 类和对象的关系

    	Java 中需要根据类,创建对象
    	
    	一个类,可以创建出多个对象
    

    2). 类的组成
    请添加图片描述
    3).创建对象和使用对象的格式
    请添加图片描述
    4).案例
    需求:定义一个手机类(Phone),内部编写两个成员变量(品牌 brand,价格 price)编写两个成员方法(打电话 call,发短信 sendMessage)方法中使用输出语句模拟打电话和发短信即可.编写一个手机测试类(TestPhone)创建手机类对象并调用成员变量赋值并打印,调用成员方法执行

package com.cwanxi.test;

public class Phone {
    private String brand;

    public String getBrand() {
        return brand;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    private int price;
    public void call(){
        System.out.println("打电话");
    }
    public void senMessage(){
        System.out.println("发短信");
    }
}

TestPhone

package com.cwanxi.test;

public class TestPhone {
    public static void main(String[] args) {
        Phone phone=new Phone();
        phone.setBrand("小米");
        phone.setPrice(10000);
        System.out.println(phone.getBrand()+phone.getPrice());
        phone.senMessage();
        phone.call();
    }
}


成员变量和局部变量的区别
在这里插入图片描述

this 关键字
this 代表当前类对象的引用(地址)


构造方法

	构造方法概述
	格式:
	方法名与类名相同,大小写也要一致
	没有返回值类型,连void都没有
	没有具体的返回值(不能由return带回结果数据)
class Student {
    int age;
    public Student(int age){
       this.age = 18;
   }
}

构造方法注意事项

构造方法的创建
如果没有定义构造方法,系统将给出一个默认的无参数构造方法
如果定义了构造方法,系统将不再提供默认的构造方法

构造方法的重载
构造方法也是方法,允许重载关系出现

推荐的使用方式
无参数构造方法,和带参数构造方法,都自己手动给出

面向对象三大特征

		封装  继承  多态

封装案例

package com.cwanxi.test;

public class WaterDispenser {
    private String brand;
    private String color;
    private int capacity;
    private String model;

    public WaterDispenser(String brand, String color, int capacity, String model) {
        this.brand = brand;
        this.color = color;
        this.capacity = capacity;
        this.model = model;
    }

    public String getBrand() {
        return brand;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public int getCapacity() {
        return capacity;
    }

    public void setCapacity(int capacity) {
        this.capacity = capacity;
    }

    public String getModel() {
        return model;
    }

    public void setModel(String model) {
        this.model = model;
    }

    public void show (String brand, String color, int capacity, String model) {
        System.out.println("品牌为:" + brand);
        System.out.println("颜色为:" + color);
        System.out.println("容量为:" + capacity + "L");
        System.out.println("模式为:" + model);
    }

}

Test

package com.cwanxi.test;

public class WaterTest {
    public static void main(String[] args) {
        WaterDispenser waterDispenser=new WaterDispenser("小米","红色",5,"自动");
        String brand=waterDispenser.getBrand();
        String color=waterDispenser.getColor();
        int capacity=waterDispenser.getCapacity();
        String model=waterDispenser.getModel();
        waterDispenser.show(brand,color,capacity,model);
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值