Chapter 3 Writing classes

32 298 0
Chapter 3 Writing classes

Đ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

Writing Classes Writing Classes Chapter 3 Chapter 3 1 Out lin e • Introduction to Object-Oriented Programming (OOP) • Writing a Java class • Encapsulation and accessor methods 2 Procedural Programming Procedural Programming and and Object-Oriented Programming Object-Oriented Programming  Procedural Programming consists of designing a set of functions (or algorithms) to solve a problem • Algorithms + Data Structures = Programs  Object-Oriented Programming (OOP) puts data first, then looks at the algorithms that operate on the data 3 Concepts in OOP Concepts in OOP  Class • A class is the model, pattern, or blueprint from which objects are actually made • A class means a category of things • A class can be used in Java as the data type  Object • Object means a particular item that belongs to a class • Object also called an “instance”  Example: • String s1 = "Hello"; Here, String is the class, and the variable s1 is objects (or “instances of the String class”) 4 Concepts in OOP Concepts in OOP (cont.) (cont.)  Attributes • The properties that describe an object  Behaviors • Activities of an object  Each object has a set of attributes (state) and a set of behaviors 5  2003 Prentice Hall, Inc. All rights reserved. Example: Class and Object Class: Ball Attributes: radius, color Behavior: thrown, bounced, rolled Object1 radius = 5 color = red thrown, bounced, rolled Object3 radius = 3 color = green thrown, bounced, rolled Object2 radius = 10 color = blue thrown, bounced, rolled 6 Examples Examples  List attributes and behaviors of students • Student code, name, gender, birthday, birthplace, class, grade 1, grade 2,… • Calculate average grade, view grade, pay tuition,…  Suppose that you write a program to calculate area and perimeter of a rectangle. Find objects and list attributes and behaviors of them • Object: Rectangle • Attributes: width, height • Behaviors: Calculate area, calculator perimeter 7 Examples Examples (cont.) (cont.)  Suppose that you want to write a program to monthly pay the employees of a company. Paying employees based on products they made, in addition, you have to compute Social Security and Medicare taxes. Find objects and list attributes and behaviors of them • Object: Employee • Attributes: employee number, name, address, social security number, products,… • Behaviors: compute employee pay, compute employee Social Security and Medicare taxes, mail employee a paycheck once a month,… 8 Objects model Objects model ClassName - Attributes + Behaviors Properties Data Fields Variables Instance data Methods 9 Objects model: Objects model: Example Example Rectangle - width - height + Calculate area + Calculator perimeter Student - Student code - Name - Gender - Birthday - Birthplace - Class - Grade 1 - Grade 2 + Calculate average + View grade + Pay tuition 10 [...]...Outline • Introduction to Object-Oriented Programming (OOP) • Writing a Java class • Encapsulation and accessor methods 11 Writing a Java Class class ClassName { property1 property2 constructor1 constructor2 method1 method2 } 12 Writing class Rectangle Rectangle - width: double - height: double class ClassName { property1 class Rectangle { double... the names of your classes and methods reflect their responsibilities 30 Questions  Syntax to create a class in Java?  What is constructor? How many constructors can we have in a class?  What purpose of using this keyword?  Why to make all instance variables private?  How to access private instance variables?  Write a class to calculate area of a circle with radius Test this class 31 Read more ... constructor can invoke other constructor, by using this(agrs), note the this statement must appear as the first line of code in the constructor (otherwise, a compiler error will occur) 21 Example: Writing class Rectangle (3) Using this Rectangle.java public class Rectangle { double width, height; public Rectangle( double width, double height ) { this.width = width; this.height = height; } public Rectangle()... peri = r.getPerimeter(); System.out.println("Area is "+ area); System.out.println("Perimeter is "+ peri); } } 22 Outline • Introduction to Object-Oriented Programming (OOP) • Writing a Java class • Encapsulation and accessor methods 23 Encapsulation  Encapsulation is the technique of making the fields in a class private and providing access to the fields via public methods Client (user) Methods Data ... double + getPerimeter() : double property2 public double getArea() { return width*height; } constructor1 constructor2 public double getPerimeter() { return (width+height)*2; } method1 method2 } } 13 Writing class Student Student class Student{ - studentCode: String String studentCode, name, birthplace, classID; class NameOfClass { - name: String attribute1 boolean gender; attribute2 Date birthday;... fields would be 0, all booleans would be false, and all object variables would be set to null  If you have your own constructor, you should provide a default constructor, it can do nothing 19 Example: Writing class Rectangle (2) Class with Constructors Rectangle.java public class Rectangle { double width, height; public Rectangle( double x, double y ) { width = x; height = y; } public double getArea()... client  service methods • private method: cannot be invoked from outside the class The only purpose of a private method is to help the other methods of the class do their job  support methods 25 Example: Writing class Rectangle (4) Write accessor methods Rectangle.java public class Rectangle { private double width, height; public Rectangle (double width, double height) { this.width = width; this.height... return height; } public void setHeight(double height) { this.height = height; } public double getArea() { return width * height; } public double getPerimeter() { return (width+height) * 2; } } 26 Example: Writing class Rectangle (4) Write accessor methods RectangleTest.java public class RectangleTest { public static void main( String[] args ) { Rectangle rect = new Rectangle(); rect.setWidth(10); rect.setHeight(5);... shipName = …; } public String getShipName() { return shipName; } // No setShipName method }  Getter/setter names need not correspond to instance variable names 28 Note: Java naming conventions  Start classes with uppercase letters  Start other things with lowercase letters • Instance variables, local variables, methods, parameters to methods public class MyClass { public String firstName, lastName;... of an object, using the dot operator objectName.fieldName objectName.methodName(argumentsToMethod)  Example: r.width = 10; r.height = 5; double x= r.getArea(); double y= r.getPerimeter(); 16 Example: Writing class Rectangle (1) Rectangle.java public class Rectangle { double width, height; public double getArea() { return width * height; } public double getPerimeter() { return (width+height) * 2; } . Writing Classes Writing Classes Chapter 3 Chapter 3 1 Out lin e • Introduction to Object-Oriented Programming (OOP) • Writing a Java class • Encapsulation. e • Introduction to Object-Oriented Programming (OOP) • Writing a Java class • Encapsulation and accessor methods 11 Writing a Java Class Writing a Java Class class ClassName { property1 property2 ClassName { property1 property2 . . . constructor1 constructor2 . . . method1 method2 . . . } 12 Writing class Rectangle Writing class Rectangle class ClassName { property1 property2 . . . constructor1 constructor2 .

Ngày đăng: 13/05/2014, 10:42

Từ khóa liên quan

Mục lục

  • Writing Classes

  • Outline

  • Procedural Programming and Object-Oriented Programming

  • Concepts in OOP

  • Concepts in OOP (cont.)

  • Example: Class and Object

  • Examples

  • Examples (cont.)

  • Objects model

  • Objects model: Example

  • Slide 11

  • Writing a Java Class

  • Writing class Rectangle

  • Writing class Student

  • Declaring objects

  • Accessing instance variables

  • Example: Writing class Rectangle (1)

  • Constructor

  • Constructor (cont.)

  • Example: Writing class Rectangle (2)

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

Tài liệu liên quan