文章目录
1.类与对象的初步认识
1.JAVA是基于面向对象的,关注的是对象(也就是参与过程所涉及到的主体),将一件事拆分成不同的对象,靠对象之间的交互完成。
2.类是一类对象的统称,对象就是这一类具体化的一个实例,一个类包含一个事物的属性和行为。
2.类和类的实例化
** 类是一类对象的统称,对象是这一类具体化的一个实例。**
例如:做月饼的模是一个类,月饼就是对象,一个模子可以实例化出无数个对象。
**
声明一个类就是创建一个新的数据类型,类在JAVA中属于引用类型,用关键字class来声明一个类,类中的元素称为:成员属性,类中的函数称为成员方法
语法:
class 类名{
属性/成员变量
行为/成员方法
}
例:
class Person{
public int age;//成员属性
public String name;
public String sex;
public void eat(){//成员方法
System.out.println("吃饭");
}
}
注意事项:
1.一般一个文件当中只定义一个类
2.public修饰的类必须和文件名相同
3.main方法所在的类一般用public修饰
4.一个JAVA文件只能有一个public修饰的类
5.类名用大驼峰定义
** 类的实例化**
也就是用类类型创建对象的过程,一个类可以实例化出多个对象,实例化出的对象占用实际的物理空间。类实例化出对象就像用设计图造出房子,类就是设计图。
例:
class Person{
public int age;
public String name;
public String sex;
public void eat(){
System.out.println("吃饭");
}
public void sleep(){
System.out.println("睡觉");
}
}
public class TestDemo {
public static void main(String[] args) {//输入psvm
Person person=new Person();
person.eat();
person.sleep();
}
}
//输出结果:
//吃饭
//睡觉
注意事项:
1.new关键字用来创建一个对象的实例
2.使用 . 访问对象中的属性方法
2.1 属性/成员变量
也就是在类中,但是在方法外部定义的变量
例:
class Person{
public int age;
public String name;
}
public class TestDemo {
public static void main(String[] args) {//输入psvm
Person person=new Person();
System.out.println(person.name);
System.out.println(person.age);
}
}
//结果:
//null
//0
//解释:此时name和age没有赋值,但不会报错,因为他们属于成员变量,编译器会给默认值。
一个对象的属性如果没有显示的设置初始值,会被设置一个默认的初值。换句话说,成员变量可以不初始化,有默认值,局部变量不可以不初始化,会报错。
2.2 默认值规则:
1.各种数字类型,默认值为0
2.boolean类型,默认值为false
3.char的默认值为 ‘\u0000’
4.引用类型(String,Array,自定制类),默认值为null
2.3 认识null
null为“空引用”,表示不引用任何对象,对null进行 . 操作会发生异常。
例:
class Person{
public int age;
public String name;
}
public class TestDemo {
public static void main(String[] args) {//输入psvm
Person person=new Person();
System.out.println(person.name.length());//获取字符串长度
}
}
//结果:
//Exception in thread "main" java.lang.NullPointerException: Cannot ///invoke "String.length()" because "person.name" is null
//空指针异常
例:
Person person1=null;
//表示person1不指向任何对象
注意:
引用指向对象,也就是说引用存了对象的地址。
2.4 成员变量就地初始化
很多时候不希望属性有默认值,而是需要我们显式设定初始值,可以这样写:
class Person{
public int age=18;
public String name="hh";
}
public class TestDemo {
public static void main(String[] args) {//输入psvm
Person person=new Person();
System.out.println(person.name);
System.out.println(person.age);
}
}
2.5 成员方法
用于描述一个对象的行为
例:
class Person{
public int age=18;
public String name="hh";
public void show(){
System.out.println("我叫"+name+","+"今年"+age+"岁");
}
}
public class TestDemo {
public static void main(String[] args) {//输入psvm
Person person=new Person();
person.show();
Person person2=new Person();
person2.name="jj";
person2.age=10;
person2.show();
}
}
//结果:
//我叫hh,今年18岁
//我叫jj,今年10岁
此处的show方法,表示这个对象自我展示的一个行为,show方法是和person实例相关联的,如果创建了其他实例,show行为就会发生变化。
3. 构造方法
是一种特殊的方法,使用关键字new实例化新对象时会被自动调用,用于完成初始化工作。
new的执行过程:
1.为对象分配内存空间
2.调用对象的构造方法
3.1 语法规则:
1.构造方法的方法名必须与类名相同
2.构造方法没有返回值
3.每一个类中至少存在一个构造方法(如果没有明确定义,系统自动生成一个无参构造。若类中定义了构造方法,则默认的无参构造不再生成)
4.构造方法可以重载
例:
class Person{
private int age;
private String name;
private String sex;
public Person(){//默认构造函数
this.name="zuozuo";
this.age=10;
this.sex="nan";
}
public Person (String name,int age,String sex){//带有3个参数的构造方法
this.name=name;
this.age=age;
this.sex=sex;
}
public void show(){
System.out.println("name: "+name+" age: "+age+" sex: "+sex);
}
}
public class TestDemo {
public static void main(String[] args) {//输入psvm
Person person1=new Person();
person1.show();
Person person2=new Person("huahua",18,"nv");
person2.show();
}
}
//结果:
//name: zuozuo age: 10 sex: nan
//name: huahua age: 18 sex: nv
3.2 this关键字
this表示当前对象的引用,而不是当前对象,可以借助this来访问对象的属性和方法。
3.2.1 为什么要有this引用
例:
public class Date {
public int year;
public int month;
public int day;
public void setDay(int y,int m,int d){
year=y;
month=m;
day=d;
}
public void printDate(){
System.out.println(year+"/"+month+"/"+day);
}
public static void main(String[] args) {
//构造三个日期类型的对象
Date d1=new Date();
Date d2=new Date();
Date d3=new Date();
//对三个对象进行日期设置
d1.setDay(2020,9,15);
d2.setDay(2020,9,16);
d3.setDay(2020,9,17);
//打印日期内容
d1.printDate();
d2.printDate();
d3.printDate();
}
}
以上代码定义了一个日期类,main方法创建了三个对象,通过Date类中的成员方法对对象进行设置和打印,但存在一些问题:
1.形参名与成员变量名相同:
public void setDay(int year,int month,int day){
year=year;
month=month;
day=day;
}
在这个函数体中到底是谁给谁赋值?成员变量给成员变量?参数给参数?参数给成员变量?成员变量给参数?
2.如何知道打印的是哪个对象的数据
三个对象都在调用setDay和printDate函数,但在这两个函数中没有任何有关对象的说明,这两个函数怎么知道打印的是哪个对象的数据?
3.2.2 什么是this引用
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);
}
public static void main(String[] args) {
Date d1=new Date();
Date d2=new Date();
Date d3=new Date();
d1.setDay(2020,9,15);
d2.setDay(2020,9,16);
d3.setDay(2020,9,17);
d1.printDate();
d2.printDate();
d3.printDate();
}
}
3.2.3 this引用的特性
1.this的类型:哪个对象调用就是哪个对象的引用类型
2.this只能在成员方法中使用,静态方法中不可以有this
3.在成员方法中,this只能引用当前对象,不能再引用其他对象
4.this是成员方法第一个隐藏的参数,编译器会自动传递,在成员方法执行时,编译器会负责将调用成员方法对象的引用传递给该成员方法,this负责接收
5.this不能调用静态的成员属性和方法
public void setDay(Date this,int year,int month,int day){
this.year=year;
this.month=month;
this.day=day;
}
public void printDate(Date this){
System.out.println(this.year+"/"+this.month+"/"+this.day);
}
3.2.4 this的作用:
1.this.data:访问当前对象的成员变量
2.this.func():访问当前对象的成员方法
3.this():调用当前对象的其他构造方法
4.对象的构造及初始化
4.1 如何初始化对象
首先我们知道,在JAVA方法内部定义一个局部变量时,必须要初始化,否则会编译失败。
public static void main(String[] args) {
int a;
System.out.println(a);
}
//结果:java: 可能尚未初始化变量a
要让这段代码编译通过,只需在正式使用a之前,给a设置一个初始值即可。
对象空间申请好之后,对象中包含的成员已经设置好了初始值
例:
public class Date {
public int year;
public int month;
public int day;
public void setDate(int year,int month,int day){
this.year=year;
this.month=month;
this.day=day;
}
public void printDate(Date this){
System.out.println(year+"/"+month+"/"+day);
}
public static void main(String[] args) {
Date d=new Date();
d.printDate();
d.setDate(2023,9,1);
d.printDate();
}
}
//结果:0/0/0
// 2023/9/1
通过这个例子发现,成员属性声明之后没有显式设定初始值编译器会自动给定一个初始值
区分局部变量和成员变量:
1.局部变量:定义在方法内部
2.成员变量:定义在类内部,方法外部
4.1.2 就地初始化:
public class Date {
public int year=2023;
public int month=9;
public int day=2;
public Date(int year,int month,int day){
this.year=year;
this.month=month;
this.day=day;
}
public void printDate(Date this){
System.out.println(year+"/"+month+"/"+day);
}
public static void main(String[] args) {
Date d=new Date();
d.printDate();
}
}
4.2 构造方法
构造方法,也称为构造器,是一个特殊的成员方法,名字必须与类名相同,没有返回值类型,设置为void也不可以,一般用public修饰,在创建对象时,由编译器自动调用,并且在整个对象的生命周期内只调用一次。
例:
public class Date {
public int year;
public int month;
public int day;
public Date(){
System.out.println("不带参数的构造方法");
}
}
在Date类中,Date这个无参构造方法如果没有写,JAVA会默认提供(默认提供的构造方法一定是无参的)。如果自己写了,编译器就不会生成,就会调用自己写的。
例:
public class Date {
public int year;
public int month;
public int day;
public Date(int year,int month,int day){
this.year=year;
this.month=month;
this.day=day;
}
public void printDate(Date this){
System.out.println(year+"/"+month+"/"+day);
}
public static void main(String[] args) {
Date d=new Date(2023,9,1);
d.printDate();
}
}
//结果:2023/9/1
例:
public class Date {
public int year;
public int month;
public int day;
public Date(int year,int month,int day){
this.year=year;
this.month=month;
this.day=day;
}
public void printDate(Date this){
System.out.println(year+"/"+month+"/"+day);
}
public static void main(String[] args) {
Date d=new Date();
d.printDate();
}
}
//结果:
//java: 无法将类 TESTDEMO.Date中的构造器 Date应用到给定类型;
//需要: int,int,int
//找到: 没有参数
//原因: 实际参数列表和形式参数列表长度不同
例:在构造方法中可以调用其他的构造方法
public class Date {
public int year;
public int month;
public int day;
public Date(){//在无参构造方法内部通过this调用带有三个参数的构造方法
this(2023,9,2);//这条语句必须是构造方法中的第一条语句,并且只能在构造方法内部使用
}
public Date(int year,int month,int day){
this.year=year;
this.month=month;
this.day=day;
}
public void printDate(Date this){
System.out.println(year+"/"+month+"/"+day);
}
public static void main(String[] args) {
Date d=new Date();
d.printDate();
}
}
//结果:2023/9/2
注意,不能形成环:
public Date(){
this(2023,9,2);
}
public Date(int year,int month,int day){
this();
this.year=year;
this.month=month;
this.day=day;
}
//这两个构造方法循环调用了
4.2.1 当用关键字new创建一个对象时,要完成两个步骤:
1.为对象分配内存
2.调用合适的构造方法
4.2.2 构造方法的特性
1.名字必须与类名相同
2.没有返回值类型,void也不可以
3.创建对象时由编译器自动调用,并且在对象的生命周期内只调用一次
4.构造方法可以重载(用户根据自己的需求提供不同参数的构造方法),重载也就是方法的名字相同,参数列表不同,返回值没有要求
5.构造方法的作用是对对象中的成员初始化,并不负责给对象开辟内存空间