正在加载图片...
1.2 Java as an Algorithm Language 5 int getMin(PriorityQ pq) tells us that getMin takes one parameter of type (or class)PriorityQ and returns type int. Java has a few primitive rypes and all remaining types are called classes.The primitive types are logical (boolean)and numerical (byte,char,short,int,long,float,and double) types.All classes (nonprimitive types)in Java are reference classes.Behind the scenes, variables declared in classes are"pointers";their values are addresses.Instances of classes are called objects.Declaring a variable does not create an object.Generally.objects are created with a"new"operator,which returns a reference to the new object. The data fields of an object are called instance fields in object-oriented terminology. The binary dot operator is used to access instance fields of an object. Example 1.1 Creating and accessing Java objects For this example,let's assume that date information has the following nested logical structure: 。year ·number ·isLeap month ·day That is,using informal terminology,year is a compound atribute that consists of the boolean attribute isLeap and the integer attribute number.while month and day are simple integer attributes.To reflect this nested structure,we have to define two classes in Java, one for the whole date and another for the year field.Assume we choose the names Date and Year,respectively,for these classes.Then we would declare number and isLeap as instance fields in the Year class and declare year,month,and day as instance fields in the Date class.Moreover,we would most likely define Year as an inner class of Date.The syntax is shown in Figure 1.1. class Date public Year year; public int month; public int day; public static class Year public int number; public boolean isLeap; Figure 1.1 Java syntax for the Date class with an inner Year class
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有