1、初识面向对象
-
面向过程:
-
步骤清晰简单
-
适合处理一些较为简单的问题
-
-
面向对象:
-
分类的思维模式,思考首先解决问题需要哪些分类,然后对这些分类进行单独思考。最后,才对某个分类下的细节进行面向过程的思考
-
适合处理复杂的、需要多人协作的问题
-
-
对于描述复杂的事物,为了从宏观上把握、从整体上合理分析,我们需要使用面向对象的思路来分析整个系统。但是,具体到微观操作,仍然需要面向过程的思路去处理。
-
面向对象编程的本质就是:以类的方式组织代码,以对象的组织(封装)数据。
-
抽象
-
三大特性:封装、继承、多态
-
从认识论的角度考虑是先有对象后有类。对象,是具体的事物。类,是抽象的,是对对象的抽象
-
从代码运行角度考虑是先有类后有对象。类是对象的模板。
2、方法回顾和加深
-
方法的定义
-
修饰符
-
返回类型
-
break和return的区别(break:跳出switch,结束循环)
-
方法名:注意规范(驼峰命名法),见名知意
-
参数列表:(参数类型,参数名)...
-
异常抛出
-
package com.oop;
//Demo01 类
public class Demo01 {
//main 方法
public static void main(String[] args) {
}
/*
修饰符 返回值类型 方法名(...){
//方法体
return 返回值;
}
*/
public String sayHello(){
return "hello,world";
}
public int max(int a,int b){
return a>b?a:b; //三元运算符
}
}
-
方法的调用
-
静态方法
-
非静态方法
package com.oop.Demo01; public class Demo02 { public static void main(String[] args) { //实例化这个类 //对象类型 对象名=对象值; Student student = new Student(); student.say(); } //和类一起加载的 public static void a(){ // b(); } //类实例化 之后才存在 public void b(){ } }
-
形参和实参
package com.oop.Demo01; public class Demo03 { public static void main(String[] args) { //实际参数和形式参数的类型要对应! int add=Demo03.add(1,2); System.out.println(add); } public static int add(int a,int b){ return a+b; } }
-
值传递和引用传递
package com.oop.Demo01; //值传递 public class Demo04 { public static void main(String[] args) { int a=1; System.out.println(a); //1 Demo04.change(a); System.out.println(a); //1 } //返回值为空 public static void change(int a){ a=10; } }
package com.oop.Demo01; //引用传递:对象,本质还是值传递 public class Demo05 { public static void main(String[] args) { Person person = new Person(); System.out.println(person.name); //null Demo05.change(person); System.out.println(person.name); //null } public static void change(Person person){ //person是一个对象,指向的---》 Person person = new Person();这是一个具体的人,可以改变属性! person.name="wff"; } } //定义了一个Person类,有一个属性:name class Person{ String name; }
-
this关键字
-
3、对象的创建分析
-
类是一种抽象的数据类型,它是对某一类事物整体描述/定义,但是并不能代表某一个具体的事物
-
对象是抽象概念的具体实例
创建和初始化对象
-
使用new关键字创建对象
主测试:
package com.oop.Demo02;
//一个项目应该只存在一个main方法
public class Application {
public static void main(String[] args) {
//类:抽闲的,实例化
//类实例化后会返回一个自己的对象
//student对象就是一个Student类的具体实例
Student xming = new Student();
Student xh = new Student();
xming.name="小麦";
xming.age=5;
System.out.println(xming.name);
System.out.println(xming.age);
xh.name="小红";
xh.age=5;
System.out.println(xh.name);
System.out.println(xh.age);
}
}
package com.oop.Demo02;
//学生类
public class Student {
//属性:字段
String name; //null
int age; //0
//方法
public void study(){
System.out.println(this.age+"在学校");
}
}
-
使用new关键字创建的时候,除了分配内存空间之外,还会给创建好的对象进行默认的初始化以及对类中构造器的使用
-
类中的构造器也称为构造方法,是在进行创建对象的时候必须要调用的,构造器的特点:
-
必须和类的名字相同
-
必须没有返回类型,也不能写void
-
package com.oop.Demo02;
//一个项目应该只存在一个main方法
public class Application {
public static void main(String[] args) {
//new 实例化了一个对象
Person person = new Person("wff");
System.out.println(person.name);
}
}
package com.oop.Demo02;
public class Person {
//一个类就算什么都不写,也会存在一个方法
//显示的定义构造器
String name;
//1、使用new关键字,本质是在调用构造器
//2、用来初始化值
public Person(){
}
//有参构造:一旦定义了有参构造,无参就必须显示定义
public Person(String name){
this.name=name;
}
//快捷键:alt+insert 创建构造器
}
/*
public static void main(String[] args) {
//new 实例化了一个对象
Person person = new Person("wff");
System.out.println(person.name); //
}
构造器:
1、和类名相同
2、没有返回值
作用:
1、new 本质在调用构造方法
2、初始化对象的值
注意点:
1、定义有参构造之后,如果想使用无参构造,显示的
定义一个无参的构造
*/
创建对象内存分析
package com.oop.Demo02;
import com.oop.Demo03.Pet;
public class Application {
public static void main(String[] args) {
/*
1、类与对象
类是一个模板:抽象,对象是一个具体的实例
2、方法
定义、调用
3、对应的引用
引用类型: 基本类型
对象是通过引用来操作的:栈--->堆
4、属性:字段Field 成员变量
默认初始化:
数字: 0 0.0
char: u0000
boolean: false
引用: null
修饰符 属性类型 属性名 = 属性值!
5、对象的创建和使用
- 必须使用new 关键字来创建对象,构造器 Person wff = new Person();
- 对象的属性 wff.name
- 对象的方法 wff.read()
6、类:
静态的行为 属性
动态的行为 方法
*/
}
}
4、面向对象三大特性
封装(数据的隐藏)
-
高内聚,低耦合。类的内部数据操作细节自己完成,不允许外部干涉;仅暴露少量的方法给外部使用。
-
通常,禁止直接访问一个对象中数据的实际表示,而应通过操作接口来访问,称为信息隐藏。
-
属性私有,get/set。
package com.oop;
import com.oop.Demo04.Student;
/*
1、提高程序的安全性,保护数据
2、隐藏代码的实现细节
3、统一接口
4、系统可维护性增加了
*/
public class Application {
public static void main(String[] args) {
Student s1 = new Student();
s1.setName("wff");
System.out.println(s1.getName());
s1.setAge(222); //不合法
System.out.println(s1.getAge());
}
}
package com.oop.Demo04;
//类 private:私有
public class Student {
//属性私有
private String name; //名字
private int id; //学号
private char sex; //性别
private int age; //性别
//提供一些可以操作这个属性的方法
//提供一些public的get和set方法
//alt+insert 可以自动生成get、set方法
//get 获得这个数据
public String getName(){
return this.name;
}
//set 设置这数据的值
public void setName(String name){
this.name=name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public char getSex() {
return sex;
}
public void setSex(char sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
if(age>120||age<0) { //不合法
this.age=3;
}else{
this.age = age;
}
}
}
继承
-
继承的本质是对某一批类的抽象,从而实现对现实世界更好的建模。
-
JAVA中类只有单继承,没有多继承!!!
-
继承是类和类之间的一种关系,此外,类与类之间的关系还有依赖、组合、聚合等
-
继承关系的两个类,一个为子类(派生类),一个为父类(基类)。子类继承父类,使用关键字extends来表示。
Person类:
package com.oop.Demo05;
//父类
public class Person {
//public
//protected
//default
//private
//在Java中,所有类都默认直接或者间接继承Object
private int money=1_0000;
public void say(){
System.out.println("ffffffff");
}
public int getMoney() {
return money;
}
public void setMoney(int money) {
this.money = money;
}
}
Student类:
package com.oop.Demo05;
//子类继承父类,会拥有父类的全部方法
public class Student extends Person{
//ctrl+H 继承树
}
-
object类
-
super
Application类:
package com.oop;
import com.oop.Demo05.Student;
public class Application {
public static void main(String[] args) {
Student student = new Student();
// student.test("wmj");
// student.test1();
}
}
Person类:
package com.oop.Demo05;
//父类
public class Person {
//在Java中,所有类都默认直接或者间接继承Object
public Person(){
System.out.println("person无参执行了");
}
protected String name="wff";
//私有的东西无法被继承
public void print(){
System.out.println("person");
}
}
Student类:
package com.oop.Demo05;
//子类继承父类,会拥有父类的全部方法
public class Student extends Person{
public Student(){
//隐藏代码;默认调用了父类的无参构造
super();//调用父类的构造器,必须要在子类构造器的第一行
System.out.println("Student无参执行了");
}
private String name="wwwfff";
public void print(){
System.out.println("Student");
}
public void test1() {
print(); //Student
this.print(); //Student
super.print(); //person
}
public void test(String name){
System.out.println(name); //wmj
System.out.println(this.name); //wwwfff
System.out.println(super.name); //wff
}
}
super注意点:
1、super调用父类的构造方法,必须在构造方法的第一个
2、super必须只能出现在子类的方法或者构造方法中
3、super和this不能同时调用构造方法
对比 this :
1、代表的对象不同:
this:本身调用的这个对象
super:代表父类对象的应用
2、前提:
this:没有继承也可以使用
super:只能在继承条件才可以使用
3、构造方法:
this():本类的构造
super():父类的构造
-
方法重写
Application类:
package com.oop;
import com.oop.Demo05.A;
import com.oop.Demo05.B;
public class Application {
public static void main(String[] args) {
//静态方法会根据类名在静态方法区找到方法执行,非静态方法根据类实例在方法区找到方法执行
//静态的方法和非静态的方法区别很大
//静态方法:方法的调用只和左边定义的数据类型有关
//静态方法只能被继承,不能被重写
//非静态:重写
A a = new A();
a.test(); //A
//父类的引用指向了子类
B b=new A(); //子类重写了父类的方法
b.test(); //B
}
}
A类:
package com.oop.Demo05;
public class A extends B{
// public static void test() {
// System.out.println("A=>test();");
// }
/*
静态方法的结果A=>test();
B=>test();
*/
@Override
//非静态
public void test() {
System.out.println("A=>test();");
}
//结果A=>test();
//A=>test();
}
B类:
package com.oop.Demo05;
//重写都是方法的重写,和属性无关
public class B {
public void test(){
System.out.println("B=>test();");
}
}
重写:需要有继承关系,子类重写父类的方法!
1、方法名必须相同
2、参数列表必须相同
3、修饰符:范围可以扩大: Public > Protected > Default > private
4、抛出的异常:范围可以被缩小,但不能扩大;(子类不能大于父类)
5、声明为final和static的方法不能被重写,但是为static的能够再次被声明
重写,子类的方法和父类必须要一致,方法体不同!
为什么需要重写?
1、父类的功能,子类不一定需要,或者不一定满足!
alt+Insert--->override
多态
-
即同一方法可以根据发送对象的不同而采取多种不同的行为方式
-
一个对象的实际类型是确定的,但可以指向对象的引用的类型有很多
-
多态存在的条件:
-
有继承关系
-
子类重写父类方法
-
父类引用指向子类对象
-
-
多态是方法的多态,属性没有多态性。
Application类(测试):
package com.oop;
import com.oop.Demo06.Person;
import com.oop.Demo06.Student;
public class Application {
public static void main(String[] args) {
//一个对象的实际类型是确定的
//可以指向的引用类型就不确定了:父类的引用指向子类
//Student能调用的方法都是自己的或者继承父类的
Student s1 = new Student();
//Person 父类型,可以指向子类,但是不能调用子类独有的方法
Person s2 = new Student();
Object s3 = new Student();
//对象能执行哪些方法,主要看对象左边的类型,和右边关系不大
((Student)s2).eat();
//子类重写了父类的方法,执行子类的方法
s1.eat();
}
}
Person类:
package com.oop.Demo06;
public class Person {
public void run(){
System.out.println("rrruunn");
}
}
/*
多态注意事项:
1、多态是方法的多态,属性没有多态
2、父类和子类,有联系 类型转换异常! ClassCastException!
3、存在条件:继承关系,方法需要重写,父类引用指向子类对象! Father f1=new Son();
不能被重写的:1、static 方法,属于类,它不属于实例;
2、final常量; 3、private方法。
*/
Student类:
package com.oop.Demo06;
public class Student extends Person {
@Override
public void run() {
System.out.println("son");
}
public void eat(){
System.out.println("chi");
}
}
-
instanceof (类型转换)引用类型,判断一个对象是什么类型
Application类:
package com.oop;
import com.oop.Demo06.Person;
import com.oop.Demo06.Student;
public class Application {
public static void main(String[] args) {
//类型之间的转换: 父 子
Person obj = new Student();
//student将这个对象转换为Student类型,我们就可以使用Student类型的方法了
// Student student = (Student) obj;
// student.go();
// ((Student)obj).go();
//子类转换为父类,可能会丢失自己的本来的一些方法
Student student = new Student();
student.go();
Person person=student;
}
}
/*
//Object > String
//Object > Person > Student
//Object > Person > Teacher
// System.out.println( X instanceof Y ); 能不能编译通过
//编译看左,执行看右
Object object = new Student();
System.out.println(object instanceof Student); //true
System.out.println(object instanceof Person); //true
System.out.println(object instanceof Object); //true
System.out.println(object instanceof Teacher); //False
System.out.println(object instanceof String); //False
System.out.println("========================================");
Person person = new Student();
System.out.println(person instanceof Student); //true
System.out.println(person instanceof Person); //true
System.out.println(person instanceof Object); //true
System.out.println(person instanceof Teacher); //False
// System.out.println(person instanceof String); //编译报错
Student student = new Student();
System.out.println(student instanceof Student); //true
System.out.println(student instanceof Person); //true
System.out.println(student instanceof Object); //true
// System.out.println(student instanceof Teacher); //编译报错
// System.out.println(student instanceof String); //编译报错
*/
/*
1、父类引用指向子类的对象
2、把子类转换为父类,向上转型;
3、把父类转换为子类,向下转型; 强制转换
4、方便方法的调用,减少重复的代码。
*/
Person类:
package com.oop.Demo06;
public class Person {
}
Student类:
package com.oop.Demo06;
public class Student extends Person {
public void go(){
System.out.println("go");
}
}
Teacher类:
package com.oop.Demo06;
public class Teacher extends Person {
}
static关键字详解
package com.oop.Demo07;
//static
public class Student {
private static int age; //静态变量
private double score; //非静态变量
public void run(){
}
public static void go(){
}
public static void main(String[] args) {
new Student().run();
Student.go(); //go();
// Student s1 = new Student();
// System.out.println(Student.age);
// System.out.println(s1.age);
// System.out.println(s1.score);
}
}
package com.oop.Demo07;
public class Person {
//2:赋初始值
{
System.out.println("匿名代码块");
}
//1:只执行一次
static{
System.out.println("静态代码块");
}
//3
public Person(){
System.out.println("构造方法");
}
public static void main(String[] args) {
Person person1 = new Person();
System.out.println("=================================");
Person person2 = new Person();
}
}
package com.oop.Demo07;
//静态导入包
import static java.lang.Math.random;
import static java.lang.Math.PI;
public class Test {
public static void main(String[] args) {
System.out.println(random());
System.out.println(PI);
}
}
5、抽象类和接口
抽象类
-
abstract修饰符可以用来修饰方法也可以用来修饰类,如果修饰方法,那么该方法就是抽象方法;如果修饰类,那么该类就是抽象类;
-
抽象类中可以没有抽象方法,但是有抽象方法的类一定要声明为抽象类;
-
抽象类,不能使用new关键字来创建对象,它是用来让子类继承的;
-
抽象方法,只有方法的声明,没有方法的实现,它是用来让子类实现的;
-
子类继承抽象类,那么就必须要实现抽象类没有实现的抽象方法,否则该子类也要声明为抽象类。
package com.oop.Demo08;
//abstract 抽象类 类:单继承(接口可以多继承)
public abstract class Action {
//约束~有人帮我们实现
//abstract,抽象方法,只有方法名字,没有方法的实现!
public abstract void dosomething();
//1、不能new这个抽象类,只能靠子类去实现它:约束
//2、抽象类中可以写普通的方法
//3、抽象方法必须在抽象类中
//抽象的抽象,约束
//思考?抽象类存在构造器吗?存在意义?
}
package com.oop.Demo08;
//抽象类的所有方法,继承了它的子类,都必须要实现它的方法,除非也转成抽象方法
public class A extends Action{
@Override
public void dosomething() {
}
}
接口
-
普通类:只有具体实现;抽象类:具体实现和规范(抽象方法)都有!
-
接口:只有规范,声明接口的关键字是interface(自己无法写方法),约束和实现分离:面向接口编程;
-
接口就是规范,定义的是一组规则,接口的本质是契约;
-
OO的精髓,是对对象的抽象,最能体现这一点的就是接口,设计模式所研究的,实际上就是如何合理地去抽象。
接口:
package com.oop.Demo09;
//抽象的思维!!!
//interface 定义的关键字,接口都需要有实现类
public interface UserService {
//接口中的所有定义都是抽象的 public
//常量 public static final
int age=88;
void add(String name);
void delete(String name);
void update(String name);
void query(String name);
}
package com.oop.Demo09;
public interface TimeService {
void timer();
}
实现类:
package com.oop.Demo09;
//抽象类:extends
//类 可以实现接口 implements 接口
//实现了接口中的类,就需要重写接口中的方法
//多继承,利用接口
public class UserServiceImpl implements UserService,TimeService{
@Override
public void add(String name) {
}
@Override
public void delete(String name) {
}
@Override
public void update(String name) {
}
@Override
public void query(String name) {
}
@Override
public void timer() {
}
}
接口的作用: 1、约束 2、定义一些方法,让不同的人实现 3、方法都是public abstract 4、常量都是public static final 5、接口不能被实例化,接口中没有构造方法 6、implements可以实现多个接口 7、必须要重写接口中的方法
6、内部类及OOP实战
-
内部类就是在一个类的内部再定义一个类
-
1、成员内部类
package com.oop.Demo10;
public class Outer {
private int id=10;
public void out(){
System.out.println("这是外部类的方法");
}
public class Inner{
public void in(){
System.out.println("这是内部类的方法");
}
//获得外部类的私有属性
//内部类可以直接访问外部类方法和属性,不需创建外部类的对象
public void getID(){
System.out.println(id);
}
}
}
package com.oop;
import com.oop.Demo10.Outer;
public class Application {
public static void main(String[] args) {
Outer outer = new Outer();
//通过这个外部类来实例化内部类
Outer.Inner inner = outer.new Inner();
inner.in();
inner.getID();
}
}
-
2、静态内部类
-
3、局部内部类
package com.oop.Demo10;
public class Outer {
//局部内部类
public void method(){
class Inner{
public void in(){
}
}
}
}
//一个Java类中可以有多个class类,但是只能有一个public class
//class A{
// public static void main(String[] args) {
//
// }
//}
-
4、匿名内部类
package com.oop.Demo10;
public class Test {
public static void main(String[] args) {
//没有名字初始化类,不用将实例保存到变量中
new Apple().eat();
UserService userService=new UserService() {
@Override
public void hello() {
}
};
}
}
class Apple{
public void eat(){
System.out.println("1");
}
}
interface UserService{
void hello();
}