• MIT CS143 lecture 02 Overview of COOL


    Cool Overview

    Classroom Object Oriented Language

    Designed to:

    • Be implementable in a short time
    • Give a taste of implementation of modern(Abstraction、Static typing、Reuse (inheritance)、Memory management、etc)

    But many things are left out

    一个简单的例子

    class Point {
     	x : Int ← 0;
     	y : Int ← 0;
    };
    
    • 1
    • 2
    • 3
    • 4

    Cool programs are sets of class definitions:

    • A special class Main with a special method main
    • All Cool code lives inside classes

    A class is a collection of attributes and methods

    Instances of a class are objects

    Cool 对象

    class Point {
     	x : Int ← 0;
     	y : Int; (* use default value *)
    };
    
    • 1
    • 2
    • 3
    • 4

    The expression new Point creates a new object of class Point

    An object can be thought of as a record with a slot for each attribute:

    在这里插入图片描述

    方法

    A class can also define methods for manipulating the attributes:

    class Point {
     	x : Int ← 0;
     	y : Int ← 0;
    	movePoint(newx : Int, newy : Int): Point {
     		{ 
     			x ← newx; 
     			y ← newy; 
     			self; 
     		} -- close block expression
     	}; -- close method
     }; -- close class
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    Methods can refer to the current object using self

    Information Hiding in Cool

    • Methods are global
    • Attributes are local to a class(They can only be accessed by the class’s methods)
    class Point {
     	... 
     	x () : Int { x };
     	setx (newx : Int) : Int { x ← newx }; 
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5

    Each object knows how to access the code of a method. As if the object contains a slot pointing to the code

    在这里插入图片描述

    In reality implementations save space by sharing these pointers among instances of the same class
    在这里插入图片描述

    Inheritance

    We can extend points to colored points using subclassing(class hierarchy)

    class ColorPoint inherits Point {
     	color : Int ← 0;
     	movePoint(newx : Int, newy : Int): Point {{
     		color ← 0;
     		x ← newx;
     		y ← newy; 
     		self;
     	}}; 
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    在这里插入图片描述

    Cool Types

    Every class is a type

    Base classes:

    • Int:for integers
    • Bool:for boolean values: true, false
    • String :for strings
    • Object:root of the class hierarchy

    All variables must be declared(compiler infers types for expressions)

    Cool Type Checking

    x : A;
    x ← new B;
    
    • 1
    • 2

    Is well typed if A is an ancestor of B in the class hierarchy(Anywhere an A is expected a B can be used)

    Type safety:A well-typed program cannot result in runtime type errors

    Method Invocation and Inheritance

    • Methods are invoked by dispatch
    • Understanding dispatch in the presence of inheritance is a subtle aspect of OO languages

    在这里插入图片描述

    方法调用的一个例子(调用一个单参数方法m(x)

    在这里插入图片描述

    Other Expressions

    Expression language

    every expression has a type and a value:

    • Loops:while E loop E pool
    • Conditionals:if E then E else E fi
    • Case statement:case E of x : Type ⇒ E; … esac
    • Arithmetic:+, -, …
    • Logical operations <, =, …
    • Assignment x ← E
    • Primitive I/O out_string(s), in_string(), …

    Missing features

    • arrays, floating point operations, exceptions, …

    Cool 内存管理

    • Memory is allocated every time new is invoked
    • Memory is deallocated automatically when an object is no longer reachable
    • Done by the garbage collector (GC)(There is a Cool GC)
  • 相关阅读:
    POSTGRESQL 从越来越多的ORACLE DBA 考取 PG 证书, 回顾2019- 2022
    微信哪些行为操作容易造成频繁而封号?
    ESP8266-Arduino编程实例-MQ-5液化天然气传感器驱动
    docker安装elasticsearch,elasticsearch-head
    update select
    Mempool Library
    设计模式复习
    MP3文件与文本转语音服务
    基于协作mimo系统的RM编译码误码率matlab仿真,对比硬判决译码和软判决译码
    4种实用的制作URL 文件的方法
  • 原文地址:https://blog.csdn.net/kking_edc/article/details/124967681