> 文档中心 > 面向对象编程

面向对象编程


面向对象编程

面向对象编程的本质就是:以类的方式组织代码,以对象的方式封装数据。

  • 抽象
  • 三大特征
    1. 封装
    2. 继承
    3. 多态

类与对象的创建

package oop.demo01;//学生类public class Student {    //属性:字段    String name;    int age;    //方法    public void study(){ System.out.println(this.name+"在学习");    }}

测试:

package oop.demo01;//一个项目应该只有一个main方法public class Application {    public static void main(String[] args) { //类:抽象的,实例化 //类实例化后会返回一个自己的对象! //student对象就是一个Student类的实例!//==========Student========== Student xiaoming = new Student(); Student xiaohong = new Student(); xiaoming.name = "ming"; xiaoming.age = 18; xiaohong.name = "hong"; xiaohong.age = 18; System.out.println(xiaoming.name); System.out.println(xiaoming.age); System.out.println(xiaohong.name); System.out.println(xiaohong.age); xiaoming.study();    }}

构造器

Java中的构造方法是一种特殊的方法,用于初始化对象。Java构造函数在对象创建时被调用。它构造值,即提供对象的数据。

  1. 和类名相同
  2. 没有返回值

作用:

  1. new本质在调用方法
  2. 初始化对象的值

注意点:

  • 定义有参构造之后,如果想使用无参构造,显示的定义一个无参构造
