• 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)
  • 相关阅读:
    【Java 小知识】Request、Servlet
    【无标题】
    力扣刷题:正则表达式匹配、
    SqlServer函数,存储过程的创建和使用
    Spring原理:PostProcessor与AOP原理
    java-php-net-python-校园后勤计算机毕业设计程序
    「Python条件结构」嵌套if:判断三角形及三角形的类型
    Fluorescein-PEG-DSPE 磷脂-聚乙二醇-荧光素荧光磷脂PEG衍生物
    使用 AgileConfig 动态配置 NLog
    java基于springboot的插画漫画约稿网站 vue
  • 原文地址:https://blog.csdn.net/kking_edc/article/details/124967681