zoukankan      html  css  js  c++  java
  • 面向对象的核心特征之:封装、继承、多态

    1.封装 Encapsulation

    封装Encapsulation的定义:
    is the process of hiding the implementation details of an object 隐藏了一个对象的实现细节
    The internal state is usually not accessible by other objects 内部的状态也不为其他对象所访问
    The only access to manipulate the object data is through its interface 对象的数据只能通过接口去访问
    Encapsulation allows objects to be viewed as black boxes’ 封装使得对象可以被看成一个“黑盒子”
    It protects an objects internal state from being corrupted by other objects.  保护数据
    Also, other objects are protected from changes in the object implementation. 一个对象实现方法的改变,不影响其他相关对象
    Communication is achieved through an interface’  对象间通过“接口”进行通信

       要封装什么?
       内部的、不想让其他人随意了解的信息
       可以封装类的属性,如,“人” 这个类,封装个人的工资、资产、年龄等信息
       可以封装类的方法,如, “人”如何赚钱()?如何消磨时间()?
       我们为什么要封装
       保护隐私
       保护数据安全
       隔离复杂度 (内部实现细节不对外公开)。如“空调”,封装了制冷的过程,对人提供了一个制冷的按钮
       面向对象的封装有四种方式(可见性)
       Public
       Private
       Protected
       Package  

       规则:对象应该只显示与之交互所需的接口。与对象的使用无关的详细信息应该对其他对象隐藏。这是封装的一般规则

       建议:对外提供Getters和Setters支持数据隐藏的概念因为其他对象不应该直接操作另一个对象中的数据。例如,如果用户需要访问某个属性,则调用方法返回该属性(getter)  

    2.继承 Inheritance

     什么是继承

     定义:有两个类:A类与B类,当两个类之间产生父子关系的时候,我们称为继承。
        继承是一种新建类的方式,继承的类称之为子类或派生类。
        被继承的类称之为父类或基类或超类。
        子类继承父类,也就意味着子类继承了父类所有的属性和方法,可以直接调用。
        java使用extends关键实现两个类的继承关系
      语法:
      public class FatherClass{
      //属性
      //方法
      }
      public class ChildClass extends FatherClass{
      //属性
      //方法
      }
     为什么使用继承:

     提高代码的复用性,使类与类之间产生了继承的关系,也是多态的前提

    3.多态 Polymorphism

    定义:

    本意:有多种形态 “Having many forms”
    “When one class inherits from another, then polymorphism allows a subclass to stand in for the superclass.” 当一个类从另一个类继承而来,多态使得子类可以代替父类

    The sender of a stimulus doesn’t need to know the receiver’s class消息发送方不需要知道消息接收方属于那个子类

    Different receivers can interpret the message in their own way同一类族的接收者可以按自己的方式处理消息

    推论Consequence
    different receivers can response to the same stimulus based on their interpretation同一类族的接收者可以按自己的方式处理同一个消息
    So we can have a variety of objects who process the same piece of data in different ways.有多种对象可以按自己的方式处理相同的数据

    举例:

    如下图,Jet继承AirPlane,然后AirPlane继承Plane

        多态的三个条件:

    1) 继承的存在(继承是多态的基础,没有继承就没有多态).

    2) 子类重写父类的方法(多态下调用子类重写的方法).

    3) 父类引用变量指向子类对象(子类到父类的类型转换).

        子类转换成父类时的规则:

          将一个父类的引用指向一个子类的对象,称为向上转型(upcastiog),自动进行类型转换.

          此时通过父类引用调用的方法是子类覆盖或继承父类的方法,不是父类的方法.

          此时通过父类引用变量无法调用子类特有的方法.

    如果父类要调用子类的特有方法就得将一个指向子类对象的父类引用赋给一个子类的引用,称为向下转型,此时必须进行强制类型转换.

     多态是由于继承和重写机制,相同类型的对象,执行相同的方法,得到的结果可能不一样。 
    例如:我再给以上例子的圆类中加一些代码:
    public class Circle extends Shape{

       //特有的半径属性
       public float R;

       //获得半径的方法
       public void GetR(float r){
       R=r;
       }

       //重写周长和面积的方法
        public void Length(){
        float a = (float) ((float)2*3.14*R);;
        System.out.println("周长是"+a);
        }
        public void Area(){
        float b = (float)3.14*R*R;
        System.out.println("面积是"+b);
        }

    }

    测试代码:
    public static void main(String[] args) { 
       Circle cir = new Circle();
       cir.GetR(3);
       cir.Area();
       cir.Length();   
    }

    测试结果并不是Shape中的方法执行的结果,由于方法相同,但是继承之后子类重写了方法,方法体不同,导致了最后结果不一样,这就是多态。

  • 相关阅读:
    sklearn中禁止输出ConvergenceWarning:
    sklearn里训练集和测试集的分割
    sklearn模型的保存与加载使用
    django项目成功启动,但views里代码未执行
    使用sklearn对iris数据集做基本的训练和预测
    unrecognized option '--high-entropy-va'
    快速下载Mingw(不使用sourceforge.net)
    cc1.exe: sorry, unimplemented: 64-bit mode not compiled in
    GoLand里Go Module模式下import自定义包
    GoLand生成可执行文件(Windows、Linux)
  • 原文地址:https://www.cnblogs.com/cdlyy/p/12354738.html
Copyright © 2011-2022 走看看