CS312 - Spring 2012 - Class 2
- assignment 1?
- door code
- text editor?
- keep up with the reading
look at regular expression slides
- what is a class?
- a template that describe the behavior of a class of objects (generally, the data and methods)
- what are instance variables?
- instance variables are variables that are defined by a class and are associated with a particular object (i.e. an instance of the class)
- what are methods?
- methods are functions that are associated with a class of objects.
- what are accessor and mutator methods?
- accessor methods ask some question about the object
- mutator methods manipulate or change the underlying object
- what is a static variable in Java?
- a variable associated with the class not a particular object of that class
- why are they useful?
- sometimes we may want to aggregate data about the class of objects
- sometimes we may want to communicate data between objects within a class (e.g. locks, semaphors)
- why do we care about classes?
- the key component of object-oriented programming
naming conventions for ruby
- variables, class names, etc: Figure 2.1 from pg. 37 or Ruby
- class files: class_name_with_underscores.rb
- other files: descriptive_name_with_underscores.rb
classes in ruby
- say we wanted to define a person class in Java with the following characteristics:
- instance variables
- name (defaults to 18 if no age specified)
- name, age
- name: getter
- age: getter and setter
- is legal to drink
- look at Person.java in
- look at person.rb in
- this is the equivalent code in ruby
- what are some differences?
- we see it has two instance variables: @name and @age
- these do NOT need to be defined like in java
- you can just use them. the @ signals that they aren't local variables.
- attr_ define attributes
- think of these like adding getters and setters
- they define methods that can be called externally on the object to access the instance variables
- attr_reader creates a getter method
- :name would create a method name()
- attr_writer creates a setter method
- :name would create a method name= (allowing for assignment)
- attr_accessor creates both getter and setter methods
- they are NOT necessary for defining instance variables
- :name is a symbol (we'll see these a lot and will talk more about them)
- think of it like the constructor
- specifies an optional parameter
- if one parameter is specified, age defaults to 18
- if two are specified, then the second parameter gets assigned to age
- age and @age are different things
- @age is the instance variable
- age is the parameter
- as we saw before to_s is the equivalent of toString
- look at person_script.rb in
- what do you think it does?
- what are some new things?
- to create a new object, we use .new (which calls the initialize method)
- notice if you say "puts some_object" it calls the .to_s method
- the '?' is an operator (this is in C++, Java and other languages)
- it has three components
<boolean> ? <if_true> : <if_false>
- true and false
- the only things that are false in ruby are nil and false
- everything else is true
- we can see what methods a Person object has by calling the .methods method
- like in java, everything inherits from Object, which is where we get all the other methods that we did not define)
- imports the code "relative" to the current location
- basically, starts executing the new file from the top moving down, then comes back to the current file
- for any run, if code is required, will only execute once
- "require" is for built-in (or installed) modules
- what if we wanted to keep track of how many Person objects we'd created?
- how would we do it in java?
- static variable
- in ruby, we can declare "class variables"
- denoted using @@
- only one instance of the variable per class (not per object)
- we can also create class methods (similar to static methods) by adding self. in front of the method name
- unlike in java these can only be called using the class and not on an object
- Person.population is legal
- mary.population is NOT legal
- let's say we now want to make a student class with the same attributes as the person class, but with more specificity
- look at student_script.rb
- what attributes does the person class have?
- age and name from Person
- look at student.rb in
- anything new?
- "<" is used to specify inheritance
- in ruby, you can only inherit from one class (why?)
- notice that @semesters is an instance variables without any accessor methods (i.e. can't be accessed externally)
- we can access @age because we inherited from the class
- instance variables by default are protected
- what is the method ten_point_scale= ?
- ten_point_scale is a virtual attribute
- there is no actual instance variable
- but we can access the object as if there were
building a data structure
- look at