Java Review 3 Java classes and objects

Source: Internet
Author: User

There are two articles written in the front, so write something useful on your blog, and if you want to learn Java, you have to understand the relationship between classes and objects, because one of the main features of Java is Object-oriented.

Let's take a look at the concepts of classes and objects: (in fact, they hate these concepts at school)

    • Object : An object is an instance of a class that has state and behavior. For example, a dog is an object whose state is: color, name, variety, behavior: wagging the tail, barking, eating, etc.
    • class : A class is a template that describes the behavior and state of a class of objects.
Let's take an example, coding .....     Since a class is a description of the behavior and state of a class of objects, we select such objects as "dogs". As explained above to the object:A dog is an object, its state is: color, name, variety, behavior: WAG tail, bark, eat and so on. In the code below, I created two objects, two dogs, their names are "Tom" and "Luxi"; their color is black one is white, their age is 2 years old one is 1 years old, of course, they are dogs, all have wagging tail, bark, eat behavior.
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 run public void run () {System.ou T.println ("The Dog Runs Fast"); The behavior of the dog is called public void Jiao () {System.out.println ("This dog does not like to be called");} The behavior of the dog wagging the tail public void Yaoweiba () {System.out.println ("The dog shakes his tail to express his affection"); public static void Main (string[] args) {//Create a Dog object Tomdog Tomdog = new Dog ();//define the name of the dog Tomdog.setname ("Tom");//define the age of the dog T Omdog.setage (2);//Output name Tomdog.getname ();//Output Age tomdog.getage ();//define the behavior of the Dog run Tomdog.run ();//define the behavior of the dog is called Tomdog.jiao () ;//define the behavior of the dog wagging its tail tomdog.yaoweiba ();//Create another Dog object LuxiSystem.out.println ("-------------------------------"); Tomdog.setname ("Luxi"); Tomdog.setage (1); Tomdog.getname (); Tomdog.getage ();} The Get Set method public static String GetName () {System.out.println ("The dog's name is called:" + name); return name; public static void SetName (String name) {dog.name = name;} public static int Getage () {System.out.println ("The Age of the dog is:" + ages); public static void Setage (int.) {dog.age = age;}}
Operation Result:
The dog's name is "Tom."
The age of the dog is: 2
This dog runs fast.
The dog doesn't like to bark.
The dog wagged his tail to express his affection.
-------------------------------
The dog's name is: Luxi
The age of the dog is: 1

just a second. To create an object:

Objects are created from a class. In Java, use the keyword new to create a new object. Creating an object requires the following three steps:

    • declaration : Declares 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);
    • Initialize : When you create an object with new, the constructor method is called to initialize the object.
    • Tomdog = new Dog ("Tom", 2);
      Luxidog = new Dog ("Luxi", 1);
Look at the code below:
public class Dog {//Defines the age of the dog private String name;//defines the age of the dog private Integer age;//declaration object luxidogprivate Static Dog luxidog;//statement Object Tomdogprivate Static Dog tomdog;//constructor public Dog (String name, Integer age) {super (); this.name = Name;this.age = age; System.out.println ("The dog's name is:" + name + ", Age is:" + Ages);} The dog's behavior runs public void run () {System.out.println ("The Dog Runs Fast");} The behavior of the dog is called public void Jiao () {System.out.println ("This dog does not like to be called");} The behavior of the dog wagging the tail public void Yaoweiba () {System.out.println ("The dog shakes his tail to express his affection"); public static void Main (string[] args) {//instantiation initialize Tom Dog tomdog = new Dog ("Tom", 2);//define the behavior of the Dog run Tomdog.run ();//define the behavior of the dog is called Tomdo G.jiao ();//define the behavior of the dog wagging its tail tomdog.yaoweiba (); System.out.println ("-------------------------------");//Instantiate initialize Luxi dog Luxidog = new Dog ("Luxi", 1);// Define the behavior of this dog run Luxidog.run ();//define the behavior of this dog is called Luxidog.jiao ();//define the behavior of the dog wagging his tail luxidog.yaoweiba ();} 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.) {This. Age = Age;}} 
The result of the operation is:The name of the dog is: Tom, age is: 2
This dog runs fast.
The dog doesn't like to bark.
The dog wagged his tail to express his affection.
-------------------------------
The dog's name is: Luxi, age is: 1
This dog runs fast.
The dog doesn't like to bark.
The dog wagged his tail to express his affection.


Java Review 3 Java classes and objects

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.