• 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)
  • 相关阅读:
    达梦数据库备份策略
    [lesson60]数组类模板
    目前放疗中可用的一些开源软件
    DML添加数据-删除数据-修改数据
    插图精美的html & css教程
    第一章:为什么要并行计算
    天地图WMTS地图瓦片下载
    Ubuntu搭建FTP服务
    Python02--python中的缩进,注释和模块
    玻色量子CEO文凯博士出席GTIC 2022全球AI芯片峰会,解读光量子计算新进展
  • 原文地址:https://blog.csdn.net/kking_edc/article/details/124967681