Java是一门纯面向对象的语言(Object Oriented Program,简称OOP),一切皆为对象。
通过类来描述对象
类的定义和使用
一个Java程序
多个类,一个public类,一个类一个.class字节码文件
class关键字定义类
// 创建类
class ClassName{
field; // 字段(属性) 或者 成员变量
method; // 行为 或者 成员方法
}
成员变量和成员方法,均分为普通成员变量(方法),静态static成员变量(方法)
class BichonFrise{
public String name;
public int age;
public String character;
public float weight;
public void dogBard(){
System.out.println(name+"(*^__^*)汪汪~~汪汪汪~~");
}
public void dogSleep(){
System.out.println(name+"(。-_-。)呼呼呼呼");
}
public void dogEat(){
System.out.println(name+"φ(≧ω≦*)♪吧唧吧唧");
}
public void dogDrink(){
System.out.println(name+"( ̄┰ ̄*)咕噜咕噜");
}
}
注意
类名大驼峰
成员前public,访问修饰限定符
static 关键字.,带上叫静态成员方法
建议一个Java一个类
类的实例化
类,相当于一种新的类型,与java语言自带的内置类型int,double类似,类自定义了一个新的类型,使用这些类来定义实例(或者称为对象)。
用类类型创建对象,称为类的实例化,采用new关键字,配合类名来实例化对象。
new 关键字用于创建一个对象的实例
使用 . 来访问对象中的属性和方法
同一个类可以创建多个实例
类的实例化要在public类中的main方法中,不能在类中main方法外
在public类中实例化类类型会导致循环依赖的问题,即两个或多个类互相依赖对方才能实例化成功,这会导致编译错误或运行时错误。
例如,如果Class A中实例化了Class B,而Class B中又实例化了Class A,那么就会形成循环依赖的问题。
public class J1009_1 {
public static void main(String[] args) {
BichonFrise dog1 = new BichonFrise();
BichonFrise dog2 = new BichonFrise();
BichonFrise dog3 = new BichonFrise();
dog1.name = "暖暖";
dog2.name = "花花";
dog3.name = "豆豆";
dog1.dogBard();
dog3.dogDrink();
dog2.dogSleep();
}
}
this引用
为什么需要this引用
1.参名与成员变量名相同:
public void setDay(int year, int month, int day){
year = year;
month = month;
day = day;
}
那函数体中到底是谁给谁赋值?成员变量给成员变量?参数给参数?参数给成员变量?成员变量参数?
2.三个对象都在调用setDate和printDate函数,但是这两个函数中没有任何有关对象的说明,setDate和printDate函数如何知道打印的是那个对象的数据呢?
this引用指向当前对象(成员方法运行时调用该成员方法的对象),在成员方法中所有成员变量的操作,都是通过该引用去访问。只不过所有的操作对用户是透明的,即用户不需要来传递,编译器自动完成。
public class Date {
public int year;
public int month;
public int day;
public void setDay(int year, int month, int day){
this.year = year;
this.month = month;
this.day = day;
}
public void printDate(){
System.out.println(this.year + "/" + this.month + "/" + this.day);
}
}
this引用的是调用成员方法的对象。
3.this还可以调用其他构造方法来简化代码
public class Date {
public int year;
public int month;
public int day;
// 无参构造方法--内部给各个成员赋值初始值,该部分功能与三个参数的构造方法重复
// 此处可以在无参构造方法中通过this调用带有三个参数的构造方法
// 但是this(1900,1,1);必须是构造方法中第一条语句
public Date(){
//System.out.println(year); 注释取消掉,编译会失败
this(1900, 1, 1);
//this.year = 1900;
//this.month = 1;
//this.day = 1;
}
// 带有三个参数的构造方法
public Date(int year, int month, int day) {
this.year = year;
this.month = month;
this.day = day;
}
}
this(...)必须是构造方法中第一条语句
不能形成环,即不能来回调用
this引用的特性
this的类型:对应类类型引用,即哪个对象调用就是哪个对象的引用类型,代表当前对象的引用
this只能在"成员方法"中使用
在"成员方法"中,this只能引用当前对象,不能再引用其他对象
this是“成员方法”第一个隐藏的参数,编译器会自动传递,在成员方法执行时,编译器会负责将调用成员方法对象的引用传递给该成员方法,this负责来接收
this.成员变量 this.成员方法
产生并初始化对象
为对象分配内存空间
调用合适的构造方法
构造方法初始化
构造方法对对象中的成员进行初始化,不负责给对象开辟空间。
public class Date {
public int year;
public int month;
public int day;
// 构造方法:
// 名字与类名相同,没有返回值类型,设置为void也不行
// 一般情况下使用public修饰,public称为访问修饰限定符
// 在创建对象时由编译器自动调用,并且在对象的生命周期内只调用一次
public Date(int year, int month, int day){
this.year = year;
this.month = month;
this.day = day;
System.out.println("Date(int,int,int)方法被调用了");
}
public void printDate(){
System.out.println(year + "-" + month + "-" + day);
}
public static void main(String[] args) {
// 此处创建了一个Date类型的对象,并没有显式调用构造方法
Date d = new Date(2021,6,9); // 输出Date(int,int,int)方法被调用了
d.printDate(); // 2021-6-9
}
}
特性
名字必须与类名相同
没有返回值类型,设置为void也不行
创建对象时由编译器自动调用,并且在对象的生命周期内只调用一次
构造方法可以重载
public class Date {
public int year;
public int month;
public int day;
// 无参构造方法
public Date(){
this.year = 1900;
this.month = 1;
this.day = 1;
}
// 带有三个参数的构造方法
public Date(int year, int month, int day) {
this.year = year;
this.month = month;
this.day = day;
}
public void printDate(){
System.out.println(year + "-" + month + "-" + day);
}
public static void main(String[] args) {
Date d = new Date();
d.printDate();
}
}
上述两个构造方法:名字相同,参数列表不同,因此构成了方法重载。
如果用户没有显式定义,编译器会生成一份默认的构造方法,生成的默认构造方法一定是无参的。
public 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 d = new Date();
d.printDate();
}
}
上述Date类中,没有定义任何构造方法,编译器会默认生成一个不带参数的构造方法。
一旦用户定义,编译器则不再生成。
救急不救穷
构造方法内部调用构造方法
构造方法中,可以通过this调用其他构造方法来简化代码
构造方法中调用构造方法都必须是构造方法中的第一条语句,且不能成环
public Date(){
this(1900,1,1);
}
public Date(int year, int month, int day) {
this();
}
/*
无参构造器调用三个参数的构造器,而三个参数构造器有调用无参的构造器,形成构造器的递归调用
编译报错:Error:(19, 12) java: 递归构造器调用
*/
绝大多数情况下使用public来修饰,特殊场景下会被private修饰
private 私有的 protected 受保护的 public 公有的
构造方法快捷键
构造方法在生命周期内只能调用一次
默认初始化
为什么局部变量在使用时必须要初始化,而成员变量可以不用呢?
new 关键字背后所发生的一些事情:
在JVM层面需要做好多事情:
1. 检测对象对应的类是否加载了,如果没有加载则加载
2. 为对象分配内存空间
3. 处理并发安全问题
比如:多个线程同时申请对象,JVM要保证给对象分配的空间不冲突
初始化所分配的空间
对象空间被申请好之后,对象中包含的成员已经设置好了初始值,比如:
就地初始化
在声明成员变量时,就直接给出了初始值。
有局限性