The class of the Java self-study article

Source: Internet
Author: User
Tags multiple inheritance in java

Make up for yesterday's blog.

Object-Oriented Programming

1 Related Concepts

What does object-oriented mean?

Object-oriented programming is to apply the thought method commonly used in the process of understanding the world to the program design.

Objects are the things that exist in the real world, they are tangible, can also be intangible, the characteristics of objects are divided into static characteristics and dynamic characteristics of two.

Static characteristics refer to the appearance, nature and properties of the object;

Dynamic characteristics refer to the function, behavior, etc. of an object.

objective things are complex, but people always from a certain point of view, using the ability of abstract analysis, from a number of features to extract the most representative, the most reflective of the nature of the characteristics of the object to be studied in detail. People abstract the static characteristics of objects into attributes, which are described by data, are called variables in the Java language, and people abstract the dynamic characteristics of objects into behavior, which is represented by a set of code, and the operation of the data is done in the Java language, which is called the method. An object consists of a set of properties and a set of methods that manipulate the property.

A set of objects that have the same properties and the same behavior is called a class. In object-oriented programming, a class is a single unit that has a class name, which includes member variables that describe the properties of an object, and a member method of the class that describes the behavior of the object.

in Java programming, a class is considered an abstract data type that includes not only data but also methods.

To solve a problem by using a class, you must create an instantiated class object with a class, and then access the class's member variables through the class object to invoke the class's member methods to implement the program's functionality.

A class can create multiple class objects, they have the same attribute pattern, However, you can have different property values. the Java program opens up memory space for each class object to hold its own property values.

Properties of the class

* Package Nature

Encapsulation is an important principle that an object-oriented approach should follow. It has two meanings: one refers to the object's attributes and behavior as an inseparable whole, the two "encapsulated" in an indivisible independent unit (i.e. object).

Another feature of the encapsulation mechanism is that different levels of "visibility" or access rights are specified for variables and methods encapsulated within a whole.

* Inheritance

have classes that reflect the general nature of things, and then derive a class that reflects a particular thing on its basis. In Java Programming, the existing classes can be the Java Development environment provides a batch of the most basic program-class library. User-developed program classes are inherited from these existing classes. An inherited class is called a parent class or a superclass, and a class that inherits from it is called a subclass or derived class.

The inheritance mechanism in object-oriented programming greatly enhances the reusability of program code, improves the development efficiency of software, reduces the possibility of errors in program, and facilitates the modification and expansion of programs.

If a subclass is allowed to inherit only one parent class, it is called single inheritance, and if more than one parent class is allowed, it is called multiple inheritance. Many object-oriented programming languages currently do not support multiple inheritance. the Java language uses interfaces (interface) to compensate for the fact that subclasses that do not support multiple inheritance in Java cannot enjoy the imperfections of members of more than one parent class.

* polymorphism

Polymorphism is allowed to appear in the program duplicate name phenomenon; The Java language contains method overloads and members covering two forms of polymorphism.

Method overloading: In a class, multiple methods are allowed to use the same name, but the parameters of the method are different and the functions are done differently.

Member overrides: Subclasses and parent classes allow the same variable name, but different data types allow the same method name, but complete functionality is different.

The concept of the above material is too strong, difficult to understand, read a few times, and then look at the following procedures, and then go back to read a few times this material, you can fully understand, here do not understand the following procedures in the various concepts I will use examples to explain these concepts. (There are a lot of things I don't understand at first, but I see that the program behind it can understand 80% hope you work hard! )

2 classes and Objects

from now on we are going to write the program, is object-oriented programming, classes (class) and objects (object) are the core concepts of object-oriented methods. A class is a description of something, an abstraction, a conceptual definition, an object that is actually an individual of that kind of thing, and thus an instance (Instance).

Multiple objects of the same class that are produced by the same method start with the same state, but when you modify one of the objects, the other objects are not affected.

Declaration of the class

class name

{

Data Type Properties ;

method Name (parameter 1, parameter 2 ...) )

{

Program Statements ; defining the contents of a method

return expression ;

}

}

Instance

class person// classes name (class name standard capitalization start)

