(infinitespeak.wordpress.com) Classes and Structs. Dr. Sarah Abraham

Similar documents
iphone Application Programming Lecture 3: Swift Part 2

iphone Application Programming Lecture 3: Swift Part 2

Mobile Application Programming. Swift Classes

SWIFT - PROTOCOLS. Protocols also follow the similar syntax as that of classes, structures, and enumerations:

Mobile Application Programming. Swift Classes

iphone Application Programming Lab 3: Swift Types and Custom Operator + A02 discussion

Introductory ios Development

Protocols and Delegates. Dr. Sarah Abraham

ITP 342 Mobile App Dev. Fundamentals

Introduction to Swift. Dr. Sarah Abraham

Inheritance and Polymorphism

Object Oriented Programming

Object Oriented Programming

Functions and Collections. Dr. Sarah Abraham

Chapter 5 Object-Oriented Programming

C++ (classes) Hwansoo Han

Porting Objective-C to Swift. Richard Ekle

Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs.

Course Text. Course Description. Course Objectives. StraighterLine Introduction to Programming in C++

More On inheritance. What you can do in subclass regarding methods:

Chapter 14 Abstract Classes and Interfaces

Java Object Oriented Design. CSC207 Fall 2014

VIRTUAL FUNCTIONS Chapter 10

CS Programming I: Inheritance

CS/ENGRD 2110 FALL Lecture 5: Local vars; Inside-out rule; constructors

Questions. Exams: no. Get by without own Mac? Why ios? ios vs Android restrictions. Selling in App store how hard to publish? Future of Objective-C?

Type Analysis. Type Checking vs. Type Inference

Object-Oriented Programming

CSE 303: Concepts and Tools for Software Development

CS-202 Introduction to Object Oriented Programming

Object Oriented Programming: Based on slides from Skrien Chapter 2

Inheritance. Transitivity

Collection Views. Dr. Sarah Abraham

What property of a C# array indicates its allocated size? What keyword in the base class allows a method to be polymorphic?

Classes. Logical method to organise data and functions in a same structure. Also known as abstract data type (ADT).

C++ Inheritance and Encapsulation

What about Object-Oriented Languages?

Week 5-1: ADT Design

Data Storage. Dr. Sarah Abraham

Practice for Chapter 11

INHERITANCE & POLYMORPHISM. INTRODUCTION IB DP Computer science Standard Level ICS3U. INTRODUCTION IB DP Computer science Standard Level ICS3U

DOWNLOAD PDF CORE JAVA APTITUDE QUESTIONS AND ANSWERS

Introduction to Programming Using Java (98-388)

Everything is an object. Almost, but all objects are of type Object!

Course Content. Objectives of Lecture 24 Inheritance. Outline of Lecture 24. Inheritance Hierarchy. The Idea Behind Inheritance

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

Object-Oriented Programming (Java)

Course Content. Objectives of Lecture 24 Inheritance. Outline of Lecture 24. CMPUT 102: Inheritance Dr. Osmar R. Zaïane. University of Alberta 4

CS/ENGRD 2110 FALL Lecture 5: Local vars; Inside-out rule; constructors

iphone Application Programming Lab 2: MVC and Delegation + A01 discussion

Introducing C++ David Chisnall. March 15, 2011

PowerPoint Slides. Object-Oriented Design Using JAVA. Chapter 2. by Dale Skrien

CO Java SE 8: Fundamentals

C++ Important Questions with Answers

Object Oriented Programming. Java-Lecture 11 Polymorphism

SWIFT - METHODS. In Swift language, Classes, Structures and Enumeration instances are accessed through the instance methods.

Static Semantics. Winter /3/ Hal Perkins & UW CSE I-1

CSE 374 Programming Concepts & Tools. Hal Perkins Fall 2015 Lecture 19 Introduction to C++

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

Design issues for objectoriented. languages. Objects-only "pure" language vs mixed. Are subclasses subtypes of the superclass?

CPS 506 Comparative Programming Languages. Programming Language

OOP Design Conclusions and Variations

CSE 401/M501 Compilers

Programming Exercise 14: Inheritance and Polymorphism

Object Oriented Design. Object-Oriented Design. Inheritance & Polymorphism. Class Hierarchy. Goals Robustness Adaptability Flexible code reuse

What are the characteristics of Object Oriented programming language?

Chair of Software Engineering. Languages in Depth Series: Java Programming. Prof. Dr. Bertrand Meyer. Exercise Session 9.

Polymorphism. Object Orientated Programming in Java. Benjamin Kenwright

CS Internet programming Unit- I Part - A 1 Define Java. 2. What is a Class? 3. What is an Object? 4. What is an Instance?

C++ Inheritance and Encapsulation

Developing Applications for ios

Intro to OOP Visibility/protection levels and constructors Friend, convert constructor, destructor Operator overloading a<=b a.

Inheritance -- Introduction

More C++ : Vectors, Classes, Inheritance, Templates

A FRAMEWORK FOR DATA STRUCTURES IN A TYPED FORTH

