面向对象 - 图1

概述

java 是一门面向对象的语言,面向对象编程,实际上就是把现实世界映射到计算机模型的一直编程方法
一个个人认为比较形象的比喻就是 例如古代的印刷技术, 把一个个字先刻成模板,这个模板在Java 中称之为
而印刷出来的一个个字, 则称之为 实例
下面创建一个 Person 类

  1. package com.ehi.basic;
  2. /*
  3. * Person class
  4. *
  5. * @author xxx on 2020/6/4 08:28
  6. */
  7. public class Person {
  8. // 定义一个类的属性
  9. private String name;
  10. private int age;
  11. // 类的构造函数,定义这个类要如何构造出实例
  12. // 每个类都有构造函数,如果没有明确写出来,则系统默认一个空的构造函数
  13. public Person(String name, int age){
  14. this.name = name;
  15. this.age = age;
  16. }
  17. public void eat(){
  18. System.out.println("人会吃饭");
  19. }
  20. public static void main(String[] args){
  21. Person p = new Person("john",18);
  22. p.eat();
  23. }
  24. }

面向对象编程主要有三大特性:

  1. 封装
  2. 继承
  3. 多态

封装

概念:

类 是实例的模板, 封装 就是把类的几大部分组装起来, 通过权限控制,只暴露接口供外部调用

属性

首先看类的属性,属性在类中一般会被定义为私密,通过下面的关键字,可以制定不同的权限控制
面向对象 - 图2

构造方法

接下来是 构造方法,一个类的构造方法可以不写,也可以写一个或者多个

  1. public class ObjectTest {
  2. public static void main(String[] args) {
  3. People p = new People("xiaoming");
  4. People p2 = new People(10);
  5. People p3 = new People();
  6. }
  7. }
  8. class People{
  9. String name;
  10. int age;
  11. public People(String name){
  12. this.name=name;
  13. System.out.println("这是一个有名字的人 "+ this.name);
  14. }
  15. public People(){
  16. System.out.println("这是一个没有名字没有年纪的人");
  17. }
  18. public People(int age){
  19. this.age=age;
  20. System.out.println("这是一个有年纪的人"+this.age);
  21. }
  22. }

上面的写法表示方法重载,也就是说,名字相同,参数的不同,就称之为方法的重载,方法的重载不仅可以用在构造函数,还可以用在普通函数,一样的用法。

继承

继承一般使用 extends 来表示继承

/*
 * @Description: This is the sample for object
 * @author: johnw;
 * @Created: 4:49 PM;
 */

public class ObjectTest {
    public static void main(String[] args) {

        Student s = new Student();
    }
}

class People{
    String name;
    int age;
    public People(String name){
        this.name=name;
        System.out.println("这是一个有名字的人 "+ this.name);
    }
    public People(){
        System.out.println("这是一个没有名字没有年纪的人");
    }
    public People(int age){
        this.age=age;
        System.out.println("这是一个有年纪的人"+this.age);
    }
}

class Student extends People {}

如果你没有重写构造函数,那么默认会使用父类的构造函数
但是如果你有使用父类的构造函数,但是又想在这个基础上添加一些自己的东西,那么就可以用到 super

class Student extends People {
    public Student (){
        super();
        System.out.println("新建一个学生");
    }
}

public class ObjectTest {

    public static void main(String[] args) {
        Student s = new Student();
        /* output
        这是一个没有名字没有年纪的人
        新建一个学生
        */

    }
}

多态

多态是指,针对某个类型的方法调用,其真正执行的方法取决于运行时期实际类型的方法
这句话可以理解为,声明变量的时候,可以声明父类类型,但是实际运行的时候使用子类构造实例。
假设这么一个例子,你要你创建的实例说出自己的名字,一个是学生,一个是老师
如果要实现这样的功能,那么就需要两个函数

public static void sayName(Student s){
    s.getName();
}

还有一个给老师的

public static void sayName(Teacher t){
    t.getName();
}

但是这么写略显复杂,有了多态,我们就不需要这么复杂的写代码
见下面代码

class People{
    private String name;
    private int age;

    public People(){
        System.out.println("这是一个没有名字没有年纪的人");
    }

    public void getName(){
        System.out.println("我是人,但是可能不是学生,是老师");
    }
}

class Student extends People {
    public void getName(){
        System.out.println("我是学生");
    }
}

public class ObjectTest {
    public static void sayName(People p){
        p.getName();
    }
    public static void main(String[] args) {
        Student s = new Student();
        sayName(s); // 我是学生
        Teacher t = new Teacher();
        sayName(t);
    }
}