类和对象(上)

简介: 类和对象(上)

🎇面向对象和面向过程


我们在接触C语言开始其实一直在学习的就是面向过程,在Java和C++中我们接触的是面向对象

面向对象的语言简称OOP


用生活中的例子来理解:

洗衣服的时候:

人将衣服放到盆里,倒水,倒洗衣液,揉搓,二次清洗,晾衣服这样的一系列过程称为面向过程

也就是说人亲自干这些事情

将衣服倒进洗衣机后,加入洗衣液,人就不用管了,剩下的交给洗衣机就好,人不用知道洗衣机是咋洗衣服的,这就叫做面向对象,也就是多个对象之间的交互。


✨类


我们怎样定义一个类

首先,类名要用大驼峰

在一个类里面,有字段和方法,字段也叫成员变量或属性,方法又叫做成员行为,下面来看一段代码


class   Rabbit{
    //定义成员变量
    public     String  name;
    public     String  gender;
    public     String  color;
    public     String  weight;
    public     String  age;
    //定义成员方法
    public void   swag(){
        System.out.println(name+"摇尾巴");
    }
    public void  eat(){
        System.out.println(name+"吃青草");
    }
}


就像这段代码里面,这就是一个类

有了类,就要通过类产生对象,这一过程又叫做对象的实例化,做法是通过new关键字来创建对象,继续用以上代码来看


public class TestDemo {
    public static void main(String[] args) {
        Rabbit rabbit1=new Rabbit();
        Rabbit rabbit2=new Rabbit();
        Rabbit rabbit3=new Rabbit();
        //对成员变量赋值
        rabbit1.name="小白";
        rabbit1.gender="雄性";
        rabbit1.color="白色";
        rabbit1.age="2岁";
        rabbit2.name="小白";
        rabbit2.gender="雄性";
        rabbit2.color="白色";
        rabbit2.age="2岁";
        rabbit3.name="小白";
        rabbit3.gender="雄性";
        rabbit3.color="白色";
        rabbit3.age="2岁";
    }
}

通过以上代码段我可以发现一个问题,通过new可以实例化多个对象


