Help, In the middle of translating Python script to Scala
I'm trying to translate: http://thinkstats.com/survey.py this script.
So here's what I'm translating right now (Python):
"""This file contains code for use with "Think Stats",
by Allen B. Downey, available from greenteapress.com
Copyright 2010 Allen B. Downey
License: GNU GPLv3 http://www.gnu.org/licenses/gpl.html
"""
import sys
import gzip
import os
class Record(object):
"""Represents a record."""
class Respondent(Record):
"""Represents a respondent."""
class Pregnancy(Record):
"""Represents a pregnancy."""
Scala:
import sys.process._
import java.util.zip.GZIPInputStream
import java.io._
class Record[T](val obj: T)
class Respondent[T](val record: Record[T])
class Pregnancy[T](val record: Record[T])
Question:
Did I do the class Respondent
and the class Pregnancy
correctly? Is the type annotation correct for these class? Is the logic correct? I've开发者_JAVA百科 just read up on type parameterization so I'm a bit iffy on this and wanted to see if I'm on the correct path.
Thank you for your time.
As I recall, the Python syntax you're showing is for class extension (inheritance). The equivalent Scala would be
/** Represents a record.
*/
class Record
/** Represents a respondent.
*/
class Respondent extends Record
/** Represents a pregnancy.
*/
class Pregnancy extends Record
Scala comments of the form /** ... */
will show up as documentation in ScalaDoc.
Type parameterization isn't needed here. Its main use is to allow classes to accept or return values of an arbitrary parameterized type. For example, List[Int]
and List[String]
are lists of integers and strings respectively.
精彩评论