package oop.demo01;public class Person {    //一个类即使什么都没写,它也会存在一个方法    //显示的定义构造器    String name;    //实例化初始值    //1.使用new关键字,本质实在调用构造器    //2.用来初始化值    public Person(){    }    //有参构造    public Person(String name){ this.name = name;    }    //alt+insert}

测试:

package oop.demo01;//一个项目应该只有一个main方法public class Application {    public static void main(String[] args) { //==========Person========== Person person = new Person("yang"); System.out.println(person.name);    }}

类与对象

  1. 类与对象
  • 类是一个模板:抽象;

  • 对象是一个具体的实例

  1. 对象的引用

对象是通过引用来操作的:栈—>堆(地址)

  1. 属性:字段Field成员变量

    ​ 默认初始化:

    ​ 数字: 0 0.0

    ​ char: u0000

    ​ boolean: false

    ​ 引用: null

    修饰符 属性类型 属性名 = 属性值

  2. 类:

    ​ 静态的属性 属性

    ​ 动态的方法 方法

封装

(数据的隐藏)

  • 该漏的漏,该藏的藏

  • 高内聚,低耦合

  • 属性私有,get/set

意义:

  1. 提高程序安全性,保护数据
  2. 隐藏代码实现细节
  3. 统一接口
  4. 系统可维护性
public class Student {    //属性私有    private String name;    private int age;    private char sex;    //提供一些操作这些属性的方法    //提供一些public的get、set方法 //get 获得这个数据    public String getName() { return name;    }    //set 给这数据设置值    public void setName(String name) { this.name = name;    }    public int getAge() { return age;    }    public void setAge(int age) { this.age = age;    }    public char getSex() { return sex;    }    public void setSex(char sex) { this.sex = sex;    }}
import oop.demo02.Student;public class Application {    public static void main(String[] args) { Student s1 = new Student(); s1.setName("yang"); System.out.println(s1.getName()); s1.setAge(18); System.out.println(s1.getAge());    }}

继承

继承的本质是对某一批的抽象,从而实现对现实世界的更好建模

extands意思是“扩展”。子类是父类的扩展。

(在Java中,所有关系,都默认直接或间接继承object)

public class Person {    private int money = 10_0000_0000;    public void say(){ System.out.println("say");    }    public int getMoney() { return money;    }    public void setMoney(int money) { this.money = money;    }}

Java中类只有单继承,没有多继承!一个儿子只能有一个爸爸,但是一个爸爸可以有多个儿子

super

super:

  1. super调用父类的构造方法,必须在构造方法的第一个
  2. super必须只能出现在子类的方法或者构造方法中
  3. super 和 this 不能同时调用构造方法

Vs this:

​ 代表对象不同

​ this:本身调用者这个对象

​ super:代表父类对象的应用

前提

​ this:没有继承也可以使用

​ super:只能在继承条件才可以使用

构造方法

​ this():本类的构造

​ super():父类的构造

方法的重写

需要有继承关系,子类重写父类的方法

  1. 方法名必须相同

  2. 参数列表也必须相同

  3. 修饰符:范围可以扩大,但不能缩小 public>Protected>Default>private

  4. 抛出的异常:范围,可以被缩小,但不能扩大

    Exception(大)—> ClassNotFoundException

重写,子类的方法和父类必要一致:方法体不同

为什么需要重写:

  1. 父类的功能,子类不一定需要,或者不一定满足

A:

package oop.demo04;public class A extends B{    //注解:有功能的注解    @Override    public void test() { System.out.println("A=>test()");    }}

B:

package oop.demo04;//重写是方法的重写,和属性无关public class B {    public  void test() { System.out.println("B=>test()");    }}

Application:

import oop.demo03.Student;import oop.demo04.A;import oop.demo04.B;public class Application {    public static void main(String[] args) { //静态的方法和非静态的方法区别很大 //静态方法:方法的调用之和左边定义的数据类型有关 //非静态:重写 A a = new A(); a.test();  //A //父类的引用指向了子类 B b = new A(); //子类重写了父类的方法 b.test();  //B    }}

多态

注意事项:

  1. 多态是方法的多态,属性没有多态

  2. 父类和子类,有联系 类型转换异常 ClassCastException

  3. 存在条件:继承关系,方法需重写,父类的引用指向子类对象 Father f1 = new Son();

    1. static 方法,属于类,它不属于实例
    2. final 常量;
    3. private 方法;

Person

package oop.demo05;public class Person {    public void run(){ System.out.println("run");    }}

Student

package oop.demo05;public class Student extends Person{    @Override    public void run() { System.out.println("son");    }    public void eat() { System.out.println("eat");    }}

Application

package oop;import oop.demo05.Person;import oop.demo05.Student;public class Application {    public static void main(String[] args) { //可以指向的引用类型就不确定了:父类的引用指向子类 //Student 能调用的方法都是自己的或者继承父类的 Student s1 = new Student(); //Person 父类型,可以指向子类,但不能调用子类独有的方法 Person s2 = new Student(); Object s3 = new Student(); s2.run(); s1.run(); //对象能执行哪些方法,主要看对象左边的类型,和右边关系不大 ((Student) s2).eat(); s1.eat();

instanceof

instanceof 严格来说是Java中的一个双目运算符,用来测试一个对象是否出现在某个实例对象的原型链上。

(它的作用是测试它左边的对象是否是它右边的类的实例)

import oop.demo05.Person;import oop.demo05.Student;import oop.demo05.Teacher;public class Application {    public static void main(String[] args) { Object obj = new Student(); System.out.println(obj instanceof Student);//true System.out.println(obj instanceof Person);//true System.out.println(obj instanceof Object);//true System.out.println(obj instanceof Teacher);//false System.out.println(obj instanceof String);//false System.out.println("====================="); Person per = new Student(); System.out.println(per instanceof Student);//true System.out.println(per instanceof Person);//true System.out.println(per instanceof Object);//true System.out.println(per instanceof Teacher);//false //System.out.println(per instanceof String); System.out.println("====================="); Student stu = new Student(); System.out.println(stu instanceof Student);//true System.out.println(stu instanceof Person);//true System.out.println(stu instanceof Object);//true //System.out.println(stu instanceof Teacher); //System.out.println(stu instanceof String);    }}

类型转换

  1. 父类引用指向子类对象
  2. 把子类转换成父类,向上转型(不用强转)
  3. 把父类转换成子类,向下转型(强转)可能会丢失一些方法
  4. 方便方法的调用,减少重复的代码

抽象的编程思想:封装、继承、多态

抽象类

abstract , 抽象方法,只有方法名字,没有方法的实现!

  1. 不能new这个抽象类,只能靠子类去实现它;约束!
  2. 抽象类中可以写普通方法
  3. 抽象方法必须在抽象类中

//抽象的抽象:约束

接口

在这里插入图片描述

package oop.demo06;//interface  定义的关键字,接口都需要有实现类public interface UserService {    //接口中的所有定义其实都是抽象的  public  abstract    void add(String name);    void delete(String name);    void update(String name);    void query(String name);}
package oop.demo06;public interface TimeService {    void timer();}
package oop.demo06;//抽象类: 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. 必须要重写接口中的方法

内部类

  • 成员内部类
package oop.demo07;public class Outer {    private int id = 10;    public void out(){ System.out.println("wai");    }    public class Inner{ public void in(){     System.out.println("nei"); } //获得外部私有属性以及方法 public void getID(){     out();     System.out.println(id); }    }}
package oop;import oop.demo07.Outer;public class Application {    public static void main(String[] args) {Outer outer = new Outer(); //通过这个外部类来实例化内部类 Outer.Inner inner = outer.new Inner(); inner.in(); inner.getID();    }}
  • 局部内部类
package oop.demo07;public class Outer {    //局部内部类    public void method(){ class Inner{     public void in(){     } }    }    }//获得外部私有属性以及方法 public void getID(){     out();     System.out.println(id); }    }}
package oop;import oop.demo07.Outer;public class Application {    public static void main(String[] args) {Outer outer = new Outer(); //通过这个外部类来实例化内部类 Outer.Inner inner = outer.new Inner(); inner.in(); inner.getID();    }}
  • 局部内部类
package oop.demo07;public class Outer {    //局部内部类    public void method(){ class Inner{     public void in(){     } }    }    }

戒烟网