文章目录
类和对象
面向对象的初步认知
什么是面向对象
Java是一门纯面向对象的语言(Object Oriented Program,简称OOP),在面向对象的世界里,一切皆为对象。面向对象是解决问题的一种思想,主要依靠对象之间的交互完成一件事情。
面向对象与面向过程
传统洗衣服过程
传统洗衣服所注重的是过程,每个过程都需要注意细节,多一步,少一步都可能出现问题。按照该种方式来写代码,将来扩展或者维护起来会比较麻烦。
现代洗衣服过程
这里有四个对象:人,衣服,洗衣服,洗衣机
与传统洗衣服过程不同的是虽然都是由洗衣机,衣服,人,洗衣粉都有参与进来。人只需要把洗衣粉,放进洗衣机,并开启洗衣机就行,人不用关心洗衣机是如何洗衣服的过程
🍉🍉🍉🍉🍉🍉🍉🍉🍉🍉🍉🍉🍉🍉🍉🍉🍉🍉🍉🍉🍉🍉🍉🍉
以面向对象方式来进行处理,就不关注洗衣服的过程,具体洗衣机是怎么来洗衣服,如何来甩干的,用户不用去关心,只需要将衣服放进洗衣机,倒入洗衣粉,启动开关即可,通过对象之间的交互来完成的。
注意:面向过程和面相对象并不是一门语言,而是解决问题的方法,没有那个好坏之分,都有其专门的应用场景。
类定义和使用
面相对象程序设计关注的是对象,而对象是现实生活中的实体。如小猫
猫的品种,颜色,体重,猫会抓老鼠,吃饭…
这些简化的抽象结果计算机也不能识别,开发人员可以采用某种面相对象的编程语言来进行描述。
🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱
简单认识类
类是用来对一个实体(对象)来进行描述的,主要描述该实体(对象)具有哪些属性(外观尺寸等),哪些功能(用来干啥)。这样的话计算机就可以进行识别了。
描述一只小猫
猫的属性有: 品种
体重
颜色
猫的行为有:吃饭
玩耍
睡觉
class Cat{
public String varieties;//成员变量
public int weight;//成员变量
public String color;//成员变量
public void eat() {
System.out.println("吃饭");//成员方法
}
public void play(){
System.out.println("玩耍");//成员方法
}
public void sleep(){
System.out.println("睡觉");//成员方法
}
}
通常情况下我们把猫的属性称为成员变量(这与局部变量不同不用赋初值,后面会讲到),把猫的行为成员方法,
注意事项
类名注意采用大驼峰定义
成员前写法统一为public
一般一个文件当中只定义一个类
main方法所在的类一般要使用public修饰(注意:Eclipse默认会在public修饰的类中找main方法)
public修饰的类必须要和文件名相同
不要轻易去修改public修饰的类的名称,如果要修改,通过开发工具修改
🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱
类的实例化
什么是实例化
定义了一个类,就相当于在计算机中定义了一种新的类型,与int,double类似,只不过int和double是java语言自
带的内置类型,而类是用户自定义了一个新的类型。如上面的猫,它们都是类(一种新定义的类型)有了这些自定义的类型之后,就可以使用这些类来定义实例(或者称为对象)。
用类类型创建对象的过程,称为类的实例化,在Java中采用new关键字,配合类名来实例化对象。
class Cat{
public String varieties;
public int weight;
public String color;
public void eat() {
System.out.println("吃饭");
}
public void play(){
System.out.println("玩耍");
}
public void sleep(){
System.out.println("睡觉");
}
}
public class study {
public static void main(String[] args) {
Cat cat1 =new Cat();
cat1.varieties="布偶";
cat1.weight=20;
cat1.color="白色";
cat1.eat();
cat1.play();
cat1.sleep();
Cat cat2 =new Cat();
cat2.varieties="缅因";
cat2.weight=15;
cat1.color="黄色";
cat2.eat();
cat2.play();
cat2.sleep();
}
}
注意事项
new 关键字用于创建一个对象的实例.
使用 . 来访问对象中的属性和方法.
同一个类可以创建对个实例.(我在这里实例化了俩个对象cat1和cat2)
类和对象的说明
-
类只是一个模型一样的东西,用来对一个实体进行描述,限定了类有哪些成员.
-
类是一种自定义的类型,可以用来定义变量.
-
一个类可以实例化出多个对象,实例化出的对象 占用实际的物理空间,存储类成员变量
-
做个比方。类实例化出对象就像现实中使用建筑设计图建造出房子,类就像是设计图,只设计出需要什么东
西,但是并没有实体的建筑存在,同样类也只是一个设计,实例化出的对象才能实际存储数据,占用物理空间
我画了一个内存图来举例子
每个对象都有自己的成员
cat1和cat2是引用变量在栈上开辟空间用来存它俩对象(即成员变量或(称为局部变量))的地址。成员方法在方法区上不占用内存,何时用合适再调。
每次new一次即实例化一次就开辟一次空间
这与数组的引用基本一样,我的上一篇文章中提到过 对了
1.当一个对象赋值null的时候这个引用不知向任何对象。
2.一个引用只能指向一个对象
3.引用不能指向引用
🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱
this引用
this引用指向当前对象(成员方法运行时调用该成员方法的对象),在成员方法中所有成员变量的操作,都是通过该
引用去访问。只不过所有的操作对用户是透明的,即用户不需要来传递,编译器自动完成。
class Cat{
public void birth(int year,int month,int day){
System.out.println("猫的生日是"+year+""+month+""+day);
}
}
public class study {
public static void main(String[] args) {
Cat cat1 =new Cat();
cat1.birth(2023,1,1);
Cat cat2 =new Cat();
cat2.birth(2022,12,31);
}
}
以这个为基础在方法后面改一下
class Cat{
int year;
int month;
int day;
public void birth(Cat this,int year,int month,int day){
this.year=year;
this.month=month;
this.day=day;
System.out.println("猫的生日是"+year+""+this.month+""+this.day);
}
}
public class study {
public static void main(String[] args) {
Cat cat1 =new Cat();
cat1.birth(2023,1,1);
Cat cat2 =new Cat();
cat2.birth(2022,12,31);
}
}
运行成功
通过这个可以知道
每个方法第一参数是this
this代表的是当前对象的引用
this.year=year; this.month=month; this.day=day;
this代表当前引用的对象,即cat1调用这个方法的话就是给cat1当前的对象year赋值,cat2调用这个方法的话就是给cat2当前的对象year赋值,如上面的内存图所视,year为cat1和cat2引用的对象,cat1和cat2所引用的对象之间是互不干扰。
this引用的特性
-
this的类型:对应类类型引用,即哪个对象调用就是哪个对象的引用类型
-
this只能在"成员方法"中使用。//在静态方法不能使用。
-
在"成员方法"中,this只能引用当前对象,不能再引用其他对象
-
this是“成员方法”第一个隐藏的参数,编译器会自动传递,在成员方法执行时,编译器会负责将调用成员方法
对象的引用传递给该成员方法,this负责来接收
除此之外this可以访问成员方法
class Cat{ int year; int month; int day; public void func(){ System.out.println("ok"); this.get(2023,1,1); } public void get(int year,int month,int day){ this.year=year; this.month=month; this.day=day; System.out.println("猫的生日是"+year+""+this.month+""+this.day); } } public class study { public static void main(String[] args) { Cat cat1 =new Cat(); cat1.func(); } }
注意:this不能再静态方法中使用
this可以访问构造方法 这个下面再讲
对象的构造及初始化
构造方法
构造方法(也称为构造器)是一个特殊的成员方法,名字必须与类名相同,在创建对象时,由编译器自动调用,并且
在整个对象的生命周期内只调用一次。总而言之就是必须与类名一样,没有返回值。如果自己写了构造方法编译器,便不会提供,仅会调用自己写的。
class Cat{
int year;
int month;
int day;
public Cat(){//这个为构造方法
System.out.println("iii");
}
public void birth(Cat this,int year,int month,int day){
this.year=year;
this.month=month;
this.day=day;
System.out.println("猫的生日是"+year+""+this.month+""+this.day);
}
}
public class study {
public static void main(String[] args) {
Cat cat1 =new Cat();
cat1.birth(2023,1,1);
Cat cat2 =new Cat();
cat2.birth(2022,12,31);
}
}
出现了俩次iii即cat1和cat2都调用了一次
注意:构造方法的作用就是对对象中的成员进行初始化,并不负责给对象开辟空间。
特性
-
名字必须与类名相同
-
没有返回值类型,设置为void也不行
-
创建对象时由编译器自动调用,并且在对象的生命周期内只调用一次(相当于人的出生,每个人只能出生一次)
-
构造方法可以重载(用户根据自己的需求提供不同参数的构造方法)
class Cat{
int year;
int month;
int day;
public Cat(){
System.out.println("ok");
}
public Cat(int year,int month,int day){
this.year=year;
this.month=month;
this.day=day;
System.out.println("猫的生日是"+year+""+this.month+""+this.day);
}
}
public class study {
public static void main(String[] args) {
Cat cat1 =new Cat(2023,1,1);
Cat cat2=new Cat();
}
}
public Cat(){ }
public Cat(int year,int month,int day){
}
这俩个实现了重载。
上述两个构造方法:名字相同,参数列表不同,因此构成了方法重载。
- 如果用户没有显式定义,编译器会生成一份默认的构造方法,生成的默认构造方法一定是无参的。没有定义任何构造方法,编译器会默认生成一个不带参数的构造方法
**注意:**一旦用户定义,编译器则不再生成。
-
构造方法中,可以通过this调用其他构造方法来简化代码
class Cat{ int year; int month; int day; public Cat(){ this(2023,1,1); System.out.println("ok"); } public Cat(int year,int month,int day){ this.year=year; this.month=month; this.day=day; System.out.println("猫的生日是"+year+""+this.month+""+this.day); } } public class study { public static void main(String[] args) { Cat cat2=new Cat(); } }
this(2023,1,1);
调用了public Cat(int year,int month,int day)
因此可以说明this可以调用构造方法。
this(…)必须是构造方法中第一条语句
并且不能形成环
如
class Cat{
int year;
int month;
int day;
public Cat(){
this(2023,1,1);//不能这样用
System.out.println("ok");
}
public Cat(int year,int month,int day){
this();//不能这样用
this.year=year;
this.month=month;
this.day=day;
System.out.println("猫的生日是"+year+""+this.month+""+this.day);
}
}
public class study {
public static void main(String[] args) {
Cat cat2=new Cat();
}
}
这是错误的 当this(2023,1,1);
调用public Cat(int year,int month,int day)
的时候 this();
又想调用 public Cat()
形成闭环会导致编译错误
- 绝大多数情况下使用public来修饰,特殊场景下会被private修饰
就地初始化
在声明成员变量时,就直接给出了初始值
class Cat{
int year=2022;
int month=1;
int day=1;
public void func(){
System.out.println("ok");
this.get(2023,1,1);
}
public void get(int year,int month,int day){
this.year=year;
this.month=month;
this.day=day;
System.out.println("猫的生日是"+year+""+this.month+""+this.day);
}
}
public class study {
public static void main(String[] args) {
Cat cat1 =new Cat();
cat1.func();
}
}
🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱
今天是2023年的第一天。祝大家新年快乐。
我今天本来想把类和对象都搞定的😔才整了一半,这篇也是今年我的第一篇博客,我在此重新立下flag 这一年要好好学习,在新的一年我们一起努力吧。