CLASSES CHAPTER 25 1. Topics  Understanding Classes –The Anatomy of a Class  Class Inheritance –Superclasses and Subclasses –Virtual and Override 2.

Slides:



Advertisements
Similar presentations
Chapter 13 Abstraction and inheritance. This chapter discusses n Implementing abstraction. u extension u inheritance n Polymorphism/dynamic binding. n.
Advertisements

Final and Abstract Classes
1 Inheritance Classes and Subclasses Or Extending a Class.
10 Copyright © 2005, Oracle. All rights reserved. Reusing Code with Inheritance and Polymorphism.
The Web Warrior Guide to Web Design Technologies
OOP - Object Oriented Programming Object Oriented Programming is an approach to programming that was developed to make large programs easier to manage.
Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 3 Advanced Object-Oriented Concepts.
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
Slides prepared by Rose Williams, Binghamton University Chapter 7 Inheritance.
Encapsulation, Inheritance & Interfaces CSE 115 Spring 2006 February 27, March 1 & 3, 2006.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
C# Programming: From Problem Analysis to Program Design1 Creating Your Own Classes C# Programming: From Problem Analysis to Program Design 3rd Edition.
1 Chapter 7 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
HELLO WORLD: YOUR FIRST PROGRAM CHAPTER Topics  Hello World?  Creating a Unity Project –The Unity Project Folder  MonoDevelop: Unity's Code Editor.
Chapter 13: Object-Oriented Programming
GETTING INPUT Simple I/O. Simple Input Scanner scan = new Scanner(System.in); System.out.println("Enter your name"); String name = scan.nextLine(); System.out.println("Enter.
Inheritance using Java
SE 350 – Programming Games Lecture 6: Programming with Unity Lecturer: Gazihan Alankuş Please look at the last slide for assignments (marked with TODO)
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
An Object-Oriented Approach to Programming Logic and Design
DEBUGGING CHAPTER Topics  Getting Started with Debugging  Types of Bugs –Compile-Time Bugs –Bugs Attaching Scripts –Runtime Errors  Stepping.
UFCEKU-20-3Web Games Programming Unity 3D Basic Concepts Using and Creating Prefabs.
EEC-693/793 Applied Computer Vision with Depth Cameras Lecture 13 Wenbing Zhao
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 2 1 Java Inheritance.
11 Chapter 11 Object-Oriented Databases Database Systems: Design, Implementation, and Management 4th Edition Peter Rob & Carlos Coronel.
SE 350 – Programming Games Lecture 7: Programming with Unity Lecturer: Gazihan Alankuş Please look at the last slide for assignments (marked with TODO)
CSSE501 Object-Oriented Development. Chapter 12: Implications of Substitution  In this chapter we will investigate some of the implications of the principle.
Chapter 8. About the Midterm Exam.. Exam on March 12 Monday (Tentatively) Review on March 7 Wednesday Cover from Chapter 6 Grades will be out before spring.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
SE 320 – Introduction to Game Development Lecture 7: Programming Lecturer: Gazihan Alankuş Please look at the last two slides for assignments (marked with.
“The beauty of empowering others is that your own power is not diminished in the process.” – Barbara Colorose Thought for the Day.
CIS162AD Inheritance Part 3 09_inheritance.ppt. CIS162AD2 Overview of Topics  Inheritance  Virtual Methods used for Overriding  Abstract Classes and.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Chapter 10: Introduction to Inheritance. Objectives Learn about the concept of inheritance Extend classes Override superclass methods Call constructors.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
CPRG 215 Introduction to Object-Oriented Programming with Java Module 3- Introduction to Object Oriented Programming concepts Topic 3.4 Constructors, Overloading,
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
CSSE501 Object-Oriented Development. Chapter 10: Subclasses and Subtypes  In this chapter we will explore the relationships between the two concepts.
GameDevClub CODE CHEAT SHEET NOTE: ALL OF THE CODE IS CASE-SENSITIVE AND THE SYNTAX IS STRICT SO A LOT OF YOUR ERRORS WILL PROBABLY COME FROM TYPOS If.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
Inheritance in C++ Bryce Boe 2012/08/28 CS32, Summer 2012 B.
FUNCTIONS AND PARAMETERS CHAPTER Topics  Definition of a Function  Function Parameters and Arguments  Returning Values –Returning void  Naming.
UFCEKU-20-3Web Games Programming Instantiating World Objects.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
Copyright 2009 by Pearson Education Building Java Programs Chapter 9: Inheritance and Interfaces Lecture 9-1.
Expressive Intelligence Studio // Center for Games and Playable Media // Unity Pro John Murray Expressive.
SUBCLASSES - JAVA. The Purpose of Subclasses Class Farm String getOwner() void setOwner(String s) int getSize() void setSize(int s) Class DairyFarm String.
Java Programming Fifth Edition Chapter 9 Introduction to Inheritance.
Behavior trees & The C# programming language for Java programmers
Digital Game Design ACST 3710 Your First Unity Program 1.
EEC-693/793 Applied Computer Vision with Depth Cameras
OOP - Object Oriented Programming
EEC-693/793 Applied Computer Vision with Depth Cameras
Inheritance and Polymorphism
Microsoft Visual Basic 2005: Reloaded Second Edition
FUNCTIONS AND PARAMETERS
EEC-693/793 Applied Computer Vision with Depth Cameras
Inheritance, Polymorphism, and Interfaces. Oh My
Myo + Oculus Rift Tutorial
Fundaments of Game Design
Chapter 9 Carrano Chapter 10 Small Java
Fundaments of Game Design
EEC-693/793 Applied Computer Vision with Depth Cameras
Final and Abstract Classes
Unity Game Development
Chapter 7 Inheritance.
Presentation transcript:

CLASSES CHAPTER 25 1

Topics  Understanding Classes –The Anatomy of a Class  Class Inheritance –Superclasses and Subclasses –Virtual and Override 2

Understanding Classes  Classes are the key concept in Object-Oriented Programming  A class is a definition of a type of object  There can be many instances of a single class –Each person in this classroom could be thought of as an instance of the Human class  C# classes combine data and functionality –Classes have variables, which are called fields –Classes have functions, which are called methods  You're already using classes! –Each C# script you've written is a class  Classes represent objects in your game 3

Understanding Classes  Example: A character in a standard RPG –Fields you would want for each character stringname;// The character's name floathealth;// The amount of health she has floathealthMax;// Her maximum amount of health List inventory;// List of Items in her inventory List equipped;// A List of Items she has equipped –Methods you would want void Move(Vector3 newLoc) {…}// Moves her to newLoc void Attack(Character target) {…}// Attacks target with the current weapon or spell void TakeDamage(float dmgAmt) {…}// Reduces health void Equip(Item newItem) {…}// Adds an Item to the equipped List 4

The Anatomy of a Class 5 Includes The Class Declaration Fields Methods Properties p.s. Line numbers are handled automatically by MonoDevelop

The Anatomy of a Class  We'll explore each part of a class named Enemy –The Enemy class is for a simple top-down space shooter game –An Enemy instance moves down the screen at a speed of 10  Includes –Include code libraries in your project –Enables standard Unity libraries and objects e.g., GameObject, MonoBehaviour, Transform, Renderer, etc. 1 using UnityEngine;// Required for Unity 2 using System.Collections;// Included by Unity's default 3 using System.Collections.Generic;// Required to use a List  The Class Declaration –Declares the name of the class and its superclass –Enemy is a class that extends its superclass MonoBehaviour 5 public class Enemy : MonoBehaviour { 6

The Anatomy of a Class  Fields –Fields are variables that are part of the class –Fields marked public are able to be seen by other classes and by other instances of this class –Fields marked private are only able to be seen by this one instance of a class Private fields are secrets They are also a safer way to program than always using public fields Public fields are used throughout the book so that the field values appear and are editable in the Unity Inspector 7public floatspeed = 10f;// The speed in m/s 8public floatfireRate = 0.3f;// Shots per second (Unused) Declares two public fields for all instances of the Enemy class Each instance has its own value for speed and fireRate 7

The Anatomy of a Class  Methods –Functions that are part of the class –Can also be marked public or private 11void Update() { 12Move(); 13} 14 15public virtual void Move() {// Move down the screen at speed 16Vector3 tempPos = pos; 17tempPos.y -= speed * Time.deltaTime; // Makes it Time-Based! 18pos = tempPos; 19} –Note that Move is a virtual function Virtual functions can be overridden by functions of the same name in a subclass (we'll cover this shortly) 8

The Anatomy of a Class  Properties –Properties are methods masquerading as fields –Properties can only exist within classes 35public Vector3 pos { 36get { 37return( this.transform.position ); 38} 39set { 40this.transform.position = value; 41} 42} –This property simplifies setting the transform.position of this Enemy 9

Class Instances as Components  In Unity, all class instances are treated as GameObject Components –The class instance can be accessed using GetComponent<>() Enemy thisEnemy = this.gameObject.GetComponent (); –From there, any public variable can be accessed thisEnemy.speed = 20f;// Increase speed of this Enemy to 20 –Many C# scripts can be attached to a single GameObject 10

Class Inheritance  Most classes inherit from another class 5 public class Enemy : MonoBehaviour {…}  Enemy inherits from MonoBehaviour –Enemy is the subclass of MonoBehavior –MonoBehavior is called the superclass, base class, or parent class of Enemy –This means that Enemy inherits all of MonoBehaviour's fields and methods Example inherited fields: –gameObject, transform, renderer, etc. Example inherited methods: –GetComponent<>(), Invoke(), StartCoroutine(), etc. Inheriting from MonoBehaviour is what makes Enemy able to act like a GameObject component 11

Class Inheritance  We can create a class that inherits from Enemy! 1using UnityEngine; 2using System.Collections; 3 4public class EnemyZig : Enemy { 5 // EnemyZig inherits ALL its behavior from Enemy 6}  If this class is attached to a different GameObject, that GameObject will act exactly like an Enemy –It will also move down the screen at a rate of 10m/second  Move() can be overridden because it is a virtual function –This means that EnemyZig can have its own version of Move() ! 12

Class Inheritance  EnemyZig.Move() overrides Enemy.Move() 4public class EnemyZig : Enemy { 5 public override void Move () { 6 Vector3 tempPos = pos; 7 tempPos.x = Mathf.Sin(Time.time * Mathf.PI*2) * 4; 8 pos = tempPos; // Uses the pos property of the superclass 9 base.Move(); // Calls Move() on the superclass 10 } 11} –Now, when the Update() method in Enemy calls Move(), EnemyZig instances will use EnemyZig.Move() instead This moves the EnemyZig instance back and forth horizontally –On line 9, base.Move() calls the Move() function on EnemyZig's base class, Enemy This causes EnemyZig instances to continue to move downward as well 13

Chapter 25 – Summary 14  Classes combine data (fields) and functionality (methods)  Classes can inherit from each other  Classes are used in Unity as GameObject Components  Understanding classes is the key to object-oriented programming (OOP) –Before OOP, games were often a single, very large function –With OOP, each object in the game is a class, and each class can think for itself  Next Chapter: Object-Oriented Thinking –The next chapter talks more about the OOP mentality –Also has a section where you make procedural art!