{

String name; Defining Variables

int age;

void talk ()// Create method

{

System.out.println ("My name is:" +name+ "My old is:" +age);

}

}

To create an object format

class name Object Name = new class name ();

Creating an object that belongs to a class needs to be done in the following two steps:

1 . declaring A variable that points to " objects created by a class "

2 . use new to create a new object and assign it to a previously created variable.

Use the class above to create an object

Person p; declare the object p of a person class first

p = new person (); Instantiate the person's object p with the new keyword

You can also use the following format to write

Person P = Newperson (); declares the person object p and instantiates the object directly

Attention:

An object can be used only after it is instantiated, and the keyword of the instantiated object is new.

Accessing a variable or method in an object

Access attribute: Object name . Property name

access Method: Object name . Method Name ()

Let's look at an example to illustrate what's on top.

class person// define a category

{

String name; defining variables in a class

int age;

void talk ()// methods for creating classes

{

System.out.println ("My name is:" +name+ "My old is:" +age);

}

}

Public Classjava51

{

public static void Main (String [] args)

{

person P1 = new person (); instantiate an object with a class P1

person P2 = new person (); instantiate an object with a class P1

P1.name = "Robin"; accessing variables in a class and assigning values

P1.age = 25;

P2.name = "Lili"; instantiating multiple objects with a class

P2.age = 21;

P1.talk (); accessing methods in a class

P2.talk ();

}

}

See here I believe you can be divided into classes and objects clearly, but also a general understanding of what the class is for; Let's look at the first feature of a class - encapsulation

Encapsulation of

Let's take a look at the example

Class Person

{

String name;

int age;

void Talk ()

{

System.out.println ("My name is:" +name+ "My old is:" +age);

}

}

Public Classjava51

{

public static void Main (String [] args)

{

person P1 = new person ();

person P2 = new person ();

P1.name = "Robin";

P1.age =-25;

P2.name = "Lili";

P2.age = 21;

P1.talk ();

P2.talk ();

}

}

then look at the output, it must be Robin 's age is -25, obviously this program has a problem, in order to avoid such errors, we want to carry out the package of the program (private)

Look again at the example:

Class Person

{

private String name;

private int age;

void Talk ()

{

System.out.println ("My name is:" +name+ "My old is:" +age);

}

}

Public Classjava51

{

public static void Main (String [] args)

{

person P1 = new person ();

P1.name = "Robin";

P1.age =-25;

P1.talk ();

}

}

Error when compiling

650) this.width=650; "src="%5c "alt=" spacer.gif\ "/>

&nbsp123&nbsp123&nbsp123&nbsp123&nbsp123&nbsp123&nbsp123&nbsp123

&nbsp123&nbsp123&nbsp123&nbsp123&nbsp123&nbsp123&nbsp123&nbsp123

&nbsp123&nbsp123&nbsp123&nbsp123&nbsp123&nbsp123&nbsp123&nbsp123&nbsp123 &nbsp123&nbsp123&nbsp123&nbsp123

&nbsp123

&nbsp123

&nbsp123

&nbsp123&nbsp123&nbsp123 "" ""

&nbsp123

&nbsp123&nbsp123&nbsp123&nbsp123&nbsp123&nbsp123&nbsp123&nbsp123&nbsp123 &nbsp123&nbsp123&nbsp123&nbsp123&nbsp123&nbsp123&nbsp123&nbsp123&nbsp123 &nbsp123&nbsp123&nbsp123

&nbsp123

&nbsp123&nbsp123&nbsp123

&nbsp123

&nbsp123&nbsp123&nbsp123&nbsp123&nbsp123&nbsp123&nbsp123&nbsp123&nbsp123 &nbsp123&nbsp123&nbsp123&nbsp123&nbsp123&nbsp123&nbsp123&nbsp123&nbsp123 &nbsp123&nbsp123&nbsp123&nbsp123&nbsp123&nbsp123&nbsp123

&nbsp123

&nbsp123&nbsp123&nbsp123>

&nbsp123&nbsp123&nbsp123&nbsp123&nbsp123

&nbsp123

&nbsp123

&nbsp123

&nbsp123&nbsp123&nbsp123

&nbsp123

&nbsp123

&nbsp123

&nbsp123&nbsp123&nbsp123

&nbsp123

&nbsp123

&nbsp123

&nbsp123&nbsp123&nbsp123&nbsp123&nbsp123&nbsp123&nbsp123&nbsp123&nbsp123 &nbsp123&nbsp123&nbsp123&nbsp123&nbsp123&nbsp123&nbsp123&nbsp123&nbsp123

&nbsp123&nbsp123&nbsp123 "" &nbsp123&nbsp123&nbsp123&nbsp123&nbsp123&nbsp123 &nbsp123&nbsp123&nbsp123&nbsp123&nbsp123&nbsp123&nbsp123&nbsp123&nbsp123 &nbsp123&nbsp123&nbsp123&nbsp123&nbsp123&nbsp123&nbsp123&nbsp123

&nbsp123&nbsp123&nbsp123

&nbsp123&nbsp123&nbsp123&nbsp123&nbsp123&nbsp123&nbsp123&nbsp123&nbsp123 &nbsp123&nbsp123&nbsp123&nbsp123&nbsp123&nbsp123&nbsp123&nbsp123&nbsp123 &nbsp123&nbsp123&nbsp123&nbsp123&nbsp123&nbsp123&nbsp123&nbsp123&nbsp123 &nbsp123&nbsp123&nbsp123&nbsp123&nbsp123&nbsp123&nbsp123&nbsp123&nbsp123 &nbsp123&nbsp123&nbsp123&nbsp123&nbsp123

&nbsp123

&nbsp123

&nbsp123

&nbsp123&nbsp123&nbsp123&nbsp123&nbsp123&nbsp123&nbsp123&nbsp123&nbsp123

&nbsp123

&nbsp123&nbsp123&nbsp123 "" ""

&nbsp123

&nbsp123

&nbsp123

&nbsp123&nbsp123&nbsp123

&nbsp123

&nbsp123

&nbsp123

&nbsp123&nbsp123&nbsp123>

&nbsp123&nbsp123&nbsp123&nbsp123&nbsp123

&nbsp123

&nbsp123

&nbsp123

&nbsp123&nbsp123&nbsp123

&nbsp123

&nbsp123

&nbsp123

&nbsp123&nbsp123&nbsp123

&nbsp123

&nbsp123

&nbsp123

&nbsp123&nbsp123&nbsp123

&nbsp123&nbsp123&nbsp123 ""

&nbsp123&nbsp123&nbsp123

&nbsp123&nbsp123&nbsp123

&nbsp123

650) this.width=650; "src="%5c "alt=" spacer.gif\ "/>

&nbsp123

&nbsp123

&nbsp123

&nbsp123

&nbsp123&nbsp123&nbsp123 "" ""

&nbsp123

&nbsp123&nbsp123&nbsp123&nbsp123&nbsp123&nbsp123&nbsp123&nbsp123&nbsp123 &nbsp123&nbsp123&nbsp123

&nbsp123

&nbsp123&nbsp123&nbsp123&nbsp123&nbsp123&nbsp123&nbsp123&nbsp123&nbsp123 &nbsp123&nbsp123&nbsp123&nbsp123&nbsp123&nbsp123&nbsp123&nbsp123&nbsp123 &nbsp123&nbsp123&nbsp123&nbsp123&nbsp123&nbsp123&nbsp123&nbsp123&nbsp123 &nbsp123

&nbsp123

&nbsp123

&nbsp123

&nbsp123&nbsp123&nbsp123

&nbsp123

&nbsp123

&nbsp123

&nbsp123&nbsp123&nbsp123>

&nbsp123&nbsp123&nbsp123&nbsp123&nbsp123

&nbsp123

&nbsp123

&nbsp123

&nbsp123&nbsp123&nbsp123

&nbsp123

&nbsp123

&nbsp123

&nbsp123&nbsp123&nbsp123

&nbsp123

&nbsp123

&nbsp123

&nbsp123&nbsp123&nbsp123

&nbsp123&nbsp123&nbsp123 ""

&nbsp123&nbsp123&nbsp123

&nbsp123&nbsp123&nbsp123

&nbsp123

&nbsp123

