Java Learning (ix), object-oriented programming

Source: Internet
Author: User
Tags abstract definition

I. Understanding Object-oriented

Structured programming

1. program = algorithm + data structure

2, algorithm first, data structure second

Object-oriented Programming OOP

1. Program = Object + Object + ...

2, according to the first, the algorithm second

3, the key: let each object responsible for performing a set of related tasks

Three, object-oriented programming characteristics

1. Objects of all Things

2, the program is a group of objects between each other in the sending message

3, each object has its own memory footprint, can be assembled into a larger object

4, each object has a type, all types of a particular object can receive the same message

Iv. concepts, distinctions and linkages of classes and objects

Concept:

Class ① class is the template and blueprint for creating objects ② class is a common abstract definition of a set of similar objects

Object ① object is the instantiation result of a class ② object is a real existence, representing something in the real world

Two key attributes of an object ① behavior: What an object can do ② state: The properties of an object, the result of a behavior

Difference: The ① class is statically defined ② object is a dynamic instance ③ program code is manipulated by objects rather than classes ④ modeling the resulting class rather than the object

Contact: ① class is the definition of an object ② object can not be separated from the modeling of class ③ class exists to instantiate the resulting object

Modeling of Classes : Abstraction and encapsulation processes

To define a class: ① defines the class name ② the Writing field represents the property ③ the method that writes the class represents the behavior

Class modeling is an abstraction and encapsulation process

Abstraction: Removing unimportant, minor information and preserving important information

Encapsulation: Information packaging, Simply put: wrapping the abstract properties and behaviors in a class.

Case study: Student class modeling

Case Description: Write a student class that describes a student object with certain behaviors and attributes

Case Design: ① modeling A student class ② defines a series of fields to describe students ' properties ③ defines a series of methods to describe students ' behavior

1  Public classStudent2 {3      PublicString number;4      PublicString name;5      Public intAge ;6      PublicString Major;7     8     //class Method9      Public voidAttendclass () {Ten          One     } A      -      Public voidExan () { -          the     } -}
View Code

Vi. creation and use of objects

Creating and working with objects: creating objects with the New keyword

Example:

public class Example ()

{

int field1;

Double field2;

public class void Main (String []args)

{

Example e=new Example ();

}

}

Use the member accessor "." Manipulating objects

Assign a value to an object property: The object name. Properties

A method is called when a message is sent to an object: Object name. Method ()

Example:

Example e=new Example ();

e.field1=100;

e.field2=200.0;

System.out.println (E.tostring ());

Code examples

1  Public classDog2 {3      PublicString brand;//Variety4      Public intAge//Age5     //Method6      PublicString toString () {7         return"This is a" +age+ "year" +brand;8     }9     Ten      Public Static voidMain (String []args) { OneDog dog=NewDog ();//constructs an object of the dog class Adog.brand= "Chinese Pastoral Dog"; -Dog.age=3; - System.out.println (dog.tostring ());  the     } -}
View Code

Vii. definition and invocation of methods

A method of a class represents a behavior (or function) of an instance

Method Definition: Method type, method signature (method name + parameter list), method body

Methods for defining classes

access modifier Type method name (parameter list) {

Method body

}

You can think of a method as a module, a "black box," complete a specific function, and return to the processing result.

Method Classification: ① return value is (void) void method ② method with specific return type ③ method without parameters ④ with parameters

Method return Value: If the method has a return value, the method must return the value using the keyword return, which is the return value type defined by the method

☆ Method can call other methods inside

Calling a method with parameters takes a value pass: ① If the parameter is a base data type, parameter passing is passing the numeric value of the parameter to the method ② if the argument is an object or an array, parameter passing is the process of passing a value copy of the reference value of an object or an array to the method ③ recursion argument

void Method1 () {

...

...

METHOD2 (1,2.0);//method invocation, pass-through argument (1,2.0)

}

METHOD2 (int i,double d) {

I and D represent formal parameters, which are called after i=1;d=2.0; data types must be identical when you pass the arguments

...

...

}

 Public classmethoddemo{ Public voidmethod1 () {System.out.println ("Return method with null type and no parameters"); }         PublicString method2 () {return"Method with no parameters with a specific return type"; }         Public voidmethod3 (String name) {System.out.println ("Returns a method with an empty type and a parameter with a value of" +name); }         PublicString Method4 (string name,intAge ) {        return"A method with a specific return type with a parameter with the value" +name+ "," +Age ; }        //passing basic data types     Public voidOperatordata (intAge ) {        //The age here is defined as the age copy value outside, not the outside of the age+1;age++; System.out.println ("Age in Method:" +Age ); }        //methods for passing reference data types     Public voidOperatorarray (int[] Array) {array[2]=100; System.out.println ("Array[2 in the method]:" +array[2]); }         Public Static voidMain (String []args) {Methoddemo demo=NewMethoddemo ();        Demo.method1 (); String msg=demo.method2 ();        SYSTEM.OUT.PRINTLN (msg); DEMO.METHOD3 ("Zhang San"); System.out.println (DEMO.METHOD4 ("Zhang San", 20)); intAge=20;        Demo.operatordata (age); System.out.println ("Age outside of Method:" +Age ); int[] array={1,2,3,4};        Demo.operatorarray (array); System.out.println ("Array[2 outside the method]:" +array[2]); } }
View Code

In the code above

The values in the method and outside the method are 11 and 10, respectively, when passing the base data type

When passing a reference data type, the value in the method and outside of the method is 100

Differential Analysis:

The basic data type is stored in memory in the stack memory, when using int to create a stack frame (age), the method is called after a stack frame (age), the method is the second one, when the method is finished after the second age out of the stack, but the second age changed the value and print, does not affect the first , the main method continues to print the output age (at this time the first one); The reference data type creates a stack frame (array) when using array, points to the memory space in the heap, and then produces a stack frame (array), which is passed by the first array (address). It also points to the memory space in the heap, the second within the method, when the second array is out of the stack after the method is called, and the value in the array array[2] in the memory space in the pointed heap changes, so 2 array[2] outputs the same value.

The main difference is that the base data type is stored in the stack, whereas the reference data type simply stores the address in the stack, and the real value is stored in the heap. For example int[] a=new int[5]; Where a is stored in the stack, a is simply a reference, the address of the array placed in the stack, and the real array is placed in the heap.

Viii. Scope of variables

public class Example () {

Public type variable 1;

Public type variable 2;

Public Type Method 1 () {

type variable 3;

}

Public Type Method 2 () {

type variable 4;

}

}

In the example above, variable 3 and variable 4 are local variables, variable 3 can only be used in method 1 , and variable 4 can only be used in method 2 . Variables 1 and 2 are member variables ( properties )that can be used in method 1 and Method 2 .

Keep updating ...

Java Learning (ix), object-oriented programming

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.