/*** Universal way for the project to display a short* message to a user.* @parammessage The text that should be shown to* the user* @paramlength How long to display the message.*/funContext.showMessage( message: String, length: MessageLength= MessageLength.LONG) {val toastLength =when(length) { SHORT -> Toast.LENGTH_SHORT LONG -> Toast.LENGTH_LONG } Toast.makeText(this, message, toastLength).show()}enumclassMessageLength { SHORT, LONG }
类型层次结构是关于对象的一个重要信息源。接口不仅仅是我们承诺要实现的方法列表,类和接口也可以有一些期望。如果一个类承诺了一个期望,它的所有子类也应该保持这一点。这个原则被称为里氏替换原则,是面向对象编程中最重要的规则之一。它通常被解释为:“如果 S 是 T 的子类,那么类型 T 的对象可以被替换为类型 S 的对象,而不改变程序的任何属性。” 为什么它很重要的一个简单解释是:每个类都可以用作超类,因此如果它的行为不像我们所期望的那样,我们可能会遇到意料之外的异常。在编程中,子类应该遵守父类的约定。
当我们定义一个元素,特别是外部 API 的部分时,我们应该定义一个合约,我们通过名称、文档、注释和类型来达到目标。合约指定了对这些元素的期望,它还可以描述应该如何使用这些东西。合约让用户对元素现在和将来的行为建立了使用的信心,它还让创建者可以更自由的更改合约中没有指定的内容。合约是一种协议,只要双方都遵守它,它就有效。
/**
* Powerset returns a set of all subsets of the receiver
* including itself and the empty set
*/
fun <T> Collection<T>.powerset(): Set<Set<T>> =
if (isEmpty()) setOf(emptySet())
else take(size - 1)
.powerset()
.let { it + it.map { it + last() } }
/**
* Powerset returns a set of all subsets of the receiver
* including itself and empty set
*/
fun <T> Collection<T>.powerset(): Set<Set<T>> =
powerset(this, setOf(setOf()))
private tailrec fun <T> powerset(
left: Collection<T>,
acc: Set<Set<T>>
): Set<Set<T>> = when {
left.isEmpty() -> acc
else -> {
val head = left.first()
val tail = left.drop(1)
powerset(tail, acc + acc.map { it + head })
}
}
fun List<Int>.product() = fold(1) { acc, i -> acc * i }
// 让一个List中所有的数相乘
fun List<Int>.product() = fold(1) { acc, i -> acc * i }
fun update() {
// 更新 users
for (user in users) {
user.update()
}
// 更新 books
for (book in books) {
updateBook(book)
}
}
fun update() {
updateUsers()
updateBooks()
}
private fun updateBooks() {
for (book in books) {
updateBook(book)
}
}
private fun updateUsers() {
for (user in users) {
user.update()
}
}
/**
* Returns a new read-only list of given elements.
* The returned list is serializable (JVM).
* @sample samples.collections.Collections.Lists.
readOnlyList
*/
public fun <T> listOf(vararg elements: T): List<T> =
if (elements.size > 0) elements.asList()
else emptyList()
/**
* This is an example descriptions linking to [element1],
* [com.package.SomeClass.element2] and
* [this element with custom description][element3]
*/
/**
* Immutable tree data structure.
*
* Class represents immutable tree having from 1 to
* infinitive number of elements. In the tree we hold
* elements on each node and nodes can have left and
* right subtrees...
*
* @param T the type of elements this tree holds.
* @property value the value kept in this node of the tree.
* @property left the left subtree.
* @property right the right subtree.
*/
class Tree<T>(
val value: T,
val left: Tree<T>? = null,
val right: Tree<T>? = null
) {
/**
* Creates a new tree based on the current but with
* [element] added.
* @return newly created tree with additional element.
*/
operator fun plus(element: T): Tree { ... }
}
interface Car {
fun setWheelPosition(angle: Float)
fun setBreakPedal(pressure: Double)
fun setGasPedal(pressure: Double)
}
class GasolineCar: Car {
// ...
}
class GasCar: Car {
// ...
}
class ElectricCar: Car {
// ...
}
interface Car {
/**
* Changes car direction.
*
* @param angle Represents position of wheels in
* radians relatively to car axis. 0 means driving
* straight, pi/2 means driving maximally right,
* -pi/2 maximally left.
* Value needs to be in (-pi/2, pi/2)
*/
fun setWheelPosition(angle: Float)
/**
* Decelerates vehicle speed until 0.
*
* @param pressure The percentage of brake pedal use.
* Number from 0 to 1 where 0 means not using break
* at all, and 1 means maximal pedal pedal use.
*/
fun setBreakPedal(pressure: Double)
/**
* Accelerates vehicle speed until max speed possible
* for user.
*
* @param pressure The percentage of gas pedal use.
* Number from 0 to 1 where 0 means not using gas at
* all, and 1 means maximal gas pedal use.
*/
fun setGasPedal(pressure: Double)
}