Classes, objects, and packages

Source: Internet
Author: User

1) Object-oriented programming (object oriented programming, short for OOP): popular since the 1970s.

2) Differences between structured programming and object-oriented programming:

A. In structured programming, the program is designed mainly around the task to be solved. Writing a program is the process of writing a specific task, and the data that needs to be used in the process is passed through the process parameters to the process. The process can view and change the data passed in, and can return the value to the process that called it.

B. Oop is primarily designed around the object of the problem being solved, and for each object we write a class that describes the properties and behavior of the object.

3) What are objects and classes?

A. Object: "Everything is Object", the object is composed of attributes and behavior, the property is the characteristics of the object, the behavior is that the object can do the action.

B. Class: A class is a collection of objects that have the same properties and behaviors. Each property of an object is represented as a member variable in a class, and each behavior becomes a method in the class.

C. Relationship: A class is an abstraction (description) of an object that is an instance of a class.

4) member variables and local variables

A. Member variable: A variable declared in a class that has a default value that can be used everywhere in a class.

The initial value of the member variable of an object
data type of member variable Initial value
byte 0
Short 0
int 0
Long 0
float 0.0
Double 0.0
Char NULL
Boolean flase

A. Member Variable components:

-Access modifiers (public, private, protected protected, default)

-Data type

-member variable name (must be a valid identifier). End with a semicolon
For example:
/**
* Student
 *
* @author Administrator
 *
 */
Public class Student {

/ * Attribute member variable * /
Public String name = null;//Name
public String sex;//Gender
public int ages;//Age
Public String Stuno;//School Number
}

B. Local variables: Variables declared in a method, without default values, are only useful in one method.

B. Part of the local variables:

-Access Modifiers

-Return value

-Method name (must be a valid identifier)

-parameter list, which appears in parentheses

For example:

/* Method */

access modifier return value type method name ([parameter list]) {
method Body-The function code block to be implemented
     }    
     */
Public Void Introduce () {
System.out.println (this.tostring ());
System.out.println ("Name:" + name + "\ n Sex:" +
sex + "\ n Ages:" + Age +
"\ n Study No.:" + Stuno);
    }

5) How do I create objects and access properties and methods of objects?

Object Object name =   New object ();

Object name. property = ...;

For example:
import Java.util.Scanner;
Public class Studenttest {
Public static void Main (string[] args) {
Scanner input = new Scanner (system.in);
/ * Create student objects * /
Student stu = new Student ();
stu.introduce ();
//Assign values to object properties
//How to invoke the object's Property object name. property name
System.out.print ("Please enter Name:");
stu.name = Input.next ();
System.out.print ("Please enter Gender:");
stu.sex = Input.next ();
//method of invoking the object, implementing self-introduction
//Object name. Method name ([argument list]);
System.out.println ("********************");
stu.introduce ();
    }
}

6) This reference: Use this when the name of the authority variable is the same as the member variable. (this refers to the current object)

7) Package (keyword: package): The packet declaration must be the first statement in the source code file other than the comment.

For example:

package com.lovo;

public class Studenttest {
Span style= "color: #ff0000; Font-family: in italics; font-size:16px; " >    public static void Main (string[] args) {
        student Student = new Student ("0051", "Faye Wong");
         Student.display ();
"

A. Use:

A. Provides a mechanism for organizing classes. You can also prevent class naming collisions.

B. Provides a namespace for the classes in the package.

B. Import the classes in other packages using the Import keyword:

A. Use the full name of the class. For example: Pay. Employee;

B. Import the package using the keyword import and the wildcard character (*). For example: Import pay.*;

C. Import the class itself using the keyword import. For example, import pay. Employee;

Classes, objects, and 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.