public class TestDemo {
    public static void main(String[] args) {
        Rabbit rabbit=new Rabbit();
       /* Rabbit rabbit2=new Rabbit();
        Rabbit rabbit3=new Rabbit();
        rabbit1.gender;*/
        System.out.println(rabbit.name);
        System.out.println(rabbit.age);
        System.out.println(rabbit.color);


我们可以发现,在未进行特别注意赋值时,成员变量有自己的默认值,其中我们要注意引用类型的默认值


public class TestDemo {
    public static void main(String[] args) {
        Rabbit rabbit=new Rabbit();
       /* Rabbit rabbit2=new Rabbit();
        Rabbit rabbit3=new Rabbit();
        rabbit1.gender;*/
        System.out.println(rabbit.name);
        System.out.println(rabbit.age);
        System.out.println(rabbit.color);
        System.out.println(rabbit.flag);
        System.out.println("===============");
        //对成员变量赋值
        rabbit.name="小白";
        rabbit.gender="雄性";
        rabbit.color="白色";
        rabbit.show();
        rabbit.eat();
        rabbit.swag();

2c232372ad6b45f5b11bc53dc594c12a.png


调用了成员方法,看上面的代码

在后期学习的时候建议一个Java文件,一个类

🎉现在让我们来看一看构造方法,为哈要看呢,因为在new一个对象时,一定会使用构造,通过构造,才能实例化对象

现在上代码


class Dog{
    public int age;
    public String name;
    public  String  color;
    public void bark(){
        System.out.println(name+"叫");}
        public void swag(){
            System.out.println(name+"摇尾巴");}
            public void show(){
                System.out.println("姓名"+name+"年龄"+age+"颜色"+color);
            }
    public  Dog(){
        System.out.println("不带参数的构造方法");
    }
    public Dog(int age, String name) {
        this.age = age;
        this.name = name;
        System.out.println("调用带两个参数的构造方法");
    }
    public Dog(String name, String color) {
        this.name = name;
        this.color = color;
        System.out.println("调用带两个参数的构造方法");
    }
    public Dog(int age, String name, String color) {
        this.age = age;
        this.name = name;
        this.color = color;
        System.out.println("调用带3个参数的构造方法");
    }
}
public class TestDemo {
    public static void main(String[] args) {
        Dog  dog=new Dog();
        dog.name="豆豆";
        dog.age=2;
        dog.color="灰色";
        dog.show();
        dog.bark();
        dog.swag();
    }
}

如上述代码,看一眼


class Date {
    public int year;
    public int month;
    public int day;
    public void printDate() {
        System.out.println(year + "-" + month + "-" + day);
    }
    public static void main(String[] args) {
        Date date = new Date();
        date.printDate();
    }
}


这段代码没有任何构造方法,编译器会自动生成不带参数的构造方法,如果自己写了,编译器就不会提供了


class Date {
    public int year;
    public int month;
    public int day;
    public  Date(){
        this.year=2022;
        this.month=6;
        this.day=12;
        this.printDate();
        System.out.println("不带参数的构造方法");
    }
    public Date(int year, int month, int day) {
        this.year = 2022;
        this.month = 3;
        this.day = 15;
        System.out.println("带三个参数的构造方法");
    }
    public void printDate() {
        System.out.println(year + "-" + month + "-" + day);
    }
    public static void main(String[] args) {
        Date date1 = new Date();
        date1.printDate();
        Date date2 = new Date(1998,5,8);
        date2.printDate();
    }
}
class Date {
    public int year;
    public int month;
    public int day;
    public  Date(){
        /*this.year=2022;
        this.month=6;
        this.day=12;
        this.printDate();
        System.out.println("不带参数的构造方法");*/
        this(1998,5,8);//代表调用另一个带有三个参数的成员方法
    }
    public Date(int year, int month, int day) {
        this.year = 2022;
        this.month = 3;
        this.day = 15;
        System.out.println("带三个参数的构造方法");
    }
    public void printDate() {
        System.out.println(year + "-" + month + "-" + day);
    }
    public static void main(String[] args) {
        Date date1 = new Date();
        date1.printDate();
        Date date2 = new Date(1998,5,8);
        date2.printDate();
    }
}


this引用所需要注意的点:


1.this引用代表当前对象的引用


2.this引用必须放在成员方法里面


3.this引用在代表调用另一个构造方法时,必须放在成员方法的第一行


4.this引用不能形成环的调用


🎈封装


封装代表面向对象的三大重要思想之一


啥叫封装呢,通俗一点解释也就是说一部手机,只漏出接口,实际内部的实现并不让用户发现


在Java语言中,也就是说,写一个公开的接口,让用户使用,不让用户知道这个具体是咋实现的


下面上代码


 class Date {
    public int year;
    private int month;
    public int day;
    public  Date(){
        /*this.year=2022;
        this.month=6;
        this.day=12;
        this.printDate();
        System.out.println("不带参数的构造方法");*/
        this(1998,5,8);//代表调用另一个带有三个参数的成员方法
    }
    public Date(int year, int month, int day) {
        this.year = 2022;
        this.month = 3;
        this.day = 15;
        System.out.println("带三个参数的构造方法");
    }
    public void printDate() {
        System.out.println(year + "-" + month + "-" + day);
    }
    public static void main(String[] args) {
        Date date1 = new Date();
        date1.printDate();
        Date date2 = new Date(1998,5,6);
        date2.printDate();
    }
}


在将month加上private以后,打印结果如下

不能打印出1998 5.18,因为private只能在当前类中使用,而default只能在同一个包中使用

代码实现封装,提供接口实现month的使用


 public class Date {
    public int year;
    private int month;
    public int day;
    public  Date(){
        /*this.year=2022;
        this.month=6;
        this.day=12;
        this.printDate();
        System.out.println("不带参数的构造方法");*/
        this(1998,5,8);//代表调用另一个带有三个参数的成员方法
    }
    public  void  setMonth(int month){
        this.month=month;
    }
    public int getMonth(){
        return month;
    }
    public Date(int year, int month, int day) {
        this.year =year;
        this.month =month;
        this.day =day;
        System.out.println("带三个参数的构造方法");
    }
    public void printDate() {
        System.out.println(year + "-" + month + "-" + day);
    }
    public static void main(String[] args) {
        Date   date=new  Date();
        date.setMonth(5);
        System.out.println("获取当前的月份"+date.getMonth());
        date.printDate();
    }
    /*public static void main1(String[] args) {
        Date date1 = new Date();
        date1.printDate();
        Date date2 = new Date(1998,5,6);
        date2.printDate();
    }*/
}

如果所有成员变量都带了private,那就点击generate,点击constructer,点击getter  and   setter;


package com.bit.www;//声明当前文件是在哪个包底下
/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: WHY
 * Date: 2022-06-13
 * Time: 9:45
 */
public class TestDemo {
}


这就是包的概念


今天的讲解到此结束,我们下期再见!

相关文章
|
8天前
|
域名解析 人工智能 Linux
|
18天前
|
Linux Go 开发工具
手机上跑 DeepSeek 智能体:开源安卓客户端 DSH Go 安装与使用指南
DSH Go 是一款开源安卓客户端,将 DeepSeek Harness 智能体引擎装进手机:免 Root、免 Termux,一键安装即用;支持本机运行(内置 Ubuntu+Node.js)或远程接管电脑会话;具备插话打断、过程折叠、屏幕操控、端到端加密分享等特色功能,真正实现智能体“口袋化”。
16946 62
|
16天前
|
人工智能
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
进不去 Antigravity 或 Gemini,提示所在地区不可用?三步查清 Google 账号关联地区、官方更正表单理由选法与改区注意事项。
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
|
2天前
|
人工智能 安全 网络安全
|
6天前
|
云安全 数据采集 人工智能
|
7天前
|
Linux API iOS开发
【DSH】DeepSeek Harness新手保姆教程(下载+安装+配置+使用,2026最新实测)
DeepSeek Harness(dsh)是 DeepSeek AI 开源的智能体框架,秉持“一切皆插件”理念,支持读文件、改代码、执行命令、处理表格等自动化任务;免费开源,兼容 Win/macOS,需配 API 密钥使用。(239字)
1250 2
|
13天前
|
人工智能 开发工具 开发者
2026最新codex下载安装保姆级使用教程(下载+安装+登录+使用,含所有历史版本)
Codex 是 OpenAI 推出的开源 AI 编程智能体,跑在本地终端和桌面客户端里,能读懂整个代码仓库,一句自然语言就能让它改代码、执行命令、跑测试、修 Bug,支持 Windows、macOS、Linux 三大平台。本文把 Codex下载、安装、登录、汉化、基础使用一次讲全,并整理了国内用户最常踩的坑,跟着做 10 分钟就能跑起来。
2338 7
|
18天前
|
弹性计算 人工智能 API
Codex超详细入门指南(下载·安装·登录·上手,全版本覆盖,2026最新版)
建议收藏本教程,2026 全年各版本更新随时回来查阅。如果你在搭建过程中遇到任何问题,欢迎结合 阿里云工单系统 提交技术支持请求,或在 阿里云开发者社区 搜索 Codex 相关话题交流。 祝你编码愉快,效率翻倍!
3808 22
|
19天前
|
Linux API iOS开发
【DSH】DeepSeek Harness新手保姆教程(下载+安装+配置+使用,2026最新实测)
DeepSeek Harness(dsh)是DeepSeek AI开源的智能体框架,秉持“一切皆插件”理念,支持读文件、改代码、跑命令、处理表格、联网搜索等操作。免费开源,兼容Windows/macOS/Linux,提供桌面端与网页端,可灵活安装插件扩展能力。