"Head First Java reading notes" (four) the behavior of the object

Source: Internet
Author: User

State affects behavior, behavior affects state

Object has state and behavior

The class describes what the object knows and what it does.

Can each object of the same type have different method behavior?

Each instance of any class has the same method, but the method can behave differently depending on the value of the instance variable.

For example, song class has the title instance variable, different instances can call the play () method, but will play different songs according to the title.

Method parameter, you can pass the value to the method

The method uses the formal parameter, and the calling party passes in the argument.

An argument is a value passed to a method and becomes a formal parameter when it is passed into the method. parameters, like local variables, have types and names that can be run within a method.

take the return value from the method

A method can have a return value, and each method declares the type that is returned.

If a method declares a return value, it must return the value of the declared type.

Java is throughvaluePassed, that is to say, byCopyPassed

Method cannot change the arguments passed in by the caller

Q&a:

What if the parameter you want to pass in is an object other than the primitive master data type?

Everything passed in Java is referred to, but this value is the value that the variable carries. Also, variables that reference objects carry remote control instead of the object itself. If you pass parameters to the method, you are actually passing in a remote controlled copy.

Can a method declare multiple return values? Is there any other way to return multiple values?

Method can only declare a single return value. You can use an array to return multiple values

Key points:
    • The class definition object is known and done.
    • The object is known as an instance variable.
    • The object is the method.
    • Method can show different behavior according to the instance variable
    • Method can use parameters, which means that you can pass in one or more values to the method
    • The arguments passed to the method must conform to the number, order, and type of the declaration.
    • The value types that are passed in and out of a method can be implicitly or explicitly narrowed.
    • Method must declare the return type. Using the void type means that the method does not return anything.
    • If a method declares a return type that is not void, it is important to return the same value as the declared object type.
using parameters and return types

Getter and setter (accessor and mutator), getter and setter allows you to perform get and set. The purpose of the Getter is only one, which is to return the value of the instance variable, and the purpose of the setter is to take a parameter to set the value of the instance variable.

Package (encapsulation)


Exposure means that it can be accessed by the dot operator, such as:

Tehcat.height = 27;

Modify the cat instance variables directly through remote control.

Data hiding

Use both public and private access modifiers (access modifier)

The following is the basic principle of encapsulation: Mark your instance variable as private and provide the public getter and setter to control the access action.

To mark an instance variable as private

Mark Getter and setter as public

Package Gooddog
 Public classGooddog {Private intsize;  Public intGetSize () {returnsize; }     Public voidSetSize (intsize) {         This. Size =size; }    voidbark () {if(Size > 60) {System.out.println ("Wooof! wooof! "); } Else if(Size > 14) {System.out.println ("Ruff! ruff! "); } Else{System.out.println ("Yip! yip! "); }    }}
 Public class Gooddogtestdriver {    publicstaticvoid  main (string[] args) {         New Gooddog ();        One.setsize (+);         New Gooddog ();        Two.setsize (8);        System.out.println ("Dog One:" + one.getsize ());        System.out.println ("Dog:" + two.getsize ());        One.bark ();        Two.bark ();    }}

Results:

Dog one:70
Dog Two:8
wooof! wooof!
yip! yip!

Declaring and initializing instance variables

A variable declaration requires at least a name and type and can also initialize (assign) a variable, but what happens if you do not have an initial instance variable and call the getter? What is the value of the instance variable before initialization?

 Public classPoordog {Private intsize; PrivateString name;  Public intGetSize () {returnsize; }     Public voidSetSize (intsize) {         This. Size =size; }     PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }} Public classpoordogtestdrive { Public Static voidMain (string[] args) {Poordog one=NewPoordog (); System.out.println ("Dog size is" +one.getsize ()); System.out.println ("Dog size is" +one.getname ()); }} Run Result: Dog size Is0dog size IsNull

Instance variables will always have a default value, even if there is no definite assignment to the instance variable, or if the setter method is not called, the instance variable will still have a value

Integer 0;

Floating points 0.0

Booleans false

References null

the difference between an instance variable and a local variable

1. Instance variables are declared within a class and not in a method

2. Local variables are declared in the method.

3. Local variables must be initialized before they are used (local variables have no default values!) if the variable is initialized before it is used, the compile time will display an error.

Q&a

What about the parameters of the method? Do the rules for local variables apply to them?

The parameters of the method are basically the same as the local variables, and they are all declared in the method. The argument does not have an undeclared problem, so the compiler will not be able to show errors on such things. If the method is called without an assignment parameter, the compiler displays an error. So the parameters are bound to be initialized, and the compiler will ensure that the method is called with parameters that match the declaration, and that the parameters are automatically assigned to them.

Comparison of variables (primitive master data type or reference)

Use = = to compare two primitive master data types, or to determine whether two references refer to the same object.

Use Equals () to determine whether two objects are equal in meaning.

"Head First Java reading notes" (four) the behavior of the object

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.