Java knows how many (7) classes and objects

Source: Internet
Author: User
Tags ming types of functions

Java is an object-oriented programming language, Understanding Java, first of all to understand the concepts of classes and objects.

Classes in Java can be seen as an upgraded version of the struct in the C language. A struct is a constructed data type that can contain different members (variables), each of which can have a distinct data type, and structure variables can be defined by struct, each with the same properties. For example:

1#include <stdio.h>2 intMain () {3 //define structure Body Student4 struct student{5 //struct-contained variables6 Char*name;7 intAge ;8 floatscore;9 };Ten //defining variables through structs One struct Student stu1; A //members of the manipulation structure body -Stu1.name = "Xiao Ming"; -Stu1.age = 15; theStu1.score = 92.5; -printf ("Age of%s is%d, score is%f\n", Stu1.name, Stu1.age, stu1.score); - return0; -}

Operation Result:

Xiaoming's age is 15, the score is 92.500000.

Classes in Java are also a type of construction data, but some extensions are made, and the members of a class can be not only variables, but also functions, and variables defined by classes have specific names called "Objects". For example:

1  Public classDemo {2  Public Static voidMain (string[] args) {3 //Defining class Student4 classstudent{//defining classes with class keyword classes5 //the variables that the class contains6 String name;7 intAge ;8 floatscore;9 //the functions that the class containsTen voidsay () { OneSYSTEM.OUT.PRINTLN (name + "is" + Age + "and the score is" +score); A } - } - //define variables by class, that is, create objects theStudent STU1 =NewStudent ();//You must use the New keyword - //members of the action class -Stu1.name = "Xiao Ming"; -Stu1.age = 15; +Stu1.score = 92.5f; - Stu1.say (); + } A}

Operation Result:
Xiaoming's age is 15, the score is 92.5.

In c language, the definition of struct variable can be done by struct name, and the memory space is allocated, but in Java, it is necessary to use the New keyword to complete the allocation of memory space by simply defining a variable with a class instead of allocating memory space.

The class can be likened to a drawing, the object is likened to a part, the drawings explain the parameters of the parts and the tasks undertaken; A drawing can produce parts of the same nature, and different drawings can produce different types of parts.

In Java, using the New keyword, you can create objects from a class, producing a part of a drawing, a process called instantiation of a class, and therefore also known as an instance of a class.

Note: The class is just a drawing, play the role of the description, do not occupy memory space, the object is the specific parts, to have a place to store, will occupy the memory space.

A class contains variables and functions that have a specific salutation, a variable called a property (often called a member variable), a function called a method, and a property and a method are collectively referred to as members of a class.

OO programming (Object Oriented programming, OOP)

Classes are a common concept, and there are classes in Java, C + +, C #, PHP, and many other programming languages that can create objects from a class. Classes can be seen as an upgraded version of the structure, C-language juniors see the lack of C language, try to improve, inherit the idea of the structure, and upgrade, so that programmers in the development or expansion of large and medium-sized projects easier.

Because languages such as Java and C + + support classes and objects, writing programs using these languages is also known as Object-oriented programming, which is also known as an object-oriented programming language. The C language is called a process-oriented programming language because it does not support the concept of classes and objects.

In fact, object-oriented is just a process-oriented upgrade.

In the C language, you can define a reusable block of code that accomplishes a feature as a function, declaring a function with a class of functions in a header file, with different types of functions being declared in different header files, so that the functions are better managed and easier to write and invoke.

In Java, you can define a block of code that accomplishes a function as a method, define a method with similar functionality in a class, which is defined in a source file (because a source file can contain only one common class), and multiple source files may be located in a folder that has a specific salutation, called a package.


Figure 1 Organizational structure of the project in C language


Figure 2 Organizational structure of projects in Java


Object-oriented programming has absolutely no advantage in software execution efficiency, its main purpose is to facilitate the programmer to organize and manage the code, quickly comb the programming ideas, bring about the innovation of the programming thought.

Series Articles:

Java know how much (1) Language overview

Java know how much (2) virtual machine (JVM) and cross-platform principle

Java know how much (3) employment direction

Java know how much (4) the difference between J2SE, Java EE, J2ME

Java know how much (5) Build Java development environment

Java know how much (6) The first example of a program

Java knows how many (7) classes and objects

Java knows how many (7) 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.