上游充通大 ParisTech SHANGHAI JIAO TONG UNIVERSITY INSTITUT DES SCIENCES ET TECHNOLOGIES PARIS INSTITUTE OF TECHNOLOGY Computational Thinking and Approach Lecture 7 Dr.Jialiang LU Jialiang.lu@situ.edu.cn
Computational Thinking and Approach Lecture 7 Dr. Jialiang LU Jialiang.lu@sjtu.edu.cn
上游充通大 ParisTech SHANGHAI JIAO TONG UNIVERSITY INSTITUT DES SCIENCES ET TECHNOLOGIES PARIS INSTITUTE OF TECHNOLOGY Class and Object OBJECT ORIENTED DEVELOPMENT
OBJECT ORIENTED DEVELOPMENT Class and Object
上游充通大 ParisTech SHANGHAI JIAO TONG UNIVERSITY INSTITUT DES SCIENCES ET TECHNOLOGIES PARIS INSTITUTE OF TECHNOLOGY Review of Objects Using objects is a techniques for structuring the data that our programs use. o So far,our programs have made use of objects created from pre-defined class such as circle. An object was defined as an active data type that knows stuff and can do stuff. More precisely,an object consists of: 1.A collection of related information. 2.A set of operations to manipulate that information. 3
Review of Objects • Using objects is a techniques for structuring the data that our programs use. • So far, our programs have made use of objects created from pre-defined class such as Circle. • An object was defined as an active data type that knows stuff and can do stuff. • More precisely, an object consists of: 1. A collection of related information. 2. A set of operations to manipulate that information. 3
上游充通大 ParisTech SHANGHAI JIAO TONG UNIVERSITY INSTITUT DES SCIENCES ET TECHNOLOGIES PARIS INSTITUTE OF TECHNOLOGY Review of Objects The information is stored inside the object in instance variables. The operations,called methods,are functions that "live"inside the object. Collectively,the instance variables and methods are called the attributes of an object. 4
Review of Objects • The information is stored inside the object in instance variables. • The operations, called methods, are functions that “live” inside the object. • Collectively, the instance variables and methods are called the attributes of an object. 4
上游充通大 ParisTech SHANGHAI JIAO TONG UNIVERSITY INSTITUT DES SCIENCES ET TECHNOLOGIES PARIS INSTITUTE OF TECHNOLOGY Circle A circle object will have instance variables such as center,which remembers the center point of the circle,and radius,which stores the length of the circle's radius. The draw method examines the center and radius to decide which pixels in a window should be colored. 5
Circle • A Circle object will have instance variables such as center, which remembers the center point of the circle, and radius, which stores the length of the circle’s radius. • The draw method examines the center and radius to decide which pixels in a window should be colored. 5
上游充通大 ParisTech SHANGHAI JIAO TONG UNIVERSITY INSTITUT DES SCIENCES ET TECHNOLOGIES PARIS INSTITUTE OF TECHNOLOGY Circle The move method will change the value of center to reflect the new position of the circle. All objects are said to be an instance of some class.The class of an object determines which attributes the object will have. A class is a description of what its instances will know and do. 6
Circle • The move method will change the value of center to reflect the new position of the circle. • All objects are said to be an instance of some class. The class of an object determines which attributes the object will have. • A class is a description of what its instances will know and do. 6
上游充通大 ParisTech SHANGHAI JIAO TONG UNIVERSITY INSTITUT DES SCIENCES ET TECHNOLOGIES PARIS INSTITUTE OF TECHNOLOGY Circle New objects are created from a class by invoking a constructor.You can think of the class itself as a sort of factory for stamping out new instances. 。( Consider making a new circle object: mycircle Circle(Point (0,0),20) circle,the name of the class,is used to invoke the constructor. 7
Circle • New objects are created from a class by invoking a constructor. You can think of the class itself as a sort of factory for stamping out new instances. • Consider making a new circle object: myCircle = Circle(Point(0,0),20) • Circle, the name of the class, is used to invoke the constructor. 7
上游充通大 ParisTech SHANGHAI JIAO TONG UNIVERSITY INSTITUT DES SCIENCES ET TECHNOLOGIES PARIS INSTITUTE OF TECHNOLOGY Circle myCircle Circle(Point(0,0),20) This statement creates a new circle instance and stores a reference to it in the variable mycircle. The parameters to the constructor are used to initialize some of the instance variables (center and radius)inside mycircle. 8
Circle myCircle = Circle(Point(0,0), 20) • This statement creates a new Circle instance and stores a reference to it in the variable myCircle. • The parameters to the constructor are used to initialize some of the instance variables (center and radius) inside myCircle. 8
上游充通大 ParisTech SHANGHAI JIAO TONG UNIVERSITY INSTITUT DES SCIENCES ET TECHNOLOGIES PARIS INSTITUTE OF TECHNOLOGY Circle myCircle Circle(Point (0,0),20) Once the instance has been created,it can be manipulated by calling on its methods: myCircle.draw(win) mycircle.move (dx,dy) 9
Circle myCircle = Circle(Point(0,0), 20) • Once the instance has been created, it can be manipulated by calling on its methods: myCircle.draw(win) myCircle.move(dx,dy) 9
上游充通大 ParisTech SHANGHAI JIAO TONG UNIVERSITY INSTITUT DES SCIENCES ET TECHNOLOGIES PARIS INSTITUTE OF TECHNOLOGY Class e Classes are just a way to define new sorts of stuff. - Classes are templates for creating user- defined objects. -Instances represent the concrete items in a program's domain. Classes serve as instance factories. 10
Class • Classes are just a way to define new sorts of stuff. – Classes are templates for creating userdefined objects. – Instances represent the concrete items in a program's domain. – Classes serve as instance factories. 10