Java Learning Notes (iii) classes, objects, packages

Source: Internet
Author: User

In object-oriented programming, the program is not designed around the task to be solved, but rather around the object of the problem to be solved.

Class: Is a collection of objects of the same property and behavior that contains the properties and methods of the class, but not all classes must contain properties and methods.

Example: To write a student class, students have a school number, name, age, gender and so on.

/**

* Student Class

*/

public class student{

/* Properties */

public int Stuno;

public String name;

public int age;

public String sex;

/* Method */

public void print () {

System.out.println ("Study No.:" +stuno+ "Name:" +name+ "Age:" +age+ "Sex:" +sex ");

}

}

When writing programs with software, packages are usually created under the project. keyword, package add-on name

Naming conventions for packages: naming with an inverted organization domain name, example: Www.baidu.com package Name: Com.baidu.dept (department name). HR (project name)

The declaration of a package must be the first executable statement placed in the source file.

Object: Everything is object.

A class is an abstraction of an object that is an instance of a class.

Instantiate the object's keyword new, example: Student stu (alias) = new Student (); Stu.stuno=1 when assigning a value to the properties of a class;

Property into a member variable, the method becomes a local variable, and when the member variable in the method is the same as the local variable name, the reference member variable is added before the variable name (this.)

In a class, a member variable is declared, the system automatically assigns it a value, the integer type, the float type is 0, the character type, the reference type is NULL, the Boolean is false, the local variable in the method is not automatically assigned to the value itself, and the local variable is only available at the declaration.

Java Learning Notes (iii) classes, objects, packages

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.