Java: Learning java from cainiao to stupid bird -- classes and objects

Source: Internet
Author: User

There is an old saying in China: People gather together to group objects. java should be said to come to life rather than life. Therefore, we can also use this old Chinese sentence to Understand java.
When talking about java, we should first talk about classes and objects. This is two clues from start to end in java, just like two male and female in a TV series.
Java is a realistic language. Therefore, we can compare classes and objects into design drawings and finished products. Classes are the blueprint, objects are products designed according to the blueprint. Therefore, in a program, we can only use classes to generate objects, but only objects can be operated directly.
1.1 categories
Class is used to describe a group of objects with the same features:
· Corresponding features should be included
· Behavior Characteristics
Therefore, the class contains two contents:
· Attribute: used to describe the data elements of an object;
· Method: perform operations on object attributes;
Declaration of the 1.2 class
[<Modifier>] class <class Name> {// brackets indicate that they can be omitted, and angle brackets indicate that they are required.
Class attributes;
Constructor;
Class method;
}
Statement of the lifecycle attribute:
[<Modifier>] <data type> <attribute name>
Statement of the restore method:
<Modifier> <return type> <Method Name> ([<parameter list>]) {
Method body
}
Pipeline constructor:
<Modifier> <Class Name> ([<parameter list>]) {
Method body
}
1.3; display column: Definition of Student Class
[Java]
Package java. duan. test;
 
 
Public class Student {// define a Student class
 
// Define attributes
 
Private String name;
 
Private String sex;
 
Private int age;
 
Private int grade;
 
// Define the method for obtaining the attribute name
 
Public String getName (){
 
Return name;
 
}
 
// How to set the attribute name
 
Public void setName (String name ){
 
This. name = name;
 
}
 
// Define the method for obtaining the property sex
 
Public String getSex (){
 
Return sex;
 
}
 
// Define how to set the property sex
 
Public void setSex (String sex ){
 
This. sex = sex;
 
}
 
// Define the method for obtaining the attribute age
 
Public int getAge (){
 
Return age;
 
}
 
// Define how to set the attribute age
 
Public void setAge (int age ){
 
This. age = age;
 
}
 
// Define the method for obtaining the property sex
 
Public int getGrade (){
 
Return grade;
 
}
 
// Define the method for obtaining the property sex
 
Public void setGrade (int grade ){
 
This. grade = grade;
 
}
 
 
}

 
1.4 Constructor
In java, each class must have at least one constructor, but you will find that the constructor is not defined in student, but the program still has no errors. This is because, if no constructor is defined in the program, the compiler automatically adds a constructor without any parameters. If the constructor is defined in the class, the program calls this constructor and the default constructor does not work.
Feature: The method name must be the same as the class name.
For example:
 
[Java]
Package java. duan. test;
 
 
Public class Dog {
 
Public int weight;
 
Public Dog (int dog_weight ){
 
 
Weight = dog_weight;
 
}
 
Dog dog1 = new Dog (20); // if it is changed to Dog dog1 = new Dog (); then an error occurs because the default constructor has no function;
 
}

 
1.5 create and use an object
After defining the attributes and methods of a class, you can use the new + constructor to create an object;
For example, you can use the following method to create student1, An Instance Object of the Student class:
Student student1 = new Student ();
The created object can call the attributes and methods in the class, for example:
[Java]
Public static void main (String [] args ){
 
Student student1 = new Student ();
 
Student1.setAge (19 );
 
System. out. println ("student age:" + student1.getAge ());
 
}

 
The same is true for other method calls. Note that for methods and Properties of private attributes, objects in different classes cannot be called directly and can only be called indirectly through other methods;
In general, classes and objects are easy to understand and learn;

Author: xyz121321043

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.