#3_Compound Data Unions of Classes_7.ppt

17 100 0
#3_Compound Data Unions of Classes_7.ppt

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Compound Data: Union of Classes Week 2 HOW TO DESIGN CLASS HIERARCHIES Basic Java Programming Course material developed by Mai Anh Tho (tho@hcmuaf.edu.vn) Course material developed by Mai Anh Tho (tho@hcmuaf.edu.vn) The Train Trip Problem Revisited • In the train trip problem, we distinguish between two kinds of trains with a boolean field • If the field is t rue, the instance represents a local train; otherwise, it is an express train TrainTrip - Route route - Schedule schedule - boolean local Route - String origin - String destination 11 Schedule - ClockTime departure - ClockTime arrival 11 ClockTime - int hour - int minute 22 • While a boolean field may work for a simple distinction like that, it really isn't a good way to think about distinct kinds of trains • It also doesn't scale to problems, like those of real train companies, that offer a variety of trains, e.g., local, regional, and long distance express trains • A better way to represent such subcategories of a class is to REFINE a class • we introduce a subclass or that one class extends another class Problem 1 • Develop a drawing program that deals with at least three kinds of shapes: dots, squares, and circles • The shapes are located on a Cartesian grid whose origin is in the northwest • A dot is located in the grid and is drawn as a small disk of a fixed size (3 pixels) • A square's location is specified via its north-west corner in the grid and its size • A circle's essential properties are its center point and its radius Examples A Shape is either a Dot a Square, or a Circle Class Diagram (Design 1) • It says that that Dot, Square, and Circle are SUBCLASSES or REFINED CLASSES of AShape • Because all instances of AShape are instances of either Dot, Square, or Circle, we say that AShape is an ABSTRACT CLASS AShape Dot - CartesianPoint location Square - CartesianPoint location - int size Circle - CartesianPoint location - int radius CartesianPoint - int x - int y 11 11 11 Refactoring (Design 2) Dot Square - int size Circle - int radius AShape # CartesianPoint location CartesianPoint - int x - int y 11 public class CartesianPoint { private int x; private int y; public CartesianPoint(int x, int y) { this.x = x; this.y = y; } } public class CartesianPointTest extends TestCase { public void testConstructor() { new CartesianPoint(30, 60); } } public abstract class AShape { protected CartesianPoint location; protected AShape(CartesianPoint location) { this.location = location; } } public class Dot extends AShape { public Dot(CartesianPoint location) { super(location); } } public class DotTest extends TestCase { public void testConstructor() { new Dot(new CartesianPoint(100, 200)); CartesianPoint location = new CartesianPoint(100, 200); new Dot(location); } } [...]... CartesianPoint(77,22), 300) • The result is an instance of Square • We can also say that every Square is a AShape and that there are no instances of AShape; the class is abstract Type • Every class is a type • If C is a class and extends class A, and if c is created with new C( ), then 1 c is an instance of C, 2 c is of type C, 3 c is also of type A, and 4 c is not an instance of A Relax & …Do Exercises … GOOD JOB!... Exercise 4.0.4 • Write down the constructors for the classes ExpressTrain, ChineseRestaurant, and Sedan Exercise 4.0.4 • Translate the three graphical shape examples from the problem statement into instances of Dot, Square, and Circle . 1) • It says that that Dot, Square, and Circle are SUBCLASSES or REFINED CLASSES of AShape • Because all instances of AShape are instances of either Dot, Square, or Circle, we say that AShape. instance of C, 2. c is of type C, 3. c is also of type A, and 4. c is not an instance of A Relax & …Do Exercises … GOOD JOB! Exercise 4.0.4 • Write down the constructors for the classes. Compound Data: Union of Classes Week 2 HOW TO DESIGN CLASS HIERARCHIES Basic Java Programming Course material

Ngày đăng: 16/07/2014, 04:00

Mục lục

  • Compound Data: Union of Classes

  • The Train Trip Problem Revisited

  • Slide 3

  • Problem 1

  • Examples

  • Class Diagram (Design 1)

  • Refactoring (Design 2)

  • Slide 8

  • Slide 9

  • Slide 10

  • Slide 11

  • Slide 12

  • Instance

  • Type

  • Relax & …Do Exercises …

  • Exercise 4.0.4

  • Slide 17

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan