Class and Object

Source: Internet
Author: User

Class and Object

Object-oriented and process-oriented explanation

We often say that advanced languages are divided into process-oriented and object-oriented.

Process-oriented: c

Object-oriented: c ++, Java

Common aspect: process-oriented and object-oriented are both specific ideas for solving practical problems.

Differences:

Let's describe the following two things:

How to drive a car (things are relatively simple and can be solved with linear thinking)

Process-oriented:1. Collection clutch 2. Archive 3. step on the accelerator and put on the clutch 4. Open

Object-oriented:

Driver

2. Automobile

The driver drove the car! Motor. start ();

How to Create a car (things are complicated and cannot be solved using simple linear thinking)

Process orientation: 1. Build wheels 2. build engines 3. Build car skins 4. windshields...

Object-oriented:

Wheel

Buy Rubber

To malaysia

Buy plane ticket

Find Rubber Factory

Pay-as-you-go

Ship Rubber to China

Abrasive Tools

Place rubber into Abrasive Tools

Outbound Wheel

Engine

....

Car Shell

....

Seat

...

Windshield

....

Assemble and create a car!

To describe complex things, we need to use an object-oriented approach to analyze the entire system in order to grasp and analyze the complex things as a whole. However, specific micro-operations may require process-oriented approaches.

To solve simple problems, you can use process-oriented solutions to solve complex problems: at the macro level, you can use object-oriented methods, while process-oriented methods are still used in detail processing.

Object-oriented Thinking: in case of a complex problem, first find the nouns in the question, then determine which nouns can be used as classes, and then determine the attributes and methods of the classes based on the problem requirements, determine the relationship between classes

1. Concepts of objects and Classes

There is a famous saying in java that everything is an object. An object refers to a specific thing.

For example, a person named James is an object, and a class is an abstract object.

Human beings are a class. James is an instance of human beings, that is, an object. Other human beings may also be other examples, such as Xiao Li and Xiao sun.

If you make a car a class, the object is my house's Buick car, the object can also be your home's Maserati car, the object is the specific thing, and the class is the collection of objects, all objects collectively referred

A class can be seen as a template or drawing. The system creates an object according to the class definition.

For example, a car model has four wheels, engines, seats, and can run.

An object of a car can be your car, with four wheels, an engine, or a running engine. It can also take you for a real example.

Class Definition

In java, classes are defined by class names.

1 class Car{2     3 }

For example, this Car class

The above Car class is blank and attributes can be defined. The running methods can be defined for wheels, engines, and seats.

Class Car {String name; int age; String color; String zuowei;
Void run (){
System. out. println ("I am a car, I can take people to run ");
}}

The property, car name, age, color, seat, and running method are defined above.

After creating a class, you should create an object to get your car through the car class.

Class Name object name = new Class Name ()

Class Car {String name; int age; String color; String zuowei; void run () {System. out. println ("I am a Car, I can run, but also can take people to run");} public static void main (String [] args) {car Car = new car (); Car. name = "Best BMW" car. age = 3; car. color = "black"; car. zuowei = "leather"; car. run ();}}

The name of your car is the best BMW. It has been three years old and black. You have been converted into a leather sofa and are still running.

In general, in addition to attributes and methods, a constructor or constructor is included in a class. You can assign values to attributes when creating an object, which is equivalent to Directly introducing the object to yourself.

Class Car {String name; int age; String color; String zuowei; public Car (String name, int age, String color, String zuowei) {this. name = name; this. age = age; this. color = color; this. zuowei = zuowei;} void run () {System. out. println ("I am a Car, I can run, but also can take people to run");} public static void main (String [] args) {car Car = new Car ("Best BMW ", 3, "black", "leather"); car. run ();}}

This is the same as the above. If you do not create a constructor when defining a class, the system will define a non-argument constructor by default.

Attribute in the class: [modifier] attribute type attribute name = [default value]

You can assign an initial value directly or without assigning a value. The system will default the initial value.

The attribute can be a basic data type or a reference data type.

Methods In a class: defining the functions of this class, such as eating, sleeping, and fighting, can be understood in this way. The attribute is a noun, and the method is a verb, the method shows what the class can do and what actions it has.

Method Definition Format:

[Modifier] method return value type method name (parameter list ){

// N statements

}

Class and object instance:

1Define a "point "(PointClass is used to represent a point in a three-dimensional space (with three coordinates ). The requirements are as follows:

2You can generate vertex objects with specific coordinates.

3You can set three coordinates.

4Provides a method to calculate the distance from this "point" to another point.

Class Point {double x, y, z; public Point (double _ x, double _ y, double _ z) {x = _ x; y = _ y; z = _ z;} public double getDistance (Point p) {// pass the parameter, which is an object of Point
// Calculate the return Math distance between two points. sqrt (x-p. x) * (x-p. x) + (y-p. y) * (y-p. y) + (z-p. z) * (z-p. z) ;}} class Test {public static void main (String [] args) {Point p = new Point (5.0, 6.0, 7.8 ); point origin = new Point (0.0, 0.0, 0.0 );
// Obtain the distance from System. out. println (p. getDistance (origin) by calling the method ));}}

Result

 

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.