Object-Oriented Programming - Lecture 3: Classes and Objects - Lê Hồng Phương

● Returning a value from a method ● The this keyword ● Access control ● Class members, instance members – The static keyword ● Defining constants

pdf14 trang | Chia sẻ: candy98 | Lượt xem: 408 | Lượt tải: 0download
Bạn đang xem nội dung tài liệu Object-Oriented Programming - Lecture 3: Classes and Objects - Lê Hồng Phương, để tải tài liệu về máy bạn click vào nút DOWNLOAD ở trên
Lecture 3: Classes and Objects Lê Hồng Phương phuonglh@gmail.com Department of Mathematics, Mechanics and Informatics, Vietnam National University, Hanoi 2012-2013 Object-Oriented Programming: Classes and Objects 2 Content ● Returning a value from a method ● The this keyword ● Access control ● Class members, instance members – The static keyword ● Defining constants 2012-2013 Object-Oriented Programming: Classes and Objects 3 Returning a value from a method ● A method can return a primitive type or a reference type. public int getArea() { return width * height; } public Bicycle getTheFastest(Bicycle myBike, Bicycle yourBike, Environment env) { Bicycle fastest; // code to calculate which bike is faster, // given each bike's gear and cadence and given the // environment (terrain and wind) return fastest; } A method of the Rectangle class A method of a class modelling a bicycle tournament 2012-2013 Object-Oriented Programming: Classes and Objects 4 Using the this keyword ● The this keyword is a reference to the current object – the object whose method or constructor is being called. public class Point { int x = 0; int y = 0; public Point(int x, int y) { this.x = x; this.y = y; } } 2012-2013 Object-Oriented Programming: Classes and Objects 5 Using the this keyword ● Within a constructor, use the this keyword to call another constructor in the same class. public class Rectangle { private int x, y; private int width, height; public Rectangle() { this(0, 0, 0, 0); } public Rectangle(int width, int height) { this(0, 0, width, height); } public Rectangle(int x, int y, int width, int height) { this.x = x; this.y = y; this.width = width; this.height = height; } // ... } 2012-2013 Object-Oriented Programming: Classes and Objects 6 Controlling access ● Access levels determine whether other classes can use a particular field or invoke a particular method. ● Two levels of access control: – Top level: public, or package-private (no explicit modifier) – Member level: public, private, protected, or package-private (no explicit modifier) 2012-2013 Object-Oriented Programming: Classes and Objects 7 Controlling access ● A class may be declared with the modifier public, that class is visible to all classes everywhere. ● If a class has no modifier (the default, also known as package-private), it is visible only within its own package. public class A { //... } class B { //... } This class is visible to all classes everywhere. This class is visible only winthin its package. 2012-2013 Object-Oriented Programming: Classes and Objects 8 Controlling access ● For members of a class, there are two additional access modifiers: private and protected. – private: the member can only be accessed in its own class. – protected: the member can only be accessed ● within its own package (as with package- private) and ● by a subclass of its class in another package. 2012-2013 Object-Oriented Programming: Classes and Objects 9 Controlling access public class BankAccount { private String accountNumber; private double balance; //... } These private fields are accessible only within the methods of this class. class Thief { //... void foo() { BankAccount account = new BankAccount(); account.balance = 0; } } Is it possible? 2012-2013 Object-Oriented Programming: Classes and Objects 10 Tips on choosing an access level ● Use the most restrictive access level that makes sense for a particular member. – Use private unless you have a good reason not to. – Avoid public fields except for constants. ● Note: – Some examples given in lectures use public fields. This helps to illustrate some points concisely but is not recommended for production code. – Public fields limit your flexibility in changing your code. 2012-2013 Object-Oriented Programming: Classes and Objects 11 Instance members, class members ● Suppose that we want to create a number of Car objects and assign each a serial number. – The first car has id 1, the next is 2... – This id number is unique to each object. ● We need to keep track of how many Car objects that have been created. ● This information is not related to any objects but to the class as a whole. 2012-2013 Object-Oriented Programming: Classes and Objects 12 Instance members, class members public class Car { private String registrationNumber; private static int numberOfCars = 1; public Car(String number) { registrationNumber = number; numberOfCars++; } // ... } This is a class member. All Car objects share this field. Car.numberOfCars This is an instance member. Each object has its own registration number. 2012-2013 Object-Oriented Programming: Classes and Objects 13 Instance members, class members ● If a method uses a static field, it must be declared as static. ● Why the main() method is declared static? public static int getNumberOfCars() { return numberOfCars; } 2012-2013 Object-Oriented Programming: Classes and Objects 14 Defining constants ● The static modifier, in combination with the final modifier, is used to define constants. ● The final modifier indicates that the value of this field cannot change. static final double PI = 3.141592653589793; static final int MAX_VALUE = 1000;