JAVA review 3 Java classes and objects

Source: Internet
Author: User

JAVA review 3 Java classes and objects

I wrote two decommissioned articles, so I wrote something useful on my blog. If you want to learn JAVA, you must understand the relationship between classes and objects ,, because a major feature of JAVA is object-oriented ..

Let's take a look at the concepts of classes and objects: (these concepts are annoying in school)

  • Object: An object is an instance of a class and has statuses and behaviors. For example, a dog is an object in the following states: color, name, breed, and behavior: tail shaking, calling, and eating.
  • ClassA class is a template that describes the behavior and status of a class of objects.For example, coding ............ Since a class describes the behavior and status of a class of objects, we select this class of objects as "dogs ". As explained above, a dog is an object in the following states: color, name, and breed. Behavior: Shake the tail, call, and eat. In the code below, I created two objects, namely, two dogs, named "Tom" and "Luxi". One of them is black and the other is white; they are two years old and one year old. Of course they are all dogs, and they all have the behavior of shaking their tails, calling, and eating.
    Public class dog {// defines the age of the dog private static String name; // defines the age of the dog private static int age; // the behavior of the dog running public void run () {System. out. println ("this dog runs fast");} // the behavior of the dog is public void jiao () {System. out. println ("this dog does not like");} // the behavior of the dog shakes its tail public void yaoweba () {System. out. println ("this dog shakes his tail to express his affection");} public static void main (String [] args) {// create a dog object Tomdog = new dog (); // define the name of the dog Tomdog. setName ("Tom"); // defines the age of the dog Tomdog. setAge (2); // output name Tomdog. getName (); // output age Tomdog. getAge (); // defines the behavior of the dog to run Tomdog. run (); // defines the behavior of a dog as Tomdog. jiao (); // defines the behavior of the dog to shake its tail Tomdog. yaoweba (); // create the LuxiSystem object of another dog. out. println ("-------------------------------"); Tomdog. setName ("Luxi"); Tomdog. setAge (1); Tomdog. getName (); Tomdog. getAge () ;}// get set Method public static String getName () {System. out. println ("the name of this dog is:" + name); return name;} public static void setName (String name) {dog. name = name;} public static int getAge () {System. out. println ("the age of this dog is:" + age); return age;} public static void setAge (int age) {dog. age = age ;}}
    Running result:
    The name of this dog is Tom.
    The age of this dog is: 2
    This dog runs fast.
    This dog is not very fond of calling
    The dog shook his tail to express his affection.
    -------------------------------
    The name of this dog is Luxi.
    The age of this dog is: 1

    Next, let's take the steps to create an object:

    The object is created based on the class. In Java, use the keyword "new" to create a new object. To create an object, follow these steps:

    • Statement: Declare an object, including the Object Name and object type.
    • Private static dog luxidog;
      Private static dog tomdog;
    • Instantiation: Use the keyword new to create an object.
    • Tomdog = new dog ("Tom", 2 );
      Luxidog = new dog ("Luxi", 1 );
    • Initialization: When an object is created using new, the constructor is called to initialize the object.
    • Tomdog = new dog ("Tom", 2 );
      Luxidog = new dog ("Luxi", 1 );
      See the following code:
      Public class dog {// defines the age private String name of the dog; // defines the age private Integer age of the dog; // declares the object luxidogprivate static dog luxidog; // declare the object tomdogprivate static dog tomdog; // The constructor public dog (String name, Integer age) {super (); this. name = name; this. age = age; System. out. println ("the name of this dog is:" + name + ", age:" + age);} // the behavior of the dog to run public void run () {System. out. println ("this dog runs fast");} // the behavior of the dog is public void jiao () {System. out. println ("this dog does not like");} // the behavior of the dog shakes its tail public void yaoweba () {System. out. println ("this dog shakes his tail to express his affection");} public static void main (String [] args) {// instantiate initialization Tom dog tomdog = new dog ("Tom", 2); // defines the behavior of this dog to run tomdog. run (); // defines the behavior of a dog as tomdog. jiao (); // defines the behavior of the dog to shake its tail tomdog. yaoweba (); System. out. println ("-----------------------------"); // instantiate the initialization Luxi dog luxidog = new dog ("Luxi", 1); // defines the behavior of this dog to run luxidog. run (); // defines the behavior of a dog as luxidog. jiao (); // defines the behavior of the dog to shake its tail luxidog. yaoweba () ;}// get set Method public String getName () {return name;} public void setName (String name) {this. name = name;} public int getAge () {return age;} public void setAge (int age) {this. age = age ;}}
      The running result is:The name of this dog is Tom, and the age is: 2
      This dog runs fast.
      This dog is not very fond of calling
      The dog shook his tail to express his affection.
      -------------------------------
      The name of this dog is: Luxi, age: 1
      This dog runs fast.
      This dog is not very fond of calling
      The dog shook his tail to express his affection.


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.