Head first Java--the behavior of objects

Source: Internet
Author: User

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

Call a method of two arguments and pass in two parameters

void Go () {

Teststuff t = new Teststuff ();

T.taketwo (12,34);

}

void TakeTwo (int x,int y) {

int z = x + y;

SYSTEM.OUT.PRINTLN ("Total is" + Z);

}

You can also pass the variable as a parameter, as long as the type matches

void Go () {

int foo = 7;

int bar = 3;

T.taketwo (Foo,bar);

}

void TakeTwo () {

int z = x + y;

SYSTEM.OUT.PRINTLN ("Total is" + Z);

}

Getter and Setter

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

Class Electricguitar {

String brand;

int numofpickkups;

Boolean rockstarusesit;

String Getbrand () {

return brand;

}

void Setbrand (String abrand) {

Brand = Abrand;

}

int getnumofpickups () {

return numofpickups;

}

void setnumofpickups (int num) {

numofpickups = num;

}

Boolean getrockstarusesit () {

return rockstarusesit;

}

void Setrockstarusesit (Boolean Yesorno) {

Rockstarusesit = Yesorno;

}

}

Packaging

Package Gooddog

Class gooddog{

private int size; //Set instance variable to private

  

public int GetSize () { //getter and setter are set to public//Although the secondary method does not add substantive functionality, the most important thing is to allow you to change your mind after the fact, you can go back to make the program more secure, Better.

return size;

}

public void setSize (int s) {

size = s;

}

}

void Bark () {

if (Size > 60) {

System.out.println ("wooof!wooof!");

} else if (Size > 14) {

System.out.println ("ruff! Ruff! ");

} else {

System.out.println ("yip! Yip! ");

}

}

Class gooddogtestdrive{

public static void Main (String [] args) {

Gooddog one = new Gooddog ();

One.getsize (70);

Gooddog = new Gooddog ();

Two.getsize (8);

System.out.println ("Dog One:" + one.getsize ());

System.out.println ("Dog:" + two.getsize ());

One.bark ();

Two.bark ();

}

}

In addition, any value can be applied to a place where the method can be called to obtain the value of that type:

int x = 3 + 24;

Can be written as: int x = 3 + one.getsize ();

Behavior of objects in arrays

Declares a dog array containing 7 dog references

Dog [] pets; //Create an array of dog classes named Pets

Pets = new Dog[7]; //Description This name is called Pets array there are several objects;

Then, create two dog objects and assign them to the first two elements of the array

Pets[0] = new Dog ();

PETS[1] = new Dog ();

Methods to invoke these two dog objects

Pets[0].setsize (30);

int x = Pets[0].getsize ();

Comparison of variables

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

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

Foo a = new foo ();

Foo b = new Foo ();

Foo C = A;

if (a = = b) {//False}

if (b = = c) {//false}

if (a = = c) {//true}

Head first Java--the behavior of objects

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.