目录
1.面向对象是解决问题的一种思想,主要依靠对象之间的交互完成一件事情。
一.什么是面向对象?
1.面向对象是解决问题的一种思想,主要依靠对象之间的交互完成一件事情。
Java就是一门面向对象的语言(Object Oriented Program,简称OOP)。
2.面向对象与面向过程的区别
面向对象以对象为中心。先把要完成的功能封装成一个一个的对象,通过调用对象的方法或属性来实现功能。具有高度的拓展性和复用性。特点是继承性,封装性,多态性。但面向对象的缺点是,对于单一的功能实现,设计思路过于复杂。
面向过程是以事件的执行过程为中心,根据一个一个的步骤编写代码。优点是,对于不复杂的事件执行效率高。缺点在于复用性,拓展性不佳。
举个栗子:
传统洗衣服与现代洗衣服
传统的方式:注重洗衣服的过程,少一个环节可能都不行。按照该种方式写代码,扩展或者维护起来比较麻烦。
现代洗衣服的方式:人将衣服放进洗衣机,倒入洗衣粉,启动洗衣机,洗衣机就会完成洗衣过程并且甩干。
此过程共有四个对象:人,衣服,洗衣粉,洗衣机
整个过程主要是:人,衣服,洗衣粉,洗衣机四个对象之间交互完成的,人不需要关心洗衣机具体是如何洗衣服的。
二. 类的定义和使用
1.简单的认识类
类是用来对一个实体(对象)进行描述的。主要描述该对象具有哪些属性(外观尺寸等),哪些功能(用来干什么),描述完成计算机识别。
对象是类的实例化。
比如:洗衣机,它是一个品牌,在 Java 中可以将其看成是一个类别。属性:产品品牌,型号,产品重量,外观尺寸,颜色 ...功能(行为):洗衣,烘干、定时
2.类的定义格式
// 创建类class ClassName {field ; // 字段 ( 属性 ) 或者 成员变量method ; // 行为 或者 成员方法}
class为定义类的关键字, ClassName为类的名字, { }中为类的主体。
主体中包含成员变量和成员方法。成员变量主要描述类具有的属性,成员方法主要描述类具有哪些功能。
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 ( " 定时功能 " );}}比特就业
注意!
类名注意采用大驼峰定义 即每个单词首字母大写。
三.类的实例化
1.类和对象的说明
![](https://i-blog.csdnimg.cn/blog_migrate/05c7f0a654a094a2eee4a346aa16458c.png)
2.什么是实例化
定义了一个类,就相当于在计算机中定义了一种新的类型。与int,double类型相似,只不过int和double是java语言自带的内置类型,而类是用户新定义的类型。比如上面的WashMachine类。这就是一种新定义的一种类型,可以用它来定义实例(对象)。
用类类型创建对象的过程,称为类的实例化,在java中采用new关键字,配合类名来实例化对象。
注意!
public class Main {public static void main ( String [] args ) {PetDog dogh = new PetDog (); // 通过 new 实例化对象dogh . name = " 阿黄 " ;dogh . color = " 黑黄 " ;dogh . barks ();dogh . wag ();PetDog dogs = new PetDog ();dogs . name = " 小白 " ;dogs . color = " 白色 " ;dogs . barks ();dogs . wag ();}}
四.this引用
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) {
// 构造三个日期类型的对象 d1 d2 d3
Date d1 = new Date();
Date d2 = new Date();
Date d3 = new Date();
// 对d1,d2,d3的日期设置
d1.setDay(2020,9,15);
d2.setDay(2020,9,16);
d3.setDay(2020,9,17);
// 打印日期中的内容
d1.printDate();
d2.printDate();
d3.printDate();
}
}
但是如果写成下面这样呢?
形参名不小心与变量名相同
public void setDay ( int year , int month , int day ){year = year ;month = month ;day = day ;}
2.this出场解决问题
this引用指向当前对象(成员方法运行时调用该成员方法的对象),在成员方法中所有成员变量的操作,都是通过该引用去访问。
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;
System.out.println("Date(int,int,int)方法被调用了");
}
//打印年月日
public void printDate(){
System.out.println(year+"-"+month+"-"+day);
}
// main方法调用方法
public static void main(String[] args) {
Date d1 = new Date(2012,04,22);
Date d2 = new Date();
d1.printDate();
d2.printDate();
}
}
注意! this引用的是调用成员方法的对象
下图就是main方法中d1,d2调用Date方法初始化。当d1调用时this引用会对d1标记(如图中的Date@498),this只能引用当前对象,这样系统就可以清楚知道是对象d1调用了构造方法Date.同理Date@501就是d2对象调用。
3.this引用特点
(1)this的类型:对应类类型引用,即哪个对象调用就是哪个对象的引用类型
(2)this只能在“成员方法”中使用
(3)在“成员方法”中,this只能引用当前对象,不能再引用其他对象
五.对象的构造及初始化
1.构造方法的概念
public class Date {public int year ;public int month ;public int day ;// 构造方法:// 名字与类名相同,没有返回值类型,设置为 void 也不行// 一般情况下使用 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}}
2.构造方法的特性
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 ();}}
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 ;}}
3.默认初始化
public class Date {public int year ;public int month ;public int day ;public Date ( int year , int month , int day ) {// 成员变量在定义时,并没有给初始值 , 为什么就可以使用呢?System . out . println ( this . year );System . out . println ( this . month );System . out . println ( this . day );}public static void main ( String [] args ) {// 此处 a 没有初始化,编译时报错:// Error:(24, 28) java: 可能尚未初始化变量 a// int a;// System.out.println(a);Date d = new Date ( 2021 , 6 , 9 );}}
Date d = new Date ( 2021 , 6 , 9 );
4.就地初始化
在声明成员变量时,就直接给出了初始值。
public class Date {public int year = 1900 ;public int month = 1 ;public int day = 1 ;public Date (){}public Date ( int year , int month , int day ) {}public static void main ( String [] args ) {Date d1 = new Date ( 2021 , 6 , 9 );Date d2 = new Date ();}}
六.static成员
1.了解static
设置一个学生类,实例化三个对象,每个对象都有自己特有的名字,性别,年龄,学分绩点等成员信息。如下所示:
public class Student{
private String name;
private String gender;
private int age;
private double score;
public static void main(String[] args) {
Student s1 = new Student("Li leilei", "男", 18, 3.8);
Student s2 = new Student("Han MeiMei", "女", 19, 4.0);
Student s3 = new Student("Jim", "男", 18, 2.6);
}
}
假设三个同学是同一个班的,那么他们上课肯定在同一个教室,那能否给类中加一个成员变量,来保存同学上课时的教室呢?答案是不行的!
在Student类中定义的成员变量,每个对象中都会包含一份实例变量,因为需要使用这些信息来描述具体的学生。而上课的教室,这个属性并不需要每个学生对象中都储存一份,需要每个学生来共享。
这时,用static关键字修饰的成员变量,称为静态成员,也可以称为类成员,其不属于某个具体的对象,是所有对象所共享的。
2.static修饰成员变量
static修饰的成员变量,称为静态成员变量。
静态成员变量特性
(1)不属于某个具体的对象,是类的属性,所有对象共享的,不存储在某个对象的空间中。
(2)既可以通过对象访问,也可以通过类名访问,一般更推荐类访问
(3)生命周期伴随类的一生(随类的加载而创建,随类的卸载而销毁)
public class Student {
private String name;
private String gender;
private int age;
private double score;
private static String classRoom ;
public Student(String name, String gender, int age, double score) {
this.name = name;
this.gender = gender;
this.age = age;
this.score = score;
}
//---------//
public static void main(String[] args) {
//类名调用静态成员变量
System.out.println(Student.classRoom);
Student s1 = new Student("Li jinxiu", "女", 19, 3.8);
//对象调用静态成员变量
System.out.println(s1.classRoom);
Student student = new Student("limin","男",21,3.05);
}
}
3.static修饰成员方法
一般类中的数据成员都设置为private,而成员方法设置为public,那设置之后,Student类中classroom属性如何在类外访问呢?
在java中,被static修饰的成员方法称为静态成员方法,是类的方法,不是某个对象所特有的。
静态成员一般是通过静态方法来访问的。
public class Student{
// ...
private static String classRoom = "Bit306";
// ...
public static String getClassRoom(){
return classRoom;
}
}
public class TestStudent {
public static void main(String[] args) {
System.out.println(Student.getClassRoom());
}
}
静态方法特性
(1)不属于某个具体的对象,是类方法
(2)可以通过对象调用,也可以通过类名.静态方法名(...)调用,更推荐后者
(3) 不能在静态方法中访问任何非静态成员变量
(4)静态方法中不能调用任何非静态方法,因为非静态方法有this参数,在静态方法中调用无法传递this引用
public static String getClassRoom(){
doClass();
return classRoom;
}
// 编译报错:Error:(35, 9) java: 无法从静态上下文中引用非静态 方法 doClass()
4.static成员变量初始化
注意! 静态成员变量一般不会放在构造方法中初始化,构造方法中初始化的是与对象相关的实例属性。 静态成员变量的初始化有两种:就地初始化和静态代码块初始化。
(1)就地初始化 :在定义时直接给出初始值
(2)静态代码块初始化
使用{ }定义的一段代码称为代码块。static定义的代码块称为静态代码块,一般用于初始化静态成员变量。
public class Student{
private String name;
private String gender;
private int age;
private double score;
private static String classRoom;
//实例代码块
{
this.name = "bit";
this.age = 12;
this.gender = "man";
System.out.println("I am instance init()!");
}
// 静态代码块
static {
classRoom = "bit306";
System.out.println("I am static init()!");
}
public Student(){
System.out.println("I am Student init()!");
}
public static void main(String[] args) {
Student s1 = new Student();
Student s2 = new Student();
}
}
注意!