More C++ : Vectors, Classes, Inheritance, Templates. with content from cplusplus.com, codeguru.com

Object-Oriented Design. March 2005 Object Oriented Design 1

Object Oriented Design

COMP 2355 Introduction to Systems Programming

Polymorphism. Arizona State University 1

Bags. Chapter 1. Copyright 2012 by Pearson Education, Inc. All rights reserved

CSE115 / CSE503 Introduction to Computer Science I. Dr. Carl Alphonce 343 Davis Hall Office hours:

Make Classes Useful Again

Class, Variable, Constructor, Object, Method Questions

Decorators. Userland Extensions to ES6 Classes

COP 3330 Final Exam Review

Lecture 18 CSE11 Fall 2013 Inheritance

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

26. Interfaces. Java. Fall 2009 Instructor: Dr. Masoud Yaghini

Inheritance and Encapsulation. Amit Gupta

CSE P 501 Compilers. Static Semantics Hal Perkins Winter /22/ Hal Perkins & UW CSE I-1

Chapter 5: Procedural abstraction. Function procedures. Function procedures. Proper procedures and function procedures

Inheritance and object compatibility

Lesson 10. Work with Interfaces and Abstract Classes. Copyright all rights reserved

OOPs Concepts. 1. Data Hiding 2. Encapsulation 3. Abstraction 4. Is-A Relationship 5. Method Signature 6. Polymorphism 7. Constructors 8.

Compaq Interview Questions And Answers

CSCI-142 Exam 1 Review September 25, 2016 Presented by the RIT Computer Science Community

Abstract Classes and Interfaces

Data Structures and Algorithms Design Goals Implementation Goals Design Principles Design Techniques. Version 03.s 2-1

SWIFT - INITIALIZATION

Transcription:

(infinitespeak.wordpress.com) Classes and Structs Dr. Sarah Abraham University of Texas at Austin CS329e Fall 2018

Classes and Structures General-purpose, flexible constructs to build blocks of code Properties and methods add functionality Defining classes and structs in a file makes external interface automatically available in Swift

Classes vs Structs Both classes and structs define: Properties, methods, initializers Classes allow for: Inheritance Type-casting to check type of class at runtime Deinitialization and reference counting Structs passed by value, classes passed by reference

Defining a Struct struct Point { var x = 0.0 var y = 0.0 //Create a struct instance and change its x value var p1 = Point() p1.x = 10.0

Defining a Class class Person { var firstname:string? var lastname:string? func description() -> String { return \(lastname!), \(firstname!)

Creating an Instance Each instance has its own memory and set of properties: let p1 = Person() Instances can call on instance methods p1.description() Method is called on the instance itself

Initializers Automatically called after memory is allocated Creates an object with a good starting state init() { self.firstname = Unknown self.lastname = Unknown If no init() is provided, will auto-generate a default init() with an empty method body

Initializing Property Values Can provide default values for properties var firstname: String = Unknown var lastname: String = Unknown init() { Can overload initializers to determine property values init(firstname: String, lastname: String) { self.firstname = firstname self.lastname = lastname

Designated Initializer Main initializer used for a class All other initializers funnel through this initializer Ensures initialization occurs through superclass chain Must call designated initializer from its superclass if it has one Set all properties of class while letting user send in customized values

Convenience Initializers Secondary, supporting initializers for a class Must call another initializer from the same class Must ultimately call the designated initializer init method is prefixed with convenience

Convenience Initializer Example //Designated initializer init(firstname: String, lastname: String) { self.firstname = firstname self.lastname = lastname //Convenience initializer convenience init() { self.init(firstname: Lev, lastname: Tolstoy )

Why All The Hassle? All this setup might be redundant in these toy examples but it s useful when there s inheritance: Person Super/Base class Player Sub/Derived class Prevents nil from creating unexpected behavior Swift passes nil values!

Class-Level Methods and Properties Type methods called on the type itself rather than an instance class keyword defines type-methods Allows subclasses to override superclass implementation static also works but methods cannot be overwritten by subclass Class-level properties are defined at the type, rather than instance, level static keyword defines class-level properties

Type-Method Example class Player { static var unlockedlevels = 1 class func unlocklevels(levels: Int) { unlockedlevels += levels var currentlevel = 1 func updatecurrentlevel(selectedlevel : Int) { if selectedlevel < Player.unlockedLevels { currentlevel = selectedlevel else { currentlevel = Player.unlockedLevels

Quiz Question! What is the difference between an instance method and a type method? A. Instance methods are called by the class instance, type methods are called at the class-level B. Instance methods can be used by all types, whereas type methods are specific to one type C. Instance methods cannot be accessed outside the class, type methods are available anywhere in the code

Private Properties and Methods Cannot be accessed outside of the class Preserve internal workings of classes Maintain modular, black box nature of classes Reduce unexpected class access patterns private keyword declared before type: private var currentsprite: Sprite private func setsprite(newsprite: Sprite) { currentsprite = newsprite