楼主: ReneeBK
759 1

[Lecture Notes]Object-Oriented Software Development [推广有奖]

  • 1关注
  • 62粉丝

VIP

已卖:4897份资源

学术权威

14%

还不是VIP/贵宾

-

TA的文库  其他...

R资源总汇

Panel Data Analysis

Experimental Design

威望
1
论坛币
49635 个
通用积分
55.6937
学术水平
370 点
热心指数
273 点
信用等级
335 点
经验
57805 点
帖子
4005
精华
21
在线时间
582 小时
注册时间
2005-5-8
最后登录
2023-11-26

楼主
ReneeBK 发表于 2016-3-31 10:35:49 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币

本帖隐藏的内容

http://homepage.cs.uiowa.edu/~tinelli/classes/022/Spring15/lectures.shtml


二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:Development Oriented software Lecture Develop Software

本帖被以下文库推荐

沙发
ReneeBK 发表于 2016-3-31 10:39:45
  1. /*
  2.    CS:2820 Object Oriented Software Development
  3.    Spring 2015
  4.    The University of Iowa
  5.    
  6.    Instructor: Cesare Tinelli
  7. */



  8. /* Subtype polymorphism and dynamic binding */

  9. class A {
  10.   def f = println("f says: ``I'm f from A''")
  11.   def g = {println("g says: ``I'm g from A, calling f''"); f}
  12. }

  13. val a = new A

  14. a.f // will call method f from class A

  15. a.g

  16. class AA extends A {
  17.   override def f = println("f says: ``I'm f from AA''")
  18.   // Does not exist in A
  19.   def h(n: Int) = n + 1
  20. }

  21. val aa = new AA

  22. aa.f  // will call method f from AA
  23. aa.g  // will call method g from A, which will then call f from _AA_


  24. val a1:A = new AA // a1 has static type A, but dynamic type AA

  25. a1.f // still calling f from AA!

  26. a1.h // a1 is considered an A, and h is not a member of A. Error.

  27. def m(a:A) = a.h

  28. def m(a:A) = a.f

  29. val aa = new AA

  30. m(aa)


  31. class AB extends A {}


  32. val ab = new AB

  33. ab.f  // calls f from A because it was not redefined in AB


  34. class AAA extends AA {
  35.   override def f = println("f says:I'm f from AAA")
  36. }


  37. val aaa: A = new AAA

  38. aaa.g // most recent overriding is the one that matters




  39. /* More on classes and inheritance in Scala */

  40. /* Example adapted from Programming in Scala, Second Edition
  41.    by Martin Odersky, Lex Spoon, and Bill Venners.
  42.    Artima, 2010.
  43. */


  44. /* We want to work with "text boxes"
  45.    A text box is an abstraction for a box of text
  46.    consisting of one or more lines of the same length
  47. */

  48. /* Abstract classes */

  49. abstract class TextBox {
  50.   // contents returns the content of the textbox in an array,
  51.   // with each element of the array containing one line
  52.   // of the box, from top to bottom
  53.   def contents(): Array[String]
  54.   // height returns the number of lines in the text box
  55.   def height(): Int
  56.   // width returns the maximum length of the lines
  57.   def width(): Int
  58. }
  59.   
  60. new TextBox // gives error, abstract classes cannot be instantiated


  61. // Scala allows parameterless methods
  62. // It also allows the definition of some methods directly
  63. // in an abstract class. These methods can use fields and
  64. // methods defined in the abstract class itself, but they
  65. // cannot use private fields and methods in concrete classes
  66. // that implement the abstract class.
  67. abstract class TextBox {
  68.   def contents: Array[String]

  69.   // height can be computed by obtaning the contents
  70.   // array and returning its length
  71.   def height: Int = contents.length

  72.   // width can be computed by finding the maximum length among the lines
  73.   def width: Int
  74. }




  75. /* Extending classes */

  76. // Array Box is concrete class implementing text boxes
  77. // by means of an array.
  78. class ArrayBox(lines: Array[String]) extends TextBox {
  79.     // since the text box is actually stored in an array
  80.     // (as expected), content is implemented simply by
  81.     // returning that array
  82.     def contents: Array[String] = lines
  83. }

  84. val a = new ArrayBox(Array("hello", "world"))

  85. a.width

  86. a.height




  87. /* Overriding methods and fields */

  88. class ArrayBox(conts: Array[String]) extends TextBox {
  89.     def contents: Array[String] = conts
  90. }

  91. // we can eliminate the need for the parameter lines above
  92. // by turning contents into a field.
  93. // putting the field in the parameter list makes it directly
  94. // initializable by the constructor

  95. class ArrayBox(
  96.     val contents: Array[String]
  97.     ) extends TextBox {
  98. }

  99. val a = Array("abc","123")
  100. val ab = new ArrayBox(a) // contents is directly set to the value of a


  101. /* Invoking superclass constructors */

  102. // A line box is a text box with exactly one line
  103. // We could implemened concretely as a special case of
  104. // an array box
  105. // we can override the definition of width and height
  106. // with a more efficient one though
  107. class LineBox(s: String) extends ArrayBox(Array(s)) {
  108.     override def width = s.length
  109.     override def height = 1
  110. }

  111. // A uniform box is a text box filled uniformly with the
  112. // same character
  113. // Its constructor needs the height and the width in advance
  114. // We can override the method height and width of TextBox
  115. // directly with parameters now
  116. class UniformBox(
  117.     ch: Char,
  118.     override val width: Int,
  119.     override val height: Int
  120.     ) extends TextBox {
  121.   private val line = ch.toString * width
  122.   val contents = Array.fill(height)(line)
  123. }




  124. /* Composition vs inheritance */

  125. // Is a LineBox really an ArrayBox?
  126. // Not really, it is just a special case of a Text Box
  127. // We do not need an array to implement it
  128. // so maybe we should define it directly as a subclass
  129. // of TextBox and store its content in a string
  130. class LineBox(s: String) extends TextBox {
  131.     val contents = Array(s)   // one line array
  132.     // we can replace the generic implementations of
  133.     // height and width by more efficient versions here too
  134.     override val width = s.length
  135.     override val height = 1
  136.   }

  137. val b1: TextBox = new ArrayBox(Array("hello", "world"))

  138. val a: ArrayBox = new LineBox("hello")

  139. val b2: TextBox = a

  140. val b3: TextBox = new UniformBox('x', 2, 3)

  141.   

  142. /* Implementing above, beside, and toString */

  143. object TB { // we pack all text box classes into module TB
  144.   abstract class TextBox {
  145.     def contents: Array[String]
  146.     def height: Int = contents.length
  147.     def width: Int = if (height == 0) 0 else contents(0).length

  148.     // above puts a box on top of another into a new box
  149.     // Assumes the two boxes to have the same width
  150.     def above(that: TextBox): TextBox =
  151.       new ArrayBox(this.contents ++ that.contents)

  152.     // besides puts one box beside another into a new box
  153.     // Assures the two boxes to have the same height
  154.     def beside(that: TextBox): TextBox = {
  155.       val contents = new Array[String](this.contents.length)
  156.       for (i <- 0 until this.contents.length)
  157.         contents(i) = this.contents(i) + that.contents(i)
  158.       new ArrayBox(contents)
  159.     }

  160.     // toString puts all lines of the text box into a single
  161.     // string, separating them with a new line character
  162.     override def toString = contents mkString "\n"
  163.   }

  164.   class LineBox(s: String) extends TextBox {
  165.     val contents = Array(s)
  166.     override val width = s.length
  167.     override val height = 1
  168.   }

  169.   class ArrayBox(conts: Array[String]) extends TextBox {
  170.     def contents: Array[String] = conts
  171.   }
  172. }


  173. import TB._ // imports all the contents of TB

  174. val l1 = new LineBox("Han ")
  175. val l2 = new LineBox("Solo ")
  176. val l3 = new LineBox("Luke ")
  177. val l4 = new LineBox("Skywalker ")

  178. val b1 = l1 beside l2
  179. val b2 = l3 beside l4

  180. val b3 = b1 above b2

  181. b3.height
  182. b3.width
复制代码

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注jltj
拉您入交流群
GMT+8, 2025-12-30 19:00