&nbsp123

&nbsp123

&nbsp123

&nbsp123&nbsp123&nbsp123&nbsp123&nbsp123&nbsp123&nbsp123&nbsp123

&nbsp123&nbsp123&nbsp123&nbsp123&nbsp123&nbsp123&nbsp123&nbsp123

&nbsp123&nbsp123&nbsp123&nbsp123

&nbsp123&nbsp123&nbsp123 ""

&nbsp123&nbsp123&nbsp123

&nbsp123&nbsp123&nbsp123&nbsp123&nbsp123&nbsp123&nbsp123&nbsp123&nbsp123 &nbsp123&nbsp123&nbsp123&nbsp123&nbsp123&nbsp123

&nbsp123&nbsp123&nbsp123 ""

&nbsp123&nbsp123&nbsp123 ""

&nbsp123

&nbsp123

&nbsp123

&nbsp123

&nbsp123&nbsp123&nbsp123

&nbsp123&nbsp123&nbsp123

&nbsp123&nbsp123&nbsp123

&nbsp123&nbsp123&nbsp123 ""

&nbsp123

&nbsp123

&nbsp123

&nbsp123&nbsp123&nbsp123

&nbsp123

&nbsp123

&nbsp123

&nbsp123

&nbsp123

&nbsp123 ""

&nbsp123

&nbsp123

&nbsp123

&nbsp123&nbsp123&nbsp123 "" "" ""

&nbsp123

&nbsp123

&nbsp123

&nbsp123&nbsp123&nbsp123

&nbsp123

&nbsp123

&nbsp123

&nbsp123&nbsp123&nbsp123 ""

&nbsp123

&nbsp123

&nbsp123

&nbsp123

&nbsp123&nbsp123&nbsp123

&nbsp123

&nbsp123

&nbsp123

&nbsp123

&nbsp123

&nbsp123&nbsp123&nbsp123

&nbsp123&nbsp123&nbsp123

&nbsp123&nbsp123&nbsp123 ""

&nbsp123

&nbsp123

&nbsp123

&nbsp123&nbsp123&nbsp123 "" ""

&nbsp123

&nbsp123

&nbsp123

&nbsp123

&nbsp123&nbsp123&nbsp123 "" &nbsp123&nbsp123&nbsp123&nbsp123&nbsp123&nbsp123 &nbsp123&nbsp123&nbsp123&nbsp123&nbsp123&nbsp123&nbsp123&nbsp123&nbsp123 &nbsp123&nbsp123

&nbsp123&nbsp123&nbsp123

&nbsp123&nbsp123&nbsp123 "" &nbsp123&nbsp123&nbsp123&nbsp123&nbsp123&nbsp123 &nbsp123&nbsp123&nbsp123&nbsp123&nbsp123&nbsp123&nbsp123&nbsp123&nbsp123 &nbsp123&nbsp123&nbsp123&nbsp123&nbsp123

&nbsp123&nbsp123&nbsp123&nbsp123&nbsp123&nbsp123&nbsp123&nbsp123&nbsp123 &nbsp123&nbsp123&nbsp123&nbsp123&nbsp123&nbsp123&nbsp123&nbsp123&nbsp123 &nbsp123&nbsp123&nbsp123&nbsp123&nbsp123&nbsp123&nbsp123&nbsp123&nbsp123 &nbsp123&nbsp123&nbsp123&nbsp123

&nbsp123

&nbsp123

&nbsp123

&nbsp123

&nbsp123

&nbsp123&nbsp123&nbsp123

&nbsp123&nbsp123&nbsp123

&nbsp123&nbsp123&nbsp123 ""

&nbsp123

&nbsp123

&nbsp123

&nbsp123&nbsp123&nbsp123 "" ""

&nbsp123

&nbsp123

&nbsp123

&nbsp123

&nbsp123&nbsp123&nbsp123

&nbsp123&nbsp123&nbsp123

&nbsp123

650) this.width=650; "src="%5c "alt=" spacer.gif\ "/>

&nbsp123


This article is from the "Ops era" blog, please be sure to keep this source http://daomeng.blog.51cto.com/1655407/1826257

The class of the Java self-study article

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.