SC_Mutable


title: 软件构造笔记Mutable
categories: 软件构造复习
tags: 软件构造笔记

Classifying Types and Operations

Mutable and immutable types

Types, whether built-in or user-defined, can be classified as mutable or immutable

可变类型的对象:提供了可改变其内部数据的值的操作

The objects of a mutable type can be changed: that is, they provide operations which when executed cause the results of other operations on the same object to give different results.

举例:

  1. Date是mutable,能够调用setMonth()函数的并且观察到getMonth()操作的变化。
  2. String是immutable的,其操作不改变内部值,而是构造新的对象
  3. StringBuilder为可变的。

Classifying the operations of an abstract type

Creators create new objects of the type.

A creator may take an object as an argument, but not an object of the type being constructed


Producers create new objects

The concat() method of String , for example, is a producer: it takes two strings and produces a new one representing their concatenation.


Observers take objects of the abstract type and return objects of a different type.

The size() method of List , for example, returns an int


Mutators change objects.

The add() method of List , for example, mutates a list by adding an element to the end.

"真是大国重器"呐!

  • 构造器例子

在SC的lab2中构造Vertex。

    // TODO constructor
    /**
     *@param (String)label - represents the vertex
     *@param (Map) toVertex - represent the arc between vertex and weight
     */
    public Vertex(String source,String target, int weight) {
    	label = source;
        toVertex = new HashMap<String, Integer>();
    	toVertex.put(target, weight);
    }
    public Vertex(String name) {
    	this.label = name;
    	this.toVertex = new HashMap<String, Integer>();
    }

构造器里面要用this而不能Vertex.label = name;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值