Java object-Oriented learning notes--1 (classes, objects, constructors)

Source: Internet
Author: User

1. Class

1) is the concept of the same type of thing, is the description of things in real life, the mapping to Java description is class definition classes.

2) The definition of a class is in the description of things, that is, the definition of attributes (variables) and methods (functions).

3) classes can be declared: properties, methods, constructors;

A property is an instance variable that declares the structure of an object, allocates memory when the object is created, and has one copy of each object!

Instance variables (object properties) are allocated in the heap and act on the entire class, and the instance variables have default values, which can participate in the operation without initialization.

4) The relationship between classes and classes:

① Association: A class as a member variable of another class

public class A {

Pulic b b;

}

Class B {}

② Inheritance:

public class B extends A {}

Class A {}

③ dependency: The individual method is related to another class.

public class A {

public void F (b b) {}//parameter has class B

Public B G () {}//return value has B

}

Class B {}

Why are classes introduced?

First look at a case:

Requirement: Implement employee information management, store the following data (name, gender, age, salary) in the information management system, and then output the information of the employee.

Case 1:

650) this.width=650; "title=" Clipboard.png "src=" http://s3.51cto.com/wyfs02/M00/6D/39/ Wkiom1venshivg-caaevqh9hhlc973.jpg "width=" 669 "height=" 269 "alt=" wkiom1venshivg-caaevqh9hhlc973.jpg "/>

The above output is an employee information, if the actual enterprise application, there are a lot of, how to operate, like the following?

Case 2:

650) this.width=650; "title=" Clipboard.png "src=" http://s3.51cto.com/wyfs02/M00/6D/34/ Wkiol1veonwgcg5naakqrkqfllo669.jpg "width=" 645 "height=" 469 "alt=" Wkiol1veonwgcg5naakqrkqfllo669.jpg "/>

Obviously, the actual demand has not been satisfied, so there is object-oriented programming.

Class Design steps:

The first step: Analyzing the characteristics of these objects and then abstracting the data types, so-called abstract data types can be understood as: the collection of different types of data as a whole to describe a new thing.

The second step is to define the constructor of the class according to the requirements, which is used to initialize the properties of the class.

The third step: the analysis of the behavior of these objects or can be implemented operations, etc., defined as a method.

This completes the design of a class and then writes the class according to the design.

Case 3:emp Entity class

650) this.width=650; "style=" width:674px;height:436px; "title=" Clipboard.png "src=" http://s3.51cto.com/wyfs02/ M01/6d/34/wkiol1veogcb9t0xaahvae9wt-k376.jpg "width=" 711 "height=" 471 "alt=" wkiol1veogcb9t0xaahvae9wt-k376.jpg "/ >

Summary: a class is a template that is used to build objects, objects are instantiated through a class, and a class can create multiple objects, each with its own data (properties) and behavior (methods).

2. Objects

object is an objective and unique entity, and it is the starting point and foundation of analyzing and solving problems in object-oriented programming process. The essence of an object is a chunk of memory, and its data structure is determined by the class that defines it. Its essence is data.

stack diagram for object creation

650) this.width=650, "title=" oq$he{e) gtuj.png "src=" http://s3.51cto.com/wyfs02/M01/6D/34/ Wkiol1veoroqkadvaagkol2xxl8095.jpg "width=" 519 "height=" 377 " alt=" wkiol1veoroqkadvaagkol2xxl8095.jpg "/>

3. Object-oriented core features: encapsulation, inheritance, polymorphism

encapsulation: is to separate the use and implementation of a class, only to retain some interfaces and methods and external relations;

Inheritance: Subclasses inherit properties and methods from their parent classes, and can add new properties and methods or override partial properties and methods, and inheritance increases the reusability of the Code;

Polymorphism: Although all of the subclasses have the same method, the objects instantiated by these subclasses can obtain completely different results after invoking these same methods, and polymorphism enhances the flexibility of the software.

4. Construction method

Construction methods are also called constructors.

1) The name of the constructor must be the same as the class name, including case.

2) The constructor does not return a value, but it cannot write void, nor can it write a return.

3) constructor parameters are generally consistent with instance variables and are prerequisites for initializing objects.

4) Call with new! And once the object is established, the constructor runs and runs only once. The general method can be called multiple times.

5) class must have a constructor! This is true, no need to question!

6) If the class does not declare (define) any of the constructors, the Java compiler will automatically insert the default constructor!

7) The default construct is no parameter, the method body is an empty constructor, and the access rights of the default constructor vary with the access rights of the owning class. For example, if the class is modified by public, the default constructor is also with the public modifier.

8) The default constructor is not visible, once you write on the constructor, the default constructor is not, it is written by the custom constructor, even if it is written by the constructor of the empty argument, but also the custom constructor, not the default constructor.

9) If the class declares a constructor, the Java compiler will no longer provide a default constructor. If you do not manually write the parameterless constructor, but call the parameterless constructor, will be an error!

10) constructors can be overloaded and overloaded for convenience, and overloading rules are the same as method overloading rules.

11) The constructor is not inherited! Although it is called the construction method, but in fact it is not often said general method.

5. This keyword

In the constructor, the This keyword is often present, substituting the current object, that is, which object calls this constructor, and which object it refers to.

The use of this:

1) this. Member Variable---Access member variable

2) This ()---calls the constructor of this class (which is typically used to call another constructor in the constructor)

Case 4:

650) this.width=650; "title=" Clipboard.png "src=" http://s3.51cto.com/wyfs02/M00/6D/39/ Wkiom1veo4jhjk4xaahtygkesbg422.jpg "alt=" Wkiom1veo4jhjk4xaahtygkesbg422.jpg "/>

This article is from "Forever Young" blog, please be sure to keep this source http://kingkongzhao.blog.51cto.com/6319491/1653805

Java object-Oriented learning notes--1 (classes, objects, constructors)

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.