一.类和对象
java当中一切皆对象
Java是一门纯面向对象的语言(Object Oriented Program,简称OOP),在面向对象的世界里,一切皆为对象。面
向对象是解决问题的一种思想,主要依靠对象之间的交互完成一件事情。用面向对象的思想来涉及程序,更符合人
们对事物的认知,对于大型程序的设计、扩展以及维护都非常友好
1.类的定义格式
类是用来对一个实体(对象)来进行描述的,主要描述该实体(对象)具有哪些属性(外观尺寸等),哪些功能(用来干
啥),描述完成后计算机就可以识别了。
在java中定义时需要用到class关键字;具体语法如下:
//创建类
classs ClassName {
field; //字段(属性) 或者 成员变量
mathod; //行为 或者 成员方法
}
class为定义类的关键字,ClassName为类的名字,{}中为类的主体。
类中包含的内容为类的成员。属性主要是用来描述类的,称之为成员属性或者类成员变量,方法主要说明类具有哪些功能,称为类的成员方法
class Person {
public String name;
public int age;
public void eat() {
System.out.println("吃饭!")
}
public void sleep() {
System.out.println("睡觉!")
}
}
class WashMachine{
public String brand; // 品牌
public String type; // 型号
public double weight; // 重量
public double length; // 长
public double width; // 宽
public double height; // 高
public String color; // 颜色
public void washClothes(){ // 洗衣服
System.out.println("洗衣功能");
}
public void dryClothes(){ // 脱水
System.out.println("脱水功能");
}
public void setTime(){ // 定时
System.out.println("定时功能");
}
}
注意事项:
- 类名注意采用大驼峰定义
- 成员前写法统一为public ,
- 此处写的方法不带static关键字
说明:
1. 一般一个文件当中只定义一个类
2.main方法所在的类一般要使用public修饰(注意:Eclipse默认会在public修饰的类中找main方法)
3.public修饰的类必须要和文件名相同
4.不要轻易去修改public修饰的类的名称
2.类的实例化
定义一个类,就相当于在计算机中定义了一种新的类型
类的实例化:由类变成对象的过程
在java中采用new关键字,配合类名来实例化对象。
public static void main(String[] args) {
//实例化一个Phone对象
Phone phone = new Phone();
//实例化一个Person对象
Person person = new person();
}
一个类可以通过关键字new 实例化多个对象
public static main(String[] args) {
Person person1 = new Person();
person1.age = 10;
person1.name = "张三";
System.out.println(person1.name + " " + person1.age);
Person person2 = new Person();
person2.age = 14;
person2.name = "李四";
System.out.println(person2.name + " " + person2.age);
}
//也可以定义数组
public static main(String[] args) {
Person[] person = new Person[];
}
注意:
- new关键字用于创建一个对象的实例
- 使用 . 来访问对象中的属性和方法
- 同一个类可以创建多个实例
3.类和对象的说明
- 类只是一个模型,用来对一个实体进行描述,限定了类有哪些成员
- 类是一种自定义的类型,可以用来定义变量
- 一个类可以实例化出多个对象,实例化出的对象占用实际的物理空间,存储类成员变量
class Person {
public String name;
public int age;
}
public static void main(String[] args) {
Person person1 = new Person();
Person person2 = new Person();
person1 = person2;//不能叫引用指向引用,这个代表person1这个引用指向了person2这个引用指向的对象
}
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-4R6elKct-1668941016088)(C:\Users\lenovo\AppData\Roaming\Typora\typora-user-images\1668392669277.png)]
引用指向对象
此时name 和age 并没有赋值,但是此时不会报错
因为他们属于成员变量,所以这里编译器会给他们默认猴子值。
如果是引用类型,那么默认为NULL ;int float...对应的他们的0值
boolean 默认值 false
char 默认值 '\u0000'
- 当一个引用赋值为null的时候,代表这个引用不指向任何对象
- 一个引用不能指向多个对象
4.this引用
public class DataUtil {
public int year;
public int month;
public int day;
public void setData(int year,int month,int day) {
this.year = year;
this.month = month;
this.day = day;
}
public void show() {
System.out.println("年:" +year +"月:" + month + "日:" + day);
}
public static void main(String[] args) {
DataUtil dataUtil = new DataUtil();
dataUtil.setData(2022,11,7);
dataUtil.show();
}
}
注意:
- 要习惯加this
- this 的类型:对应类型引用,即哪个对象调用就是哪个对象的引用类型
- this 只能在“成员方法”中使用
- 在“成员方法”中,this只能引用当前对象,不能再引用其他对象
4.1 this还有另外两种用法
1.this访问构造方法
2.this访问成员方法
5.构造方法
5.1概念
改造方法(也称为构造器)是一个特殊的成员方法,名字必须与类名相同,在创建对象时,由编译器自动调用,并且在整个对象的生命周期内只调用一次
- 名字与类名相同,没有返回值类型,设置为void 也不行
- 一般情况下使用Public修饰
- 在创建对象时由编译器自动调用,并且在对象的声明周期内只调用一次
public class Data {
public int year;
public int month;
public int day;
public Data(int year,int month,int day){
this.year = year;
this.month = month;
this.day = day;
System.out.println("Data(int,int,int)方法被调用了");
}
public void printData(){
System.out.println(year + "-" + month + "-" + day)l
}
public static void main(String[] args) {
//此处调用了一个Data类型的对象,并没有显示调用构造方法
Data d = new Data(2021,6,9)
}
}
5.2构造方法可以重载
重载:方法名必须相同,参数列表不同 返回值没有影响 不加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();
}
}
5.3默认的构造方法
如果用户没有显式定义,编译器会生成一份默认的构造方法,生成的默认构造方法一定是无参的。
//如果没有写任何的构造方法,此时java会帮我们提供一个默认的,不带参数的构造方法
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类中,没有定义任何构造方法,编译器会默认生成一个不带参数的构造方法。
//如果用户已定义,编译器则不再生成
5.4构造方法中,用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()必须是构造参数的第一条语句
- 不能形成环(会报错)
绝大多数情况下使用public来修饰,特殊场景下会被private修饰
this.data:访问当前对象的成员变量
this.func:访问当前对象的成员方法
this() :调用当前对象的其他构造方法
this:不能调用静态的成员方法和属性