Analysis of the object (a)

Based on JAVA Analysis of the object (a)

 

     For objects ( Object what) is a good idea to understand class ( class what) is

 

1.class:   class is a template that describes the dynamic behavior of a class object. For example, people who would have properties, such as name, gender, age, race, height, weight and so on.

It can be simply described as:

public class Man {

String name;

String sex;

int age;

String race;

int height;

int weight;

}

 

 

 

 People, but also has behaviors such as eating and drinking play and so on.

 It can be simply described as:         

public class Man{

void eat() {};

void drink() {};

void play() {};

}

 

 

 

 

Then the person is defined as a simple class can be:

public class Man {

String name;

String sex;

int age;

String race;

int height;

int weight;

 

void eat() {};

void drink() {};

void play() {};

}

 

 

2. Once you have class, how do we use these attributes?

That is, create an object directly to instantiate (parameterized constructor), or by object calls GET , the SET to achieve the properties ( name, sex, age, race, height, weight ) of operation, you can also use the object to call methods (eat and drink play).

At this simple class described as:

public class Man {

       // property

Private String name;

Private String sex;

int age;

String race;

int height;

int weight;

 

// constructor with no arguments

public Man() {

super();

}

// parameterized constructor

public Man(String name, String sex, int age, String race, int height, int weight) {

super();

this.name = name;

this.sex = sex;

this.age = age;

this.race = race;

this.height = height;

this.weight = weight;

}

 

 

GET // , the SET method

public String getName() {

return name;

}

 

public void setName(String name) {

this.name = name;

}

 

public String getSex() {

return sex;

}

 

public void setSex(String sex) {

this.sex = sex;

}

 

public int getAge() {

return age;

}

 

public void setAge(int age) {

this.age = age;

}

 

public String getRace() {

return race;

}

 

public void setRace(String race) {

this.race = race;

}

 

public  int  getHeight () {

return height;

}

 

public void setHeight(int height) {

this.height = height;

}

 

public int getWeight() {

return weight;

}

 

public void setWeight(int weight) {

this.weight = weight;

}

       // behavior, eating and playing

void eat() {};

void drink() {};

void play() {};

}

 

For example: To create a named Joe Smith now, sex is male, 33 years old, Chinese people, height 175 , weight 125 people.

So we write a main function:

 

public static void main(String[] args) {

// use constructor directly to create a Joe Smith, this time already own property, and given a value, while Joe Smith is a human subject

Obj = Man new new  Man ( " Joe Smith " , " M " , 33, " Chinese people " , 175, 125);

obj.drink();

 

}

     

Let us create an object:

// create an instance (John Doe) using the no-argument constructor, this time only instance of an object with a person's property, but has no value, that is to say with the basket, but no eggs basket

Man obj2=new Man();

// now put the eggs in the basket

obj2.setName ( " John Doe " );

obj2.setRace ( " Japanese " );

Here, and it should be easy to understand the meaning of the object.

It can be understood that the object is a symbol, the symbol represents a specific thing certain things, such as people, then the people of a specific object can be Joe Smith (people), but this symbol has some attributes, behavior. This symbol may be the operator of the properties, behavior.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Main Function Code:

public static void main(String[] args) {

// use constructor directly to create a Joe Smith, this time already own property, and given a value, while Joe Smith is a human subject

Obj = Man new new  Man ( " Joe Smith " , " M " , 33, " Chinese people " , 175, 125);

obj.drink();

System.out.println(obj.getName()+"\t"+obj.getRace());

 

// create an instance (John Doe) using the no-argument constructor, this time only instance of an object with a person's property, but has no value, that is to say with the basket, but no eggs basket

Man obj2=new Man();

// now put the eggs in the basket

obj2.setName ( " John Doe " );

obj2.setRace ( " Japanese " );

System.out.println(obj2.getName()+"\t"+obj2.getRace());

 

}

Guess you like

Origin www.cnblogs.com/huangwenwu/p/11422944.html