Home>

Object-oriented is hard to understand when beginners learn java,So today we come to understand this idea for everyone in a very simple way.

First, let's briefly explain the object-oriented thinking.

Object-oriented:

The official language is abstract,We set aside official explanations and definitions.Think about what you have,Correct! !! We ourselves have a series of organs such as hands, feet, eyes, nose and nose.Come to regard our own organs as our attributes,Can I sorrow and laugh and scold,These are not our actions,Then its own attributes plus its own behavior is called an object.

note! !! ourselves,An individual is an object,Because, you are you, I am me, although we have the same,But we are differentFor example you are taller than me,I have longer hair than yours.

Let ’s go further.I and you are both human,Because I have something similar to you,So you and I are human.Human being is the general name of man,It is also an abstraction of similar objects.

From above:I and you are just two special trains of human beings,But aliens can also call us humans,You can see:the concrete manifestation or instance of the class is the object,The abstract or generalization of an object is a class.

We said so simple,How to speak without examples,Below we enter the use of java code to reflect the situation.

Let's write a human class,Note:Objects come from classes,Classes come first, objects come first,Only class instantiation.

public class person {
string name;
int age;
string gender;
public person () {
}
person (string name, int age, string gender) {
this.name=name;
this.age=age;
this.gender=gender;
system.out.println (this.name + "object was created" + ", there is" + this.age + "year-old" + ", is" + this.gender + ")
}
public static void main (string [] args) {
  person p1=new person ("Zhang San", 18, "Male");
  person p2=new person ("Li Si", 19, "Female");
}
}

operation result:

Zhang San object was created,Is 18 years old and is male
Li Si object was created,19 years old and female

This concludes our understanding of object orientation.

class:

We have already mentioned that classes are attributes plus behaviors.The above code already illustrates the properties

To better illustrate the behavior,Let's add the behavior of classes in java ---method

package objectandclass;
import java.awt.secondaryloop;
public class person {
  string name;
  int age;
  string gender;
  public person () {
  }
  person (string name, int age, string gender) {
    this.name=name;
    this.age=age;
    this.gender=gender;
    system.out.println (this.name + "object was created" + ", there is" + this.age + "year-old" + ", is" + this.gender + ")
  }
  public void say () {
    system.out.println ("I said my name is" + this.name + ", don't think I can't speak,I can speak a lot.
");
  }
}

After the class has a method,Objects can also call this method,We claim that at this time the object has some behavioral manifestations of the class.

public static void main (string [] args) {
    person p1=new person ("Zhang San", 18, "Male");
    p1.say ();
    person p2=new person ("Li Si", 19, "Female");
    p2.say ();
  }

Obviously:the result of the run must be:

Zhang San object was created,Is 18 years old and is male
I said my name was Zhang San,Don't think I can't speak,I can speak a lot.
Li Si object was created,19 years old and female
I said my name was Li Si,Don't think I can't speak,I can speak a lot.

Let's talk about some characteristics of classes:

Class, he has his own stuff,There is also something for the object.Class things are members of the class

Class members generally have initialization blocks,Constructors, properties, methods, inner classes, enum classes.

If it belongs to a class (you can use the class name directly.Member calls.) Use static call.

In fact, all kinds of objects can be used.Whether static or not,But what does not need to be statically modified is the object.Can only be called by an instantiated object.

Needless to say about the object,Just remember two things,To create an object,The constructor must be called.

The initialization block can be seen as a special constructor,Passed in without parameters,When creating an object,Will be called anyway.

to sum up

  • Previous Dialog custom context fancy menu in Android
  • Next MySQL declared variables and stored procedure analysis