Classes, Objects, And Variables

Slides:



Advertisements
Similar presentations
PHP functions What are Functions? A function structure:
Advertisements

Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14 Introduction to Ruby.
Python Objects and Classes
Written by: Dr. JJ Shepherd
What is a class? a class definition is a blueprint to build objects its like you use the blueprint for a house to build many houses in the same way you.
CS102--Object Oriented Programming Review 1: Chapter 1 – Chapter 7 Copyright © 2008 Xiaoyan Li.
Ruby (on Rails) CSE 190M, Spring 2009 Week 4. Constructors Writing a new class is simple! Example: class Point end But we may want to initialize state.
Ruby (on Rails) Slides modified by ements-2ed.shtml) 1.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Ruby and the tools Topics Ruby Rails January 15, 2014 CSCE 740 Software Engineering.
Ruby and the tools 740Tools05ClassesObjectsVars Topics Ruby Classes Objects Variables Containers Blocks Iterators Spring 2014 CSCE 740 Software Engineering.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
Python uses boolean variables to evaluate conditions. The boolean values True and False are returned when an expression is compared or evaluated.
+ Ruby and other programming Languages Ronald L. Ramos.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
Ruby Objects, Classes and Variables CS 480/680 – Comparative Languages.
CS 142 Lecture Notes: RubySlide 1 Basic Ruby Syntax sum = 0 i = 1 while i
Introduction to information systems RUBY dr inż. Tomasz Pieciukiewicz.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Basic Class Structure. Class vs. Object class - a template for building an object –defines the instance data that the object will hold –defines instance.
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
These materials where developed by Martin Schray
Ruby Inheritance and other languages….
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Re-Intro to Object Oriented Programming
Topic: Classes and Objects
Creating and Using Objects, Exceptions, Strings
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
Examples of Classes & Objects
Variables All variables hold object references
Java Primer 1: Types, Classes and Operators
Review: Two Programming Paradigms
classes and objects review
PHP Classes and Objects
JavaScript: Functions.
Lecture VI Objects The OOP Concept Defining Classes Methods
Templates.
CMPE212 – Stuff… Exercises 4, 5 and 6 are all fair game now.
The Ruby Programming Language
Chapter 14 Introduction to Ruby.
Basic Ruby Syntax sum = 0 i = 1 while i <= 10 do sum += i*i
Web Systems Development (CSC-215)
Corresponds with Chapter 7
CSE341: Programming Languages Ruby: Blocks & Procs; Inheritance & Overriding Alan Borning Spring 2018.
Inheritance Dr. Bhargavi Goswami Department of Computer Science
Classes & Objects: Examples
CSCI 3328 Object Oriented Programming in C# Chapter 9: Classes and Objects: A Deeper Look – Exercises UTPA – Fall 2012 This set of slides is revised from.
The University of Texas – Pan American
Ruby and the tools 740Tools05ClassesObjectsVars
Defining Classes and Methods
Java Inheritance.
Chapter 8 Classes User-Defined Classes and ADTs
Fall 2018 CISC124 2/24/2019 CISC124 Quiz 1 marking is complete. Quiz average was about 40/60 or 67%. TAs are still grading assn 1. Assn 2 due this Friday,
Ruby Classes.
CMSC 202 Generics.
Fundaments of Game Design
Ruby Containers, Iterators, and Blocks
CSCE 740 Software Engineering
CIS 199 Final Review.
Introduction to Object-Oriented Programming
CMPE212 – Reminders Quiz 1 marking done. Assignment 2 due next Friday.
Chap 2. Identifiers, Keywords, and Types
Basic Ruby Syntax sum = 0 i = 1 while i <= 10 do sum += i*i
CS 1054: Lecture 2, Chapter 1 Objects and Classes.
Classes and Objects CGS3416 Spring 2019.
CMPE212 – Reminders Assignment 2 due next Friday.
Code Blocks, Closures, and Continuations
C++ Object Oriented 1.
Creating and Using Classes
Presentation transcript:

Classes, Objects, And Variables Sudheer Gupta Bysani

Class definition & instantiation Use keyword “class” Class definition ends by keyword “end” Instantiation : OneSong = Song.new #class definition example class Song # methods goes here # …. end

