面向对象定义
思想
面向过程思想:步骤清晰简单,第一步做什么,第二步做什么…
面向过程适合处理一些较为简单的问题。
面向对象思想:物以类聚,分类的思维模式,思考问题首先会解决问题需要哪些分类,然后对这些分类进行单独思考。最后,才对某个分类下的细节进行面向过程的思索。
面向对象适合处理复杂的问题,适合处理需要多人协作的问题!
对于描述复杂的事物,为了从宏观上把握、从整体上合理分析,我们需要使用面向对象的思路来分析整个系统。但是,具体到微观操作,仍然需要面向过程的思路去处理。
定义
面向对象编程Object-Oriented Programming,OOP
面向对象编程的本质就是:以类的方式组织代码,以对象的组织(封装)数据
抽象理念
三大特性:封装、继承、多态
从认识论角度考虑是先有对象后有类。对象,是具体的事物。类,是抽象的,是对对象的抽象
从代码运行角度考虑是先有类后有对象。类是对象的模板
类与对象的创建
-
使用new关键字创建对象
使用new关键字创建的时候,除了分配内存空间外,还会给创建好的对象进行默认的初始化以及对类中构造器的调用
-
类中的构造器也称为构造方法,是在进行创建对象的时候必须要调用的。并且构造器有一下俩个特点:
- 必须和类的名字相同
- 必须没有返回类型,也不能写void
-
构造器有带参构造和无参构造
-
有参无参构造、get函数、set函数、toString函数
public class Student {
public String name;
public int age;
public double scoce;
public Student() {
}
public Student(String name, int age, double scoce) {
this.name = name;
this.age = age;
this.scoce = scoce;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getScoce() {
return scoce;
}
public void setScoce(double scoce) {
this.scoce = scoce;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
", scoce=" + scoce +
'}';
}
public class Demo01 {
public static void main(String[] args) {
Student student1 = new Student();
student1.setName("wxy");
student1.setAge(22);
student1.setScoce(99.1);
System.out.println(student1);
Student student2 = new Student("xwr", 21, 99.5);
System.out.println(student2);
}
}
封装
高内聚、低耦合
- 高内聚:类内部数据操作细节由类内部完成,不允许外部干涉
- 低耦合:仅暴露少量的方法给外部使用。
属性私有-get set
- 对属性进行私有–private
- 对属性进行set和get方法
- 对属性进行封装,提高程序安全性,保护数据
- 隐藏代码的实现细节
- 统一接口
- 系统可维护性提高
继承
使用
- 使用关键字extends
- Java中类只有单继承,没有多继承!
- 继承是类和类之间的一种关系,除此之外,类和类之间的关系还有依赖、组合、聚合等
- 继承关系的俩个类,一个是子类,一个是父类。子类继承父类,使用关键字extends来表示
- 子类和父类之间,从意义上讲应该具有“is a”的关系
- 所有类都继承于Object类-native,默认继承
super
-
子类可用super关键字调用父类方法和属性
-
私有属性和方法无法调用
-
当调用子类构造方法时,父类构造方法也会被默认调用,且父类构造先于子类构造
-
super(),调用父类的构造方法必须在子类的第一行
-
super只能出现在子类的方法或构造中
-
super和this不能同时调用构造方法
this:本身调用者对象,没有继承也可以使用
super:代表父类对象,只能在继承条件下使用
this() 本类构造
Super() 父类构造
public Student() {
super();
System.out.println("new student");
}
方法重写
- 需要有继承关系
- 子类重写父类的方法
- 方法名必须相同,参数列表必须相同
- 修饰符:范围可以扩大 public > protected > Default > private
- 抛出异常:范围,可以被缩小,但不能扩大
public class Application {
public static void main(String[] args) {
A a = new A();
a.say();
B b = new B();
b.say();
// 父类指向子类
A aa = new B(); // 子类重写了父类的方法
aa.say();
}
}
public class A {
public void say(){
System.out.println("AAAA");
}
}
public class B extends A {
public void say(){
System.out.println("BBBB");
}
}
多态
定义
- 同一方法可以根据发送对象的不同而采用多种不同的行为方式
- 动态编译,可扩展性更强
- 对象能执行哪些方法,主要看对象左边的类型,和右边关系不大
- 多态是方法的多态,属性没有多态
- 必须有继承关系、方法需要重写、父类引用指向子类对象前提条件
- Father father = new Son();
- static 方法,属于类,不属于实例,无多态
- final是常量方法,无多态
- private为私有方法,无多态
public class Application {
public static void main(String[] args) {
// Student 能调用的方法都是自己的或者继承父类的
Student student = new Student();
// 父类对象,但父类的引用指向子类,但是不能调用子类的方法
Person person = new Student();
Object obj = new Student();
student.say();
student.run();
person.run();
}
}
public class Person {
public void run () {
System.out.println("run");
}
}
public class Student extends Person {
public void say() {
System.out.println("say");
}
public void run() {
System.out.println("run student");
}
}
引用类型转换和instanceof
- instanceof–判断数据类型之间的关系
- X instanceof Y 能不能编译通过,说明有父子关系
- 把子类转换为父类,向上转换
- 把父类转换为子类,向下转换,强制转换
- 方便方法调用,减少重复代码
Object object = new Student();
System.out.println(object instanceof Object);
System.out.println(object instanceof Teacher);
System.out.println(object instanceof Student);
System.out.println(object instanceof Person);
System.out.println("--------------------------");
Person person1 = new Student();
System.out.println(person1 instanceof Object);
System.out.println(person1 instanceof Teacher);
System.out.println(person1 instanceof Student);
System.out.println(person1 instanceof Person);
System.out.println("--------------------------");
Student student1 = new Student();
System.out.println(student1 instanceof Object);
System.out.println(student1 instanceof Student);
System.out.println(student1 instanceof Person);
System.out.println("--------------------------");
// 引用类型转换 父类 > 子类
Person person2 = new Student();
// 将person2转换为Student类型,我们就可以使用Student类型的方法了!
Student s = (Student)person2;
s.say();
// 子类 --> 父类 可能丢失自己本身的一些方法
Student s1 = new Student();
Person p = s1;
p.run();
扩展
public class App {
{
System.out.println("匿名代码块");
}
static {
System.out.println("静态代码块");
}
public App() {
System.out.println("构造");
}
public static void main(String[] args) {
App app = new App();
}
}
抽象类
- abstract
public abstract class Action {
// abstract,抽象方法,只有方法名字,没有方法实现
public abstract void doSomething();
// 1. 不能new这个抽象类,只能靠子类去实现它;约束!
// 2. 抽象类中可以写普通方法
// 3. 抽象方法必须在抽象类中
}
public class Application {
public static void main(String[] args) {
Action action = new A();
action.doSomething();
}
}
public class A extends Action {
@Override
public void doSomething() {
System.out.println("do");
}
}
接口
- 接口可以进行多实现
- 定义方法,让其他类实现
- public abstract
- public static final()
- 接口不能被实例,接口中没有构造方法
- implements可以实现多个接口
- 必须要重写接口中的方法
public interface A {
public void doSomething();
}
public interface B {
public void doSome();
}
public class Application implements A,B {
@Override
public void doSomething() {
System.out.println("do");
}
public static void main(String[] args) {
Application application = new Application();
application.doSomething();
application.doSome();
}
@Override
public void doSome() {
System.out.println("do B");
}
}