您的位置:首页 > 编程语言 > PHP开发

JHTP小结_第三章_类、对象、方法及字符串简介

2016-06-14 21:56 323 查看
 

Summary

Section 3.2 Instance Variables,
setMethods and
getMethods

•Each class you create becomes a new type that can be used to declare variablesand create objects.
• Youcan declare new classes as needed; this is one reason Java is known as anextensible language.
Section 3.2.1
Account Class with an Instance Variable, a
setMethod and a
getMethod

•Each class declaration that begins with the access modifier (p. 71)
publicmustbe stored in a file
that has the same name as the class and ends with the
.javafilenameextension.
•Every class declaration contains keyword
class followedimmediately by the class’s name.
•Class, method and variable names are identifiers. By convention all use camelcase names. Class
names begin with an uppercase letter, and method andvariable names begin with a lowercase
letter.
• Anobject has attributes that are implemented as instance variables (p. 72) andcarried with it throughout its lifetime.
•Instance variables exist before methods are called on an object, while themethods are executing and after the methods complete execution.
• Aclass normally contains one or more methods that manipulate the instancevariables that belong
to particular objects of the class.
•Instance variables are declared inside a class declaration but outside the bodiesof the class’s method
declarations.
•Each object (instance) of the class has its own copy of each of the class’sinstance variables.
•Most instance-variable declarations are preceded with the keyword
private(p.72), which is an access modifier. Variables or methods declared with accessmodifier
private are accessible only to methods of the classin which they’re declared.
•Parameters are declared in a comma-separated parameter list (p. 73), which islocated inside the
parentheses that follow the method name in the methoddeclaration. Multiple parameters are
separated by commas. Each parameter must specify a typefollowed by a variable name.
•Variables declared in the body of a particular method are local variables andcan be used only in
that method. When a method terminates, the values of itslocal variables are lost. A method’s parameters
are local variables of the method.
•Every method’s body is delimited by left and right braces ({and
}).
•Each method’s body contains one or more statements that perform the method’stask(s).
• Themethod’s return type specifies the type of data returned to a method’s caller.Keyword
void indicates that a method will perform a task but will notreturn any information.
•Empty parentheses following a method name indicate that the method does notrequire any parameters
to perform its task.
•When a method that specifies a return type (p. 73) other than
voidiscalled and completes its
task, the method must return a result to its callingmethod.
• Thereturnstatement(p. 74) passes a value from a called method back to its
caller.
•Classes often provide
public methods to allow theclass’s clients to
set or
get privateinstance
variables. The names of these methods need not begin withset
or get,but this naming convention
is recommended.
Section 3.2.2
AccountTest Class That Creates and Uses an Object ofClass
Account

• Aclass that creates an object of another class, then calls the object’s methods,is a driver class.
•ScannermethodnextLine(p.75) reads characters
until a newline character is encountered, then returns the characters as a
String.
•Scannermethodnext(p.75) reads characters
until any white-space character is encountered, then returns the characters as a
String.
• Aclass instance creation expression (p. 75) begins with keyword
newandcreates a new object.
• Aconstructor is similar to a method but is called implicitly by the
newoperatorto initialize an
object’s instance variables at the time the object iscreated.
• Tocall a method of an object, follow the object name with a dot separator (p.76), the method
name and a set of parentheses containing the method’sarguments.
•Local variables are not automatically initialized. Every instance variable hasa default initial value—
a value provided by Java when you do not specify theinstance variable’s initial value.
• Thedefault value for an instance variable of type
String is
null.
• Amethod call supplies values—known as arguments—for each of the method’sparameters. Each
argument’s value is assigned to the correspondingparameter in the method header.
• Thenumber of arguments in a method call must match the number of parameters in themethod
declaration’s parameter list.
• Theargument types in the method call must be consistent with the types of thecorresponding
parameters in the method’s declaration.
Section 3.2.3 Compiling and Executing anApp with Multiple Classes
• Thejavaccommandcan compile multiple classes at once. Simply list the source-code
filenames after the command with each filename separated by a spacefrom the next. If the directory containing
the app includes only one app’s files, you can compileall of its classes with the command
javac *.java. The asterisk (*) in*.javaindicatesthat
all files in the current directory ending with the filename extension “.java”should be compiled.
Section 3.2.4
Account UML Class Diagram with an Instance Variableand
set and

get
Methods