Constructors in ruby “initialize” method is used Member variables start with @ Eg: @name class Song def initialize(name, artist, duration) @name     = name      @artist   = artist      @duration = duration    end end

Attributes : Readable & Writable Readable : return values of instance variables Writable : assign values to instance variables class Song attr_reader :name, :artist, :duration attr_writer :duration # note : no “@” for attributes def initialize(name,artist,duration) # …. end aSong = Song.new("Bicylops", "Fleck", 260) aSong.duration >> 260 aSong.duration = 257 aSong.duration >> 257

Virtual Attributes Wrappers around an object’s instance variables Eg: to access duration in minutes class Song def durationInMinutes @duration/60.0   # force floating point end def durationInMinutes=(value) @duration = (value*60).to_i

Inheritance Use “<“ to derive from base class Use “super” to call the base call initializer class KaraokeSong < Song def initialize(name, artist, duration, lyrics) super(name, artist, duration) @lyrics = lyrics end

Class variables & Class Method Just like static variables in C++ Starts with “@@” Eg : @@num_songs By default, private to the class accessor method to be written explicitly to be accessible to outside world. Accessor method could be instance method or class method Class Methods definition preceded by class name eg: def Song.class_method # .. end

Access Control Two ways to specify access levels of methods 1st Method: Similar to C++ class MyClass def method1 # default is public end Private #subsequent methods are private def method2 Protected #subsequent methods are protected def method3

Access Control ..continued 2nd Method: class MyClass def method1 end # other methods public : method1, :method2 private : method3 protected : method4

Protected Access Used when objects need to access internal state of other objects class Account    attr_reader :balance       # accessor method 'balance'    protected :balance         # and make it protected # need a reader attribute and still not accessible to outside world    def greaterBalanceThan(other)      return @balance > other.balance    end

Container, Blocks and Iterators Sudheer Gupta Bysani

Arrays Holds a collection of object references 2 ways to create arrays 1st Method: a = [3.14, “pie”, 99 ] a.Class  Array a.Length  3 A[0]  3.14 2nd Method: b = Array.new b[0] = 3.14 b[1] = “pie”

Handy ways of using an Array a[-1]  returns last element a[-2]  returns last but one element a[1 , 3]  [start, count] a[1..3]  [start, end] Concatenation : [1, 2,3] + [4,5]  [1,2,3,4,5] Difference : [1,1,1,2,3,2,4,5] – [1,2]  [3,4,5] Removes all the occurrences Append : [1,2] << “c” << “d” << [4,5]  [1,2,”c”,”d”,4,5] Equality : [“a”, “b”] == [“a”,”c”]  false Clear : a.clear # removes all the elements of the array Each : a.each { |x| puts x }

Hashes Associate arrays/maps/dictionaries h = { “dog” => “canine, “cat” => “feline” } h.length  2 h[“dog”]  “canine” h[“cow”] = “bovine” h.each { |key, value| puts “#{key} is #{value}” } Calls block once for each key, passing key and value as parameters h.each_key { |key| puts key } Calls block once for each key Merge, has_key, has_value, invert methods available

Blocks and Iterators Iterator : a method that invokes block of code repeatedly eg: def fibUpTo(max)    i1, i2 = 1, 1         # parallel assignment    while i1 <= max      yield i1      i1, i2 = i2, i1+i2    end fibUpTo(1000) { |f| print f, " " } def threeTimes    yield    yield end threeTimes { puts "Hello" }

Find method .... Using iterator A block may also return a value to the method class Array    def find      for i in 0...size        value = self[i]        return value if yield(value)      end      return nil    end [1, 3, 5, 7, 9].find {|v| v*v > 30 }  7 # Block returns true if v*v > 30

Blocks as Closures Code block as Proc Object Use Proc#call method to invoke the block Proc object carries all the context in which block was defined. In below case, its on method nTimes def nTimes(aThing)    return lambda { |n| aThing * n } end p1 = nTimes(23) p1.call(3)  69 p1.call(4)  92 p2 = nTimes("Hello ") p2.call(3)  Hello Hello Hello "