Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 46 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
46
Dung lượng
652,44 KB
Nội dung
CHAPTER Java Programming Basic Contents • • • • • • • • • • • INTRODUCTION JAVA TYPES ARRAYS JAVA IDENTIFIERS BASIC CONTROL STRUCTURES OBJECT-ORIENTED PROGRAMMING CLASSES AND OBJECTS OBJECT STATE AND BEHAVIOR INSTANCE AND STATIC VARIABLES AND METHODS Inheritance POLYMORPHISM NNL – Khoa Toán Tin ĐHKHTN INTRODUCTION • Java is one of a great many computer programming languages in use today • Having been first released in 1994 by Sun Microsystems, its design takes advantage of modern ideas about what makes a good programming language • Java enjoyed unprecedented growth in popularity from the very day of its release NNL – Khoa Toán Tin ĐHKHTN JAVA TYPES • Java is an object-oriented language, which operates on software objects Object orientation means that the programmer can define a new type of data element by defining a new class • Having defined a new class, the programmer can create an example object of the class and manipulate it as a unique object NNL – Khoa Toán Tin ĐHKHTN JAVA TYPES • But Java programmers need some basic data types to get started These Java primitive types are not objects, but are simply the definitions of the varieties of data with which all Java programs can work The primitive types fall into three categories: integral types floating-point types boolean type integers and characters fractional numbers true or false values NNL – Khoa Toán Tin ĐHKHTN JAVA TYPES integral types byte short int long char bits wide 16 bits wide 32 bits wide 64 bits wide 16 bits wide −128 to 127 −32768 to 32767 −2 billion to billion −263 to (263 −1) “Unicode” character codes NNL – Khoa Toán Tin ĐHKHTN JAVA TYPES floating-point types float 32 bits wide +/− 3.4 × 1038 with 6–7 significant decimal digits double 64 bits wide +/− 1.8 × 10308 with 14–15 significant decimal digits boolean type – boolean logical true or false NNL – Khoa Toán Tin ĐHKHTN JAVA TYPES - EXAMPLE public class Primitive { public static void main( String[] args ) { int x; int y; int z; y = 7; z = 4; x = y + z; System.out.println( "x = " + x ); } } NNL – Khoa Toán Tin ĐHKHTN JAVA TYPES • Sometimes you will find it necessary to operate on objects instead of primitive types This is because objects are reference types, and are handled differently internally than primitive types • For the purpose of converting variables of primitive types to reference types, Java provides a set of “wrapper classes” so that the programmer can always create an object having the same value as a corresponding variable of a primitive type NNL – Khoa Toán Tin ĐHKHTN JAVA TYPES - Primitive Wrapper Class byte short int long char float double boolean Byte Short Integer Long Character Float Double Boolean NNL – Khoa Toán Tin ĐHKHTN 10 INSTANCE AND STATIC VARIABLES AND METHODS • On the other hand, we would maintain a count of the total number of Automobiles as part of the state of the class Automobile It seems natural that speed should be associated with a particular car, but we need a central place to keep track of the total number of Automobiles NNL – Khoa Toán Tin ĐHKHTN 32 INSTANCE AND STATIC VARIABLES AND METHODS • Variables like speed, which represent the state of an instance of a class, are called instance variables Variables like count, which are maintained by the class itself, are called static variables • Similarly, methods can be instance methods or static methods If two different instances of a class both call the instance method accelerate(), it will be as if there were two different copies of that method, and each instance had its own (In reality, there will not be two copies of the code) NNL – Khoa Toán Tin ĐHKHTN 33 INSTANCE AND STATIC VARIABLES AND METHODS • On the other hand, if two different instances of a class call the static method getAutomobileCount(), the two instances will be using exactly the same code, because there is only one copy, and that is shared • Variables and methods will always be instance variables and methods, unless you specifically label them static If you label a variable or method static, the variable or method will exist only at the level of the class, and will be shared among the instances NNL – Khoa Toán Tin ĐHKHTN 34 Example class Automobile { //static variable private static int count; //count of automobiles //instance variables private String make; private String model; private int year; private int hp; private double speed; //Constructor Automobile( String mk, String mdl, int yr, int power ){ make = mk; //assign constructor parameters model = mdl; // to private instance variables year = yr; hp = power; count++; //add to count of Automobiles created } NNL – Khoa Toán Tin ĐHKHTN 35 Example class AutomobileFactory { public static void main( String[] args ) { Automobile economy, family, sports; economy = new Automobile( "Kia", "Rio", 2006, 110 ); family = new Automobile( "VW", "Passat", 2002, 170 ); sports = new Automobile( "Ford", "Mustang", 2005, 300 ); System.out.println( Automobile.getCount() + " Automobiles" ); System.out.println( economy ); System.out.println( family ); System.out.println( sports ); } } NNL – Khoa Tốn Tin ĐHKHTN 36 Inheritance • OO programming makes it easy to add functionality to software without rewriting the code one has already written and tested Suppose we want to add distinctions between types of Automobiles A Ferrari can go much faster than a Kia, so the accelerate() method should be different, and perhaps other behaviors should also be different for such different Automobiles • We can add a new class called SportsCar that will inherit from Automobile, and we can give the SportsCar class a different accelerate() method NNL – Khoa Toán Tin ĐHKHTN 37 Inheritance class SportsCar extends Automobile { private double maxSpeed = 150.; //constructor SportsCar( String mk, String mdl, int yr, int power ) { super( mk, mdl, yr, power ); } //override of inherited accelerate() method public void accelerate( double newSpeed ) { if( newSpeed > maxSpeed ) speed = maxSpeed; else speed = newSpeed; } } NNL – Khoa Toán Tin ĐHKHTN 38 Inheritance • The SportsCar class extends the class Automobile; that means SportsCar inherits from Automobile Any instance of a SportsCar will also be an Automobile, and except where there are differences between the code for SportsCar and the code for Automobile, an instance of a SportsCar will have exactly the same state variables and behavior as any instance of the class Automobile NNL – Khoa Toán Tin ĐHKHTN 39 Inheritance • The only difference between instances of SportsCar and instances of Automobile will be the behavior provided by the accelerate() method NNL – Khoa Toán Tin ĐHKHTN 40 POLYMORPHISM • When a subclass overrides a method of a superior class, the behavior of the method will depend upon which type of object is being used in the program, an instance of the superior class or an instance of the subclass This characteristic of OO programming is called polymorphism, and it is a powerful feature of the OO approach NNL – Khoa Toán Tin ĐHKHTN 41 class AutomobileFactory { // family = new Automobile( "VW", "Passat", 2002, 170 ); sports = new SportsCar( "Ford", "Mustang", 2005, 300 ); // //same method call to instances of different classes family.accelerate(120 ); sports.accelerate(120 ); //polymorphism will cause the effects to be different System.out.println( family + " " + family.getSpeed() ); System.out.println( sports + " " + sports.getSpeed() ); } NNL – Khoa Tốn Tin ĐHKHTN 42 POLYMORPHISM • This will be the new output: Automobiles 2006 Kia Rio 2002 VW Passat 2005 Ford Mustang 2002 VW Passat 70.0 2005 Ford Mustang 120.0 NNL – Khoa Toán Tin ĐHKHTN 43 POLYMORPHISM • Polymorphism allows us to write programs in a more general way We can reference the superior class as we write programs, and if the object being used by the program happens to belong to a subclass of the superior class, the methods in the subclass which override the corresponding methods in the superior class will insure that methods appropriate to the particular instance will be called NNL – Khoa Tốn Tin ĐHKHTN 44 POLYMORPHISM • This is another way in which OO programming reduces the amount of code that must be written and tested, and also promotes reuse of existing code in new applications • We will cover Inheritance, polymorphism and interface in more details in the following chapters NNL – Khoa Toán Tin ĐHKHTN 45 Examples • The Fraction class • The Animal abstract class and its subclasses NNL – Khoa Toán Tin ĐHKHTN 46