• Inthe UML, each class is modeled in a class diagram (p. 77) as a rectangle withthree compartments.
The top one contains the class’s name centeredhorizontally in boldface. The middle one
contains the class’s attributes, which correspond toinstance variables in Java. The bottom one
contains the class’s operations (p. 78), which correspondto methods and constructors in Java.
• TheUML represents instance variables as an attribute name, followed by a colon andthe type.
•Private attributes are preceded by a minus sign (–) in the UML.
• TheUML models operations by listing the operation name followed by a set ofparentheses. A
plus sign (+) in front of the operation name indicatesthat the operation is a public one in the
UML (i.e., a
public method in Java).
• TheUML models a parameter of an operation by listing the parameter name, followedby a colon
and the parameter type between the parentheses after theoperation name.
• TheUML indicates an operation’s return type by placing a colon and the return typeafter the
parentheses following the operation name.
• UMLclass diagrams do not specify return types for operations that do not returnvalues.
•Declaring instance variables
private is known as data hidingor information hiding.
Section 3.2.5 Additional Notes on Class
AccountTest

• Youmust call most methods other than
main explicitly to tell themto perform their tasks.
• Akey part of enabling the JVM to locate and call method
main tobegin the app’s execution is
the
static keyword, which indicatesthat
main is a
static methodthat can be called without first
creating an object of the class in which the method isdeclared.
•Most classes you’ll use in Java programs must be imported explicitly. There’s aspecial relationship
between classes that are compiled in the same directory.By default, such classes are considered
to be in the same package—known as the default package.Classes in the same package are
implicitly imported into the source-code files of otherclasses in that package. An
import declaration
is not required when one class in a package uses anotherin the same package.
• An
importdeclarationis not required if you always refer to a class with its fully qualified class
name, which includes its package name and class name.
Section 3.2.6 Software Engineering with
private Instance Variables and
public set

and
get Methods

•Declaring instance variables
private is known as data hidingor information hiding.
Section 3.3 Primitive Types vs. ReferenceTypes
•Types in Java are divided into two categories—primitive types and referencetypes. The primitive
types are
boolean,
byte,
char,
short,
int,
long,
floatand
double. All other types arereference
types, so classes, which specify the types of objects,are reference types.
• Aprimitive-type variable can store exactly one value of its declared type at atime.
•Primitive-type instance variables are initialized by default. Variables oftypes
byte,
char, short
int,
long,
floatand doubleareinitialized to
0. Variables of type
booleanareinitialized to
false.
•Reference-type variables (called references; p. 81) store the location of anobject in the computer’s
memory. Such variables refer to objects in the program.The object that’s referenced may
contain many instance variables and methods.
•Reference-type instance variables are initialized by default to the value
null.
• Areference to an object (p. 81) is required to invoke an object’s methods. Aprimitive-type variable
does not refer to an object and therefore cannot be usedto invoke a method.
Section 3.4
Account Class: Initializing Objects withConstructors

•Each class you declare can optionally provide a constructor with parametersthat can be used to
initialize an object of a class when the object iscreated.
•Java requires a constructor call for every object that’s created.
•Constructors can specify parameters but not return types.
• Ifa class does not define constructors, the compiler provides a defaultconstructor (p. 83) with
no parameters, and the class’s instance variables areinitialized to their default values.
• Ifyou declare a constructor for a class, the compiler will
not createa
default constructor for that
class.
• TheUML models constructors in the third compartment of a class diagram. Todistinguish a
constructor from a class’s operations, the UML places theword “constructor” between guillemets
(<< and >>; p. 84) before the constructor’s name.
Section 3.5
Account Class with a Balance; Floating-PointNumbers and Type
double

• Afloating-point number (p. 84) is a number with a decimal point. Java providestwo primitive
types for storing floating-point numbers in memory—floatand
double(p.84).
•Variables of type
float represent single-precision floating-pointnumbers and have seven significant
digits. Variables of type
double representdouble-precision floating-point numbers. These
require twice as much memory as
float variablesand provide 15 significant digits—approximately
double the precision of
float variables.
•Floating-point literals (p. 84) are of type
double bydefault.
•ScannermethodnextDouble(p.88) returns
a double value.
• Theformat specifier
%f (p. 88) is used to output values of type
floator
double.The format specifier
%.2f
specifies that two digits of precision (p.88) should be output to the right of the decimal
point in the floating-point number.
• Thedefault value for an instance variable of type
double is
0.0,and the default value for an instance
variable of type
int is
